diff --git a/app/controllers/concerns/sortable.rb b/app/controllers/concerns/sortable.rb new file mode 100644 index 00000000..15e0eced --- /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 091bf871..407c5ea1 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(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.dig(: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..20438264 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:, 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 24ea9cbf..e7c75d3c 100644 --- a/app/views/green_lanes/category_assessments/index.html.erb +++ b/app/views/green_lanes/category_assessments/index.html.erb @@ -4,15 +4,31 @@ <%= 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, value: params[: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..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?page=1', 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 } + 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