Skip to content

Commit

Permalink
FE journey school search only returns FE bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
asmega committed Jun 28, 2024
1 parent d25ea4a commit b2b5b4a
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/assets/javascripts/components/college_search.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ document.addEventListener("DOMContentLoaded", function () {
return;
}

var feOnly = form.dataset.feOnly || false;

var searchInputContainer = form.querySelector("#autocomplete-container");

if (!searchInputContainer) {
Expand Down Expand Up @@ -57,7 +59,7 @@ document.addEventListener("DOMContentLoaded", function () {
Rails.ajax({
type: "post",
url: "<%= Rails.application.routes.url_helpers.school_search_index_path %>",
data: "query=" + query,
data: "query=" + query + "&fe_only=" + feOnly.toString(),
success: handleResponse,
error: handleResponse
});
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/school_search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ def search_schools
return
end

fe_only = ActiveModel::Type::Boolean.new.cast(params[:fe_only])
schools = ActiveModel::Type::Boolean.new.cast(params[:exclude_closed]) ? School.open : School

begin
@schools = schools.search(params[:query])
@schools = schools.search(params[:query], fe_only:)
rescue ArgumentError => e
raise unless e.message == School::SEARCH_NOT_ENOUGH_CHARACTERS_ERROR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def save

def results
@results ||= if journey_session.answers.school_id.present?
School.open.where(id: school_id)
School.open.fe_only.where(id: school_id)
else
School.open.search(provision_search)
School.open.fe_only.search(provision_search)
end
end

Expand Down
10 changes: 8 additions & 2 deletions app/models/school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class School < ApplicationRecord
validates :school_type_group, presence: true
validates :school_type, presence: true

scope :fe_only, -> { where(phase: 6) } # PhaseOfEducation == 16 plus

PHASES = {
not_applicable: 0,
nursery: 1,
Expand Down Expand Up @@ -119,7 +121,7 @@ class School < ApplicationRecord

before_save :sanitise_postcode_search_index

def self.search(search_term)
def self.search(search_term, fe_only: false)
raise ArgumentError, SEARCH_NOT_ENOUGH_CHARACTERS_ERROR if search_term.length < SEARCH_MINIMUM_LENGTH

search_field = :name
Expand All @@ -131,10 +133,14 @@ def self.search(search_term)
search_field, search_term = [:postcode_sanitised, sanitised_search_term]
end

where("#{search_field} ILIKE ?", "%#{sanitize_sql_like(search_term)}%")
sql = where("#{search_field} ILIKE ?", "%#{sanitize_sql_like(search_term)}%")
.order(sanitize_sql_for_order([Arel.sql("similarity(#{search_field}, ?) DESC"), search_term]))
.order(:name, close_date: :desc)
.limit(SEARCH_RESULTS_LIMIT)

sql = sql.fe_only if fe_only

sql
end

def address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
method: :patch,
builder: GOVUKDesignSystemFormBuilder::FormBuilder,
id: "further-education-provision-search-form",
data: { "fe-only" => true },
html: { novalidate: false } do |f| %>
<%= f.govuk_error_summary %>

Expand Down
4 changes: 4 additions & 0 deletions spec/factories/schools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,9 @@
open_date { 10.days.ago }
close_date { nil }
end

trait :further_education do
phase { 6 }
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.feature "Further education payments", js: true, flaky: true do
let(:college) { create(:school) }
let(:college) { create(:school, :further_education) }

scenario "happy js path" do
when_further_education_payments_journey_configuration_exists
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.feature "Further education payments" do
let(:college) { create(:school) }
let(:college) { create(:school, :further_education) }

scenario "happy path claim" do
when_further_education_payments_journey_configuration_exists
Expand Down
8 changes: 7 additions & 1 deletion spec/models/school_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
describe ".search" do
let!(:first_school) { create(:school, name: "Community School London", postcode: "SW1P 3BT") }
let!(:second_school) { create(:school, name: "Unity School London", postcode: "SW1P 3BT") }
let!(:third_school) { create(:school, name: "The Unity College Manchester", postcode: "M1 2WD") }
let!(:third_school) { create(:school, :further_education, name: "The Unity College Manchester", postcode: "M1 2WD") }

it "returns schools with a name matching the search term" do
expect(School.search("School")).to match_array([first_school, second_school])
Expand All @@ -38,6 +38,12 @@
it "orders the results by similarity" do
expect(School.search("Unity School")).to eq([second_school, first_school])
end

context "when searching for FE only" do
it "only returns FE bodies" do
expect(School.search("Unity", fe_only: true)).to eq([third_school])
end
end
end

describe "#address" do
Expand Down
16 changes: 16 additions & 0 deletions spec/requests/school_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,21 @@
expect(response.body).to include(other_school_similar_name.name)
end
end

context "when searching for FE only" do
let!(:school_3) { create(:school, :further_education, name: "some college") }

before do
school_1.update(name: "some school")
school_2.update(name: "some school")
end

it "only returns FE bodies" do
post school_search_index_path, params: {query: "some", fe_only: true}

expect(JSON.parse(response.body)["data"].size).to eql(1)
expect(response.body).to include("some college")
end
end
end
end

0 comments on commit b2b5b4a

Please sign in to comment.