From c0082cc6dbcb045a97282a35c75f9a0089688684 Mon Sep 17 00:00:00 2001 From: "rasika.abeyrathna" Date: Thu, 11 Apr 2024 18:55:49 +0100 Subject: [PATCH] GL-103: Add Admin UI for creating category assessment data --- .../category_assessments_controller.rb | 26 +++++++++++ app/models/green_lanes/theme.rb | 27 ++++++++++++ .../category_assessments/_form.html.erb | 24 +++++++++++ .../category_assessments/index.html.erb | 2 + .../category_assessments/new.html.erb | 5 +++ config/routes.rb | 2 +- spec/factories/green_lanes/theme_factory.rb | 22 ++++++++++ .../category_assessments_controller_spec.rb | 43 +++++++++++++++++++ 8 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 app/models/green_lanes/theme.rb create mode 100644 app/views/green_lanes/category_assessments/_form.html.erb create mode 100644 app/views/green_lanes/category_assessments/new.html.erb create mode 100644 spec/factories/green_lanes/theme_factory.rb diff --git a/app/controllers/green_lanes/category_assessments_controller.rb b/app/controllers/green_lanes/category_assessments_controller.rb index d0da15e8..79fa1dc7 100644 --- a/app/controllers/green_lanes/category_assessments_controller.rb +++ b/app/controllers/green_lanes/category_assessments_controller.rb @@ -1,12 +1,38 @@ module GreenLanes class CategoryAssessmentsController < AuthenticatedController + before_action :disable_service_switching! before_action :check_service def index @category_assessments = GreenLanes::CategoryAssessment.all(page: current_page).fetch end + def new + @category_assessment = GreenLanes::CategoryAssessment.new + @themes = GreenLanes::Theme.all.fetch + end + + def create + @category_assessment = GreenLanes::CategoryAssessment.new(ca_params) + + if @category_assessment.valid? && @category_assessment.save + redirect_to green_lanes_category_assessments_path, notice: 'Category Assessment created' + else + @themes = GreenLanes::Theme.all.fetch + render :new + end + end + private + def ca_params + params.require(:category_assessment).permit( + :regulation_id, + :regulation_role, + :measure_type_id, + :theme_id, + ) + end + def check_service if TradeTariffAdmin::ServiceChooser.uk? raise ActionController::RoutingError, 'Invalid service' diff --git a/app/models/green_lanes/theme.rb b/app/models/green_lanes/theme.rb new file mode 100644 index 00000000..41dc5812 --- /dev/null +++ b/app/models/green_lanes/theme.rb @@ -0,0 +1,27 @@ +module GreenLanes + class Theme + include Her::JsonApi::Model + use_api Her::XI_API + + MAX_LENGTH = 50 + + attributes :section, + :subsection, + :theme, + :description, + :category, + :created_at, + :updated_at + + collection_path '/admin/themes' + + def label + "#{category} - #{short_theme}" + end + + private + def short_theme + theme.length > MAX_LENGTH ? "#{theme[0...MAX_LENGTH]}..." : theme + end + end +end diff --git a/app/views/green_lanes/category_assessments/_form.html.erb b/app/views/green_lanes/category_assessments/_form.html.erb new file mode 100644 index 00000000..a84de504 --- /dev/null +++ b/app/views/green_lanes/category_assessments/_form.html.erb @@ -0,0 +1,24 @@ +<%= govuk_form_for category_assessment, as: :category_assessment do |f| %> + + <%= f.govuk_text_field :measure_type_id, + label: { text: 'Measure Type Id' }, + width: 'one-half' %> + + <%= f.govuk_text_field :regulation_id, + label: { text: 'Regulation Id' }, + width: 'one-half' %> + + <%= f.govuk_text_field :regulation_role, + label: { text: 'Regulation Role' }, + width: 'one-half' %> + + <%= f.govuk_collection_select :theme_id, + @themes, + :id, + :label, + options: { include_blank: 'Select Theme' }, + label: { text: 'Select Theme' } %> + + <%= submit_and_back_buttons f, green_lanes_category_assessments_path %> +<% end %> + diff --git a/app/views/green_lanes/category_assessments/index.html.erb b/app/views/green_lanes/category_assessments/index.html.erb index 94720616..aa05cad8 100644 --- a/app/views/green_lanes/category_assessments/index.html.erb +++ b/app/views/green_lanes/category_assessments/index.html.erb @@ -2,6 +2,8 @@ Manage category assessments +<%= link_to 'Add a Category Assessment', new_green_lanes_category_assessment_path, class: 'govuk-button' %> + <% if @category_assessments.any? %> diff --git a/app/views/green_lanes/category_assessments/new.html.erb b/app/views/green_lanes/category_assessments/new.html.erb new file mode 100644 index 00000000..0fdcdf94 --- /dev/null +++ b/app/views/green_lanes/category_assessments/new.html.erb @@ -0,0 +1,5 @@ +<%= govuk_breadcrumbs 'Category Assessments': green_lanes_category_assessments_path %> + +

