From 34088ae097ddeb5db035891120f4497160decfbe Mon Sep 17 00:00:00 2001 From: noracato Date: Tue, 17 Dec 2024 11:35:08 +0100 Subject: [PATCH 1/3] Update button styles --- app/components/login/button_component.rb | 2 +- app/views/layouts/_buttons.html.haml | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/components/login/button_component.rb b/app/components/login/button_component.rb index b6e88cf..52adb2c 100644 --- a/app/components/login/button_component.rb +++ b/app/components/login/button_component.rb @@ -3,7 +3,7 @@ module Login class ButtonComponent < ActionButtonComponent def initialize(form:) - super(form:, type: :submit, color: :primary, size: :lg, class: "w-full !py-3 mt-5 bg-midnight-600") + super(form:, type: :submit, color: :primary, size: :lg, class: "w-full !py-3 mt-5 bg-midnight-600 text-midnight-800") end end end diff --git a/app/views/layouts/_buttons.html.haml b/app/views/layouts/_buttons.html.haml index 700952f..c1df0c8 100644 --- a/app/views/layouts/_buttons.html.haml +++ b/app/views/layouts/_buttons.html.haml @@ -1,3 +1,5 @@ .flex.py-5 - .bg-gray-100.p-2.px-5.mr-0.ml-auto.rounded-md - = link_to t('continue_working'), last_visited_page_path, class: 'continue-button' + .flex.basis-full{class: 'lg:basis-3/4'} + - if cookies[:last_visited_page].present? + .bg-gray-100.p-2.px-5.mr-0.ml-auto.rounded-md + = link_to t('continue_working'), last_visited_page_path, class: 'continue-button' From c3fc4ec086e9a700c7e8d734c1f9ac6dd4431215 Mon Sep 17 00:00:00 2001 From: louispt1 Date: Tue, 3 Dec 2024 11:24:31 +0100 Subject: [PATCH 2/3] Added versions API and tests --- app/controllers/api/v1/versions_controller.rb | 29 +++++++++++++++++++ config/routes.rb | 1 + .../api/v1/versions_controller_spec.rb | 26 +++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 app/controllers/api/v1/versions_controller.rb create mode 100644 spec/controllers/api/v1/versions_controller_spec.rb diff --git a/app/controllers/api/v1/versions_controller.rb b/app/controllers/api/v1/versions_controller.rb new file mode 100644 index 0000000..8623ac6 --- /dev/null +++ b/app/controllers/api/v1/versions_controller.rb @@ -0,0 +1,29 @@ +module Api + module V1 + class VersionsController < BaseController + + 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 + 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/config/routes.rb b/config/routes.rb index 83baffe..50cf62d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -84,6 +84,7 @@ resources :collections, only: [:index, :show, :create, :destroy] resources :saved_scenarios, only: %i[index show create update destroy] resources :featured_scenarios, only: %i[index show] + resources :versions, only: [:index] end end diff --git a/spec/controllers/api/v1/versions_controller_spec.rb b/spec/controllers/api/v1/versions_controller_spec.rb new file mode 100644 index 0000000..426472e --- /dev/null +++ b/spec/controllers/api/v1/versions_controller_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe 'API::V1::Versions', type: :request do + let(:user) { create(:user) } + + describe 'GET /api/v1/versions' do + before do + get '/api/v1/versions', + as: :json, + headers: access_token_header(user, :read) + end + + it 'returns a successful response' do + expect(response).to have_http_status(:ok) + end + + it 'returns all versions with their URLs' do + 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'].first).to have_key('url') + puts parsed_response + end + end +end From 47dd7ed844f12c122bd6c8f542c4a49238453bfe Mon Sep 17 00:00:00 2001 From: noracato Date: Tue, 17 Dec 2024 12:26:10 +0100 Subject: [PATCH 3/3] Setup versions APi to communicate with ETModel --- .../api/v1/saved_scenarios_controller.rb | 4 ++-- app/controllers/api/v1/versions_controller.rb | 22 ++----------------- app/models/saved_scenario.rb | 2 +- app/models/version.rb | 18 ++++++++++++--- .../api/v1/versions_controller_spec.rb | 3 +-- 5 files changed, 21 insertions(+), 28 deletions(-) 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