diff --git a/app/controllers/api/v1/saved_scenarios_controller.rb b/app/controllers/api/v1/saved_scenarios_controller.rb index 580ef48..16710f6 100644 --- a/app/controllers/api/v1/saved_scenarios_controller.rb +++ b/app/controllers/api/v1/saved_scenarios_controller.rb @@ -38,7 +38,7 @@ def update result = SavedScenario::Update.call( engine_client, @saved_scenario, - saved_scenario_params + saved_scenario_params.except(:version) ) if result.successful? @@ -62,7 +62,7 @@ def destroy # Only allow a list of trusted parameters through. def saved_scenario_params params.require(:saved_scenario).permit( - :scenario_id, :title, + :scenario_id, :title, :version, :description, :area_code, :end_year, :private, :discarded ) end diff --git a/app/controllers/api/v1/versions_controller.rb b/app/controllers/api/v1/versions_controller.rb index 8623ac6..dad499c 100644 --- a/app/controllers/api/v1/versions_controller.rb +++ b/app/controllers/api/v1/versions_controller.rb @@ -1,29 +1,11 @@ module Api module V1 class VersionsController < BaseController + skip_before_action :authenticate_request! def index - versions = Version.all - base_url = request.base_url - - version_data = versions.map do |version| - { - version: version, - url: version_url(version, base_url) - } - end - - render json: { versions: version_data }, status: :ok + render json: { versions: Version.as_json }, status: :ok end - - private - - # Generates a version URL given a version name and the base URL - def version_url(version, base_url) - "https://#{version}.#{base_url}" - end - - end end end diff --git a/app/models/saved_scenario.rb b/app/models/saved_scenario.rb index a775be5..75a11b2 100644 --- a/app/models/saved_scenario.rb +++ b/app/models/saved_scenario.rb @@ -27,7 +27,7 @@ class SavedScenario < ApplicationRecord validates :end_year, presence: true validates :area_code, presence: true validates :version, presence: true, inclusion: { - in: Version.all, message: "Version should be one of #{Version.all}" + in: Version.tags, message: "Version should be one of #{Version.tags}" } serialize :scenario_id_history, coder: YAML, type: Array diff --git a/app/models/version.rb b/app/models/version.rb index 80ae200..9ddafda 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -2,13 +2,25 @@ # A valid version of the ETM class Version - LIST = %w[ - latest - ].freeze + URL = "energytransitionmodel.com".freeze + + LIST = { + "latest" => "https://#{Version::URL}", + "stable.01" => "https://stable.#{Version::URL}", + "stable.02" => "https://stable2.#{Version::URL}" + }.freeze # All available versions. Uses ActiveRecord syntax 'all' to # make future porting to db easier def self.all LIST end + + def self.tags + LIST.keys + end + + def self.as_json(*) + LIST.map { |tag, url| { tag: tag, url: url } } + end end diff --git a/spec/controllers/api/v1/versions_controller_spec.rb b/spec/controllers/api/v1/versions_controller_spec.rb index 426472e..2080768 100644 --- a/spec/controllers/api/v1/versions_controller_spec.rb +++ b/spec/controllers/api/v1/versions_controller_spec.rb @@ -18,9 +18,8 @@ parsed_response = JSON.parse(response.body) expect(parsed_response['versions']).to be_present - expect(parsed_response['versions'].map { |v| v['version'] }).to match_array(Version.all) + expect(parsed_response['versions'].map { |v| v['tag'] }).to match_array(Version.tags) expect(parsed_response['versions'].first).to have_key('url') - puts parsed_response end end end