Skip to content

Commit

Permalink
Merge pull request #440 from etalab/features/instruction-keep-filter-…
Browse files Browse the repository at this point in the history
…on-dashboard

Instruction: save search params on authorization request index
  • Loading branch information
skelz0r authored Sep 23, 2024
2 parents 6a81548 + 747b1f3 commit 35005b7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
19 changes: 15 additions & 4 deletions app/controllers/instruction/authorization_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
class Instruction::AuthorizationRequestsController < Instruction::AbstractAuthorizationRequestsController
before_action :redirect_to_searched_authorization_request, only: [:index]
before_action :save_or_load_search_params, only: [:index]

skip_before_action :extract_authorization_request, only: :index

def index
base_relation = policy_scope([:instruction, AuthorizationRequest]).includes([:organization]).not_archived

base_relation = base_relation.none if search_terms_is_a_possible_id?

@search_engine = base_relation.ransack(params[:q])
@search_engine = base_relation.ransack(params[:search_query])
@search_engine.sorts = 'last_submitted_at desc' if @search_engine.sorts.empty?

@authorization_requests = build_search_engine_results_with_order_which_puts_null_as_last.page(params[:page])
Expand All @@ -24,7 +26,7 @@ def show
def redirect_to_searched_authorization_request
return unless search_terms_is_a_possible_id?

potential_authorization_request = AuthorizationRequest.find_by(id: params[:q][:within_data_or_organization_siret_or_applicant_email_or_applicant_family_name_cont].to_i)
potential_authorization_request = AuthorizationRequest.find_by(id: params[:search_query][:within_data_or_organization_siret_or_applicant_email_or_applicant_family_name_cont].to_i)

return unless potential_authorization_request

Expand All @@ -36,9 +38,9 @@ def redirect_to_searched_authorization_request
end

def search_terms_is_a_possible_id?
return false if params[:q].blank?
return false if params[:search_query].blank?

main_search_input = params[:q][:within_data_or_organization_siret_or_applicant_email_or_applicant_family_name_cont]
main_search_input = params[:search_query][:within_data_or_organization_siret_or_applicant_email_or_applicant_family_name_cont]

return false if main_search_input.blank?

Expand All @@ -52,4 +54,13 @@ def build_search_engine_results_with_order_which_puts_null_as_last
def extract_authorization_request
@authorization_request = AuthorizationRequest.find(params[:id]).decorate
end

def save_or_load_search_params
session[search_key] = params[:search_query] if params[:search_query].present?
params[:search_query] = session[search_key] if params[:search_query].blank?
end

def search_key
:"#{controller_name}_#{action_name}_search"
end
end
3 changes: 3 additions & 0 deletions config/initializers/ransack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Ransack.configure do |c|
c.search_key = :search_query
end
6 changes: 3 additions & 3 deletions spec/features/instruction/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
visit instruction_authorization_requests_path

within('#authorization_request_search') do
fill_in 'q_within_data_or_organization_siret_or_applicant_email_or_applicant_family_name_cont', with: search_text if use_search_text
select state, from: 'q_state_eq' if state
select type, from: 'q_type_eq' if type
fill_in 'search_query_within_data_or_organization_siret_or_applicant_email_or_applicant_family_name_cont', with: search_text if use_search_text
select state, from: 'search_query_state_eq' if state
select type, from: 'search_query_type_eq' if type

click_link_or_button
end
Expand Down

0 comments on commit 35005b7

Please sign in to comment.