From a3e72a2f8d10c965c3fe3e6d489938917d02c6ed Mon Sep 17 00:00:00 2001 From: "rasika.abeyrathna" Date: Fri, 27 Sep 2024 15:48:43 +0100 Subject: [PATCH 1/4] GL-1067: Implement Search Functionality for Exemptions on Category Assessments Page --- .../category_assessments_controller.rb | 2 +- .../category_assessments/index.html.erb | 16 ++++++++++++++++ .../category_assessments_controller_spec.rb | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/green_lanes/category_assessments_controller.rb b/app/controllers/green_lanes/category_assessments_controller.rb index 091bf871..0cae517f 100644 --- a/app/controllers/green_lanes/category_assessments_controller.rb +++ b/app/controllers/green_lanes/category_assessments_controller.rb @@ -5,7 +5,7 @@ class CategoryAssessmentsController < AuthenticatedController before_action :disable_service_switching! before_action :check_service def index - @category_assessments = GreenLanes::CategoryAssessment.all(page: current_page).fetch + @category_assessments = GreenLanes::CategoryAssessment.all(query: { exemption_code: params[:exemption_code], page: current_page}).fetch end def new diff --git a/app/views/green_lanes/category_assessments/index.html.erb b/app/views/green_lanes/category_assessments/index.html.erb index 24ea9cbf..931671df 100644 --- a/app/views/green_lanes/category_assessments/index.html.erb +++ b/app/views/green_lanes/category_assessments/index.html.erb @@ -4,6 +4,22 @@ <%= link_to 'Add a Category Assessment', new_green_lanes_category_assessment_path, class: 'govuk-button' %> +<%= form_with url: green_lanes_category_assessments_path, method: :get, class: "govuk-form-group" do |form| %> +
+ + Search Category Assessments + +
+ <%= form.label :exemption_code, "Green Lanes Exemption Code", class: "govuk-label" %> + <%= form.text_field :exemption_code, class: "govuk-input govuk-!-width-one-third", id: "search-term", width: 'one-third'%> +
+ +
+ <%= form.submit "Search", class: "govuk-button" %> +
+
+<% end %> + <% if @category_assessments.any? %> diff --git a/spec/requests/green_lanes/category_assessments_controller_spec.rb b/spec/requests/green_lanes/category_assessments_controller_spec.rb index 3e1399c9..74dd878c 100644 --- a/spec/requests/green_lanes/category_assessments_controller_spec.rb +++ b/spec/requests/green_lanes/category_assessments_controller_spec.rb @@ -14,7 +14,7 @@ describe 'GET #index' do before do - stub_api_request('/admin/green_lanes/category_assessments?page=1', backend: 'xi').and_return \ + stub_api_request('/admin/green_lanes/category_assessments?query[exemption_code]&query[page]=1', backend: 'xi').and_return \ jsonapi_response :category_assessments, attributes_for_list(:category_assessment, 3, :with_theme) end From 0926cf826a2c2b639ed196671b688c5b73f9f51e Mon Sep 17 00:00:00 2001 From: "rasika.abeyrathna" Date: Fri, 27 Sep 2024 16:07:57 +0100 Subject: [PATCH 2/4] GL-1067: Implement Search Functionality for Exemptions on Category Assessments Page --- app/controllers/green_lanes/category_assessments_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/green_lanes/category_assessments_controller.rb b/app/controllers/green_lanes/category_assessments_controller.rb index 0cae517f..8739ef67 100644 --- a/app/controllers/green_lanes/category_assessments_controller.rb +++ b/app/controllers/green_lanes/category_assessments_controller.rb @@ -5,7 +5,7 @@ class CategoryAssessmentsController < AuthenticatedController before_action :disable_service_switching! before_action :check_service def index - @category_assessments = GreenLanes::CategoryAssessment.all(query: { exemption_code: params[:exemption_code], page: current_page}).fetch + @category_assessments = GreenLanes::CategoryAssessment.all(query: { exemption_code: params[:exemption_code], page: current_page }).fetch end def new From 0e9002d92cbd430a772565acebcf65779707b905 Mon Sep 17 00:00:00 2001 From: "rasika.abeyrathna" Date: Fri, 4 Oct 2024 17:27:34 +0100 Subject: [PATCH 3/4] GL-1065: Implement Sorting Functionality on Category Assessments Page --- app/controllers/concerns/sortable.rb | 17 ++++++++++++ .../category_assessments_controller.rb | 26 ++++++++++++++++++- app/helpers/application_helper.rb | 6 +++++ .../category_assessments/index.html.erb | 10 +++---- .../category_assessments_controller_spec.rb | 4 +-- 5 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 app/controllers/concerns/sortable.rb diff --git a/app/controllers/concerns/sortable.rb b/app/controllers/concerns/sortable.rb new file mode 100644 index 00000000..1ac2198a --- /dev/null +++ b/app/controllers/concerns/sortable.rb @@ -0,0 +1,17 @@ +module Sortable + extend ActiveSupport::Concern + + included do + helper_method :sort_column, :sort_direction + end + + private + + def sort_column + params[:sort] + end + + def sort_direction + %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" + end +end diff --git a/app/controllers/green_lanes/category_assessments_controller.rb b/app/controllers/green_lanes/category_assessments_controller.rb index 8739ef67..5dd9e9c4 100644 --- a/app/controllers/green_lanes/category_assessments_controller.rb +++ b/app/controllers/green_lanes/category_assessments_controller.rb @@ -1,11 +1,13 @@ module GreenLanes class CategoryAssessmentsController < AuthenticatedController + include Sortable include XiOnly before_action :disable_service_switching! before_action :check_service def index - @category_assessments = GreenLanes::CategoryAssessment.all(query: { exemption_code: params[:exemption_code], page: current_page }).fetch + merge_filters + @category_assessments = GreenLanes::CategoryAssessment.all(search_params).fetch end def new @@ -119,6 +121,28 @@ def prepare_edit @measure = GreenLanes::Measure.new(category_assessment_id: @category_assessment.id) end + def search_params + { + query: + { + filters: params[:filters].to_h, + page: current_page, + sort: params[:sort], + direction: params[:direction] + } + } + end + + def merge_filters + filters = params.fetch(:filters, {}).permit(:exemption_code).to_h + if params[:exemption_code].present? + filters.merge!(exemption_code: params[:exemption_code]) + else + params[:exemption_code] = params[:filters][:exemption_code] + end + params[:filters] = ActionController::Parameters.new(filters).permit(:exemption_code) + end + def ca_params params.require(:category_assessment).permit( :regulation_id, diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 211a74a0..09bd2568 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,4 +3,10 @@ def active_nav_link?(activator) (activator.is_a?(String) && request.path.start_with?(activator)) || (activator.is_a?(Regexp) && request.path =~ activator) end + + def sortable(column, title = nil) + title ||= column.titleize + direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc" + link_to title, { sort: column, direction: direction, page: params[:page], filters: params[:filters] } + end end diff --git a/app/views/green_lanes/category_assessments/index.html.erb b/app/views/green_lanes/category_assessments/index.html.erb index 931671df..e7c75d3c 100644 --- a/app/views/green_lanes/category_assessments/index.html.erb +++ b/app/views/green_lanes/category_assessments/index.html.erb @@ -11,7 +11,7 @@
<%= form.label :exemption_code, "Green Lanes Exemption Code", class: "govuk-label" %> - <%= form.text_field :exemption_code, class: "govuk-input govuk-!-width-one-third", id: "search-term", width: 'one-third'%> + <%= form.text_field :exemption_code, value: params[:exemption_code], class: "govuk-input govuk-!-width-one-third", id: "search-term", width: 'one-third'%>
@@ -25,10 +25,10 @@
- - - - + + + + diff --git a/spec/requests/green_lanes/category_assessments_controller_spec.rb b/spec/requests/green_lanes/category_assessments_controller_spec.rb index 74dd878c..157126d6 100644 --- a/spec/requests/green_lanes/category_assessments_controller_spec.rb +++ b/spec/requests/green_lanes/category_assessments_controller_spec.rb @@ -14,11 +14,11 @@ describe 'GET #index' do before do - stub_api_request('/admin/green_lanes/category_assessments?query[exemption_code]&query[page]=1', backend: 'xi').and_return \ + stub_api_request('/admin/green_lanes/category_assessments?query[filters]&query[page]=1&query[sort]&query[direction]', backend: 'xi').and_return \ jsonapi_response :category_assessments, attributes_for_list(:category_assessment, 3, :with_theme) end - let(:make_request) { get green_lanes_category_assessments_path } + let(:make_request) { get green_lanes_category_assessments_path, params: {filters: {}} } it { is_expected.to have_http_status :success } it { is_expected.not_to include 'div.current-service' } From 6b48c07ad284609ebdfc5d290f5805f68d1032d3 Mon Sep 17 00:00:00 2001 From: "rasika.abeyrathna" Date: Mon, 7 Oct 2024 14:50:38 +0100 Subject: [PATCH 4/4] GL-1065: Implement Sorting Functionality on Category Assessments Page --- app/controllers/concerns/sortable.rb | 2 +- .../green_lanes/category_assessments_controller.rb | 6 +++--- app/helpers/application_helper.rb | 4 ++-- .../green_lanes/category_assessments_controller_spec.rb | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/concerns/sortable.rb b/app/controllers/concerns/sortable.rb index 1ac2198a..15e0eced 100644 --- a/app/controllers/concerns/sortable.rb +++ b/app/controllers/concerns/sortable.rb @@ -12,6 +12,6 @@ def sort_column end def sort_direction - %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" + %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc' end end diff --git a/app/controllers/green_lanes/category_assessments_controller.rb b/app/controllers/green_lanes/category_assessments_controller.rb index 5dd9e9c4..407c5ea1 100644 --- a/app/controllers/green_lanes/category_assessments_controller.rb +++ b/app/controllers/green_lanes/category_assessments_controller.rb @@ -128,8 +128,8 @@ def search_params filters: params[:filters].to_h, page: current_page, sort: params[:sort], - direction: params[:direction] - } + direction: params[:direction], + }, } end @@ -138,7 +138,7 @@ def merge_filters if params[:exemption_code].present? filters.merge!(exemption_code: params[:exemption_code]) else - params[:exemption_code] = params[:filters][:exemption_code] + params[:exemption_code] = params.dig(:filters, :exemption_code) end params[:filters] = ActionController::Parameters.new(filters).permit(:exemption_code) end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 09bd2568..20438264 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -6,7 +6,7 @@ def active_nav_link?(activator) def sortable(column, title = nil) title ||= column.titleize - direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc" - link_to title, { sort: column, direction: direction, page: params[:page], filters: params[:filters] } + direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc' + link_to title, { sort: column, direction:, page: params[:page], filters: params[:filters] } 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 157126d6..c2d2511c 100644 --- a/spec/requests/green_lanes/category_assessments_controller_spec.rb +++ b/spec/requests/green_lanes/category_assessments_controller_spec.rb @@ -14,11 +14,11 @@ describe 'GET #index' do before do - stub_api_request('/admin/green_lanes/category_assessments?query[filters]&query[page]=1&query[sort]&query[direction]', backend: 'xi').and_return \ + stub_api_request('/admin/green_lanes/category_assessments?query[page]=1&query[sort]&query[direction]', backend: 'xi').and_return \ jsonapi_response :category_assessments, attributes_for_list(:category_assessment, 3, :with_theme) end - let(:make_request) { get green_lanes_category_assessments_path, params: {filters: {}} } + let(:make_request) { get green_lanes_category_assessments_path, params: { filters: {} } } it { is_expected.to have_http_status :success } it { is_expected.not_to include 'div.current-service' }
IDMeasure type idRegulation idRegulation roleTheme<%= sortable "measure_type_id", "Measure type id" %><%= sortable "regulation_id", "Regulation id" %><%= sortable "regulation_role", "Regulation role" %><%= sortable "theme_id", "Theme" %> Action