Skip to content

Commit

Permalink
GL-103: Add Admin UI for creating category assessment data
Browse files Browse the repository at this point in the history
  • Loading branch information
rasikasri committed Apr 11, 2024
1 parent 1f13e07 commit c0082cc
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 1 deletion.
26 changes: 26 additions & 0 deletions app/controllers/green_lanes/category_assessments_controller.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
27 changes: 27 additions & 0 deletions app/models/green_lanes/theme.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions app/views/green_lanes/category_assessments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>

2 changes: 2 additions & 0 deletions app/views/green_lanes/category_assessments/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Manage category assessments
</h2>

<%= link_to 'Add a Category Assessment', new_green_lanes_category_assessment_path, class: 'govuk-button' %>

<% if @category_assessments.any? %>
<table>
<thead>
Expand Down
5 changes: 5 additions & 0 deletions app/views/green_lanes/category_assessments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= govuk_breadcrumbs 'Category Assessments': green_lanes_category_assessments_path %>

<h2>New Category Assessment</h2>

<%= render 'form', category_assessment: @category_assessment %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spec/factories/green_lanes/theme_factory.rb
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions spec/requests/green_lanes/category_assessments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c0082cc

Please sign in to comment.