Skip to content

Commit

Permalink
Add phase filter to placements search
Browse files Browse the repository at this point in the history
  • Loading branch information
dp-daly committed Nov 28, 2024
1 parent 39886f1 commit d1d9ebf
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/controllers/placements/providers/placements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -78,6 +80,7 @@ def filter_params
school_ids: [],
subject_ids: [],
term_ids: [],
phases: [],
establishment_groups: [],
year_groups: [],
)
Expand All @@ -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
10 changes: 10 additions & 0 deletions app/forms/placements/placements/filter_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,6 +51,7 @@ def query_params
placements_to_show:,
academic_year_id:,
term_ids:,
phases:,
search_location:,
}
end
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions app/queries/placements/placements_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?

Expand Down
51 changes: 51 additions & 0 deletions app/views/placements/providers/placements/_filter.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@
</ul>
<% end %>

<% if filter_form.primary_selected? || filter_form.secondary_selected? %>
<h3 class="govuk-heading-s govuk-!-margin-bottom-0"><%= t(".phase") %></h3>
<ul class="app-filter-tags">
<% if filter_form.primary_selected? %>
<li>
<%= govuk_link_to(
t(".primary"),
filter_form.index_path_without_filter(
filter: "phases",
value: "primary",
),
class: "app-filter__tag",
no_visited_state: true,
) %>
</li>
<% end %>

<% if filter_form.secondary_selected? %>
<li>
<%= govuk_link_to(
t(".secondary"),
filter_form.index_path_without_filter(
filter: "phases",
value: "secondary",
),
class: "app-filter__tag",
no_visited_state: true,
) %>
</li>
<% end %>
</ul>
<% end %>

<% if filter_form.subject_ids.present? %>
<h3 class="govuk-heading-s govuk-!-margin-bottom-0"><%= t(".subject") %></h3>
<ul class="app-filter-tags">
Expand Down Expand Up @@ -220,6 +253,22 @@
<% end %>
</div>

<div class="app-filter__option">
<%= form.govuk_check_boxes_fieldset(
:phases,
legend: { text: t(".phase"), size: "s" },
small: true,
multiple: true,
) do %>
<%= form.govuk_check_box :phases,
"primary",
label: { text: t(".primary") } %>
<%= form.govuk_check_box :phases,
"secondary",
label: { text: t(".secondary") } %>
<% end %>
</div>

<div class="app-filter__option" data-controller="filter-search">
<%= form.govuk_check_boxes_fieldset(
:subject_ids,
Expand All @@ -237,6 +286,7 @@
<% end %>
</div>

<% if filter_form.primary_selected? || filter_form.phases.blank? %>
<div class="app-filter__option">
<%= form.govuk_check_boxes_fieldset(
:year_groups,
Expand All @@ -250,6 +300,7 @@
<% end %>
<% end %>
</div>
<% end %>

<div class="app-filter__option">
<%= form.govuk_check_boxes_fieldset(
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en/placements/providers/placements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ en:
location_search_prompt: Enter a town, city or postcode
partner_schools: Schools I work with
terms: Expected date
phase: Phase
primary: Primary
secondary: Secondary
only_show_partner_schools: Only show placements from schools I work with
school: School
search_location: Location
Expand Down
57 changes: 57 additions & 0 deletions spec/forms/placements/placements/filter_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
end
end

context "when given phase params" do
let(:params) { { phases: %w[primary secondary] } }

it "returns true" do
expect(filter_form).to be(true)
end
end

context "when given year group params" do
let(:params) { { year_groups: %w[year_group] } }

Expand Down Expand Up @@ -172,6 +180,30 @@
end
end

context "when removing phase params" do
let(:params) do
{ phases: %w[primary secondary] }
end

it "returns the placements index page path without the given phase param" do
expect(
filter_form.index_path_without_filter(
filter: "phases",
value: "primary",
),
).to eq(
placements_provider_placements_path(
provider,
filters: {
placements_to_show: "available_placements",
academic_year_id: current_academic_year.id,
phases: %w[secondary],
},
),
)
end
end

context "when removing year group params" do
let(:params) do
{ year_groups: %w[year_group_1 year_group_2] }
Expand Down Expand Up @@ -208,6 +240,7 @@
search_location: nil,
subject_ids: [],
term_ids: [],
phases: [],
year_groups: [],
},
)
Expand Down Expand Up @@ -243,4 +276,28 @@
).to match_array(terms)
end
end

describe "#primary_selected?" do
subject(:filter_form) { described_class.new(provider, params).primary_selected? }

context "when primary is included in the phases param" do
let(:params) { { phases: %w[primary] } }

it "returns true" do
expect(filter_form).to be(true)
end
end
end

describe "#secondary_selected?" do
subject(:filter_form) { described_class.new(provider, params).secondary_selected? }

context "when secondary is included in the phases param" do
let(:params) { { phases: %w[secondary] } }

it "returns true" do
expect(filter_form).to be(true)
end
end
end
end
Loading

0 comments on commit d1d9ebf

Please sign in to comment.