From 3a38b25c36c4e800a5d0e33300134e3e088a91d9 Mon Sep 17 00:00:00 2001 From: dp-daly Date: Wed, 27 Nov 2024 16:45:27 +0000 Subject: [PATCH] Add phase filter to placements search --- .../providers/placements_controller.rb | 19 ++ .../placements/placements/filter_form.rb | 10 + app/queries/placements/placements_query.rb | 11 + .../providers/placements/_filter.html.erb | 51 +++++ .../en/placements/providers/placements.yml | 3 + .../placements/placements/filter_form_spec.rb | 57 +++++ ...r_user_filters_placements_by_phase_spec.rb | 210 ++++++++++++++++++ 7 files changed, 361 insertions(+) create mode 100644 spec/system/placements/providers/placements/provider_user_filters_placements_by_phase_spec.rb diff --git a/app/controllers/placements/providers/placements_controller.rb b/app/controllers/placements/providers/placements_controller.rb index 1a30424086..05d4e2d46b 100644 --- a/app/controllers/placements/providers/placements_controller.rb +++ b/app/controllers/placements/providers/placements_controller.rb @@ -14,6 +14,8 @@ def index @pagy, @placements = pagy(placements.merge(query)) @placements = @placements.decorate + filter_subjects_by_phase + filter_schools_by_phase calculate_travel_time end @@ -78,6 +80,7 @@ def filter_params school_ids: [], subject_ids: [], term_ids: [], + phases: [], establishment_groups: [], year_groups: [], ) @@ -99,4 +102,20 @@ def compact_school_attribute_values(attribute) all_schools.where.not(attribute => nil) .distinct(attribute).order(attribute).pluck(attribute) end + + def filter_subjects_by_phase + if filter_form.primary_selected? && !filter_form.secondary_selected? + @subjects = @subjects.where(subject_area: "primary") + elsif !filter_form.primary_selected? && filter_form.secondary_selected? + @subjects = @subjects.where(subject_area: "secondary") + end + end + + def filter_schools_by_phase + if filter_form.primary_selected? && !filter_form.secondary_selected? + @schools = @schools.where.not(phase: "Secondary") + elsif !filter_form.primary_selected? && filter_form.secondary_selected? + @schools = @schools.where.not(phase: "Primary") + end + end end diff --git a/app/forms/placements/placements/filter_form.rb b/app/forms/placements/placements/filter_form.rb index 028d756b93..dd65cb6c31 100644 --- a/app/forms/placements/placements/filter_form.rb +++ b/app/forms/placements/placements/filter_form.rb @@ -6,6 +6,7 @@ class Placements::Placements::FilterForm < ApplicationForm attribute :search_location, default: nil attribute :year_groups, default: [] attribute :term_ids, default: [] + attribute :phases, default: [] attribute :placements_to_show, default: "available_placements" attribute :academic_year_id, default: Placements::AcademicYear.current.id attribute :only_partner_schools, :boolean, default: false @@ -50,6 +51,7 @@ def query_params placements_to_show:, academic_year_id:, term_ids:, + phases:, search_location:, } end @@ -66,6 +68,14 @@ def terms @terms ||= Placements::Term.where(id: term_ids).order_by_term end + def primary_selected? + phases.include?("primary") + end + + def secondary_selected? + phases.include?("secondary") + end + private SINGULAR_ATTRIBUTES = %w[only_partner_schools placements_to_show search_location].freeze diff --git a/app/queries/placements/placements_query.rb b/app/queries/placements/placements_query.rb index 7569bdc1e2..d4847cb145 100644 --- a/app/queries/placements/placements_query.rb +++ b/app/queries/placements/placements_query.rb @@ -11,6 +11,7 @@ def call scope = academic_year_condition(scope) scope = term_condition(scope) scope = year_group_condition(scope) + scope = phase_condition(scope) order_condition(scope) end @@ -59,6 +60,16 @@ def term_condition(scope) .or(scope.where.missing(:terms)) end + def phase_condition(scope) + return scope if filter_params[:phases].blank? + + if filter_params[:phases].include?("primary") && filter_params[:phases].include?("secondary") + scope.joins(:subject) + else + scope.joins(:subject).where(subjects: { subject_area: filter_params[:phases] }) + end + end + def year_group_condition(scope) return scope if filter_params[:year_groups].blank? diff --git a/app/views/placements/providers/placements/_filter.html.erb b/app/views/placements/providers/placements/_filter.html.erb index 516f9cd510..6beec9588b 100644 --- a/app/views/placements/providers/placements/_filter.html.erb +++ b/app/views/placements/providers/placements/_filter.html.erb @@ -62,6 +62,39 @@ <% end %> + <% if filter_form.primary_selected? || filter_form.secondary_selected? %> +

<%= t(".phase") %>

+ + <% end %> + <% if filter_form.subject_ids.present? %>

<%= t(".subject") %>