New Category Assessment

+ +<%= render 'form', category_assessment: @category_assessment %> diff --git a/config/routes.rb b/config/routes.rb index 9b7fa5a4..9ee38e65 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,7 +63,7 @@ end namespace :green_lanes, path: 'green_lanes' do - resources :category_assessments, only: %i[index] + resources :category_assessments, only: %i[index new create] end resources :tariff_updates, only: %i[index show] do diff --git a/spec/factories/green_lanes/theme_factory.rb b/spec/factories/green_lanes/theme_factory.rb new file mode 100644 index 00000000..a4397df9 --- /dev/null +++ b/spec/factories/green_lanes/theme_factory.rb @@ -0,0 +1,22 @@ +FactoryBot.define do + factory :green_lanes_theme, class: 'GreenLanes::Theme' do + sequence(:id) { |n| n } + section { 1 } + sequence(:subsection) { |n| n } + sequence(:theme) { |n| "Theme #{n}" } + description { 'Some description' } + category { 2 } + + trait :category1 do + category { 1 } + end + + trait :category2 do + category { 2 } + end + + trait :category3 do + category { 3 } + end + end +end diff --git a/spec/requests/green_lanes/category_assessments_controller_spec.rb b/spec/requests/green_lanes/category_assessments_controller_spec.rb index 0419223d..6f669ccf 100644 --- a/spec/requests/green_lanes/category_assessments_controller_spec.rb +++ b/spec/requests/green_lanes/category_assessments_controller_spec.rb @@ -17,5 +17,48 @@ let(:make_request) { get green_lanes_category_assessments_path } it { is_expected.to have_http_status :success } + it { is_expected.not_to include 'div.current-service' } end + + describe 'GET #new' do + before do + stub_api_request('/admin/themes', backend: 'xi').and_return \ + jsonapi_response :themes, attributes_for_list(:green_lanes_theme, 3) + end + + let(:make_request) { get new_green_lanes_category_assessment_path } + + it { is_expected.to have_http_status :ok } + it { is_expected.not_to include 'div.current-service' } + end + + describe 'POST #create' do + before do + stub_api_request('/admin/category_assessments', :post).to_return create_response + stub_api_request('/admin/themes', backend: 'xi').and_return \ + jsonapi_response :themes, attributes_for_list(:green_lanes_theme, 3) + end + + let :make_request do + post green_lanes_category_assessments_path, + params: { category_assessment: ca_params } + end + + context 'with valid item' do + let(:ca_params) { category_assessment.attributes.without(:id) } + let(:create_response) { webmock_response(:created, category_assessment.attributes) } + + it { is_expected.to redirect_to green_lanes_category_assessments_path } + end + + context 'with invalid item' do + let(:ca_params) { category_assessment.attributes.without(:id, :measure_type_id) } + let(:create_response) { webmock_response(:error, measure_type_id: "can't be blank'") } + + it { is_expected.to have_http_status :ok } + it { is_expected.to have_attributes body: /can.+t be blank/ } + it { is_expected.not_to include 'div.current-service' } + end + end + end