Skip to content

Commit

Permalink
Merge pull request #776 from trade-tariff/GL-1067
Browse files Browse the repository at this point in the history
GL-1067: Implement Search Functionality for Exemptions on Category As…
  • Loading branch information
rasikasri authored Oct 18, 2024
2 parents c99172f + 9b33aa8 commit 50b6e5b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
17 changes: 17 additions & 0 deletions app/controllers/concerns/sortable.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 25 additions & 1 deletion app/controllers/green_lanes/category_assessments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 20 additions & 4 deletions app/views/green_lanes/category_assessments/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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| %>
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--m">
Search Category Assessments
</legend>
<div class="govuk-form-group">
<%= 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'%>
</div>

<div class="govuk-form-group">
<%= form.submit "Search", class: "govuk-button" %>
</div>
</fieldset>
<% end %>

<% if @category_assessments.any? %>
<table>
<thead>
<tr>
<th>ID</th>
<th>Measure type id</th>
<th>Regulation id</th>
<th>Regulation role</th>
<th>Theme</th>
<th><%= sortable "measure_type_id", "Measure type id" %></th>
<th><%= sortable "regulation_id", "Regulation id" %></th>
<th><%= sortable "regulation_role", "Regulation role" %></th>
<th><%= sortable "theme_id", "Theme" %></th>
<th>Action</th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down

0 comments on commit 50b6e5b

Please sign in to comment.