From 4547fb2de4cb19c5b8058eb6abd141cfd8c37f5e Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 12 Oct 2023 11:13:43 +0100 Subject: [PATCH] Refactor and udpate scopes --- app/models/scheme.rb | 12 ++++------ .../requests/organisations_controller_spec.rb | 22 ++++++++++------- spec/requests/schemes_controller_spec.rb | 24 ++++++++++++------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index b5faee044..2bda18b02 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -8,19 +8,17 @@ class Scheme < ApplicationRecord scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } - scope :search_by_postcode, ->(postcode) { left_joins(:locations).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } - scope :search_by_location_name, ->(name) { left_joins(:locations).where("locations.name ILIKE ?", "%#{name}%") } + scope :search_by_postcode, ->(postcode) { where("schemes.id IN (SELECT DISTINCT scheme_id FROM locations WHERE REPLACE(locations.postcode, ' ', '') ILIKE ?)", "%#{postcode.delete(' ')}%") } + scope :search_by_location_name, ->(name) { where("schemes.id IN (SELECT DISTINCT scheme_id FROM locations WHERE locations.name ILIKE ?)", "%#{name}%") } scope :search_by, lambda { |param| - select("schemes.*, lower(service_name) as lowercase_service_name") - .search_by_postcode(param) + search_by_postcode(param) .or(search_by_service_name(param)) .or(search_by_location_name(param)) - .or(filter_by_id(param)).distinct + .or(filter_by_id(param)) } scope :order_by_service_name, lambda { - select("schemes.*, lower(service_name) as lowercase_service_name") - .order("lowercase_service_name ASC") + order("lower(service_name) ASC") } scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :filter_by_status, lambda { |statuses, _user = nil| diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index bacb5ca33..8cdf19913 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -123,15 +123,21 @@ RSpec.describe OrganisationsController, type: :request do end end - it "shows incomplete schemes at the top" do - schemes[0].update!(confirmed: nil, owning_organisation: user.organisation) - schemes[2].update!(confirmed: false, owning_organisation: user.organisation) - schemes[4].update!(confirmed: false, owning_organisation: user.organisation) + it "shows schemes in alpabetical order" do + schemes[0].update!(service_name: "aaa", owning_organisation: user.organisation) + schemes[1].update!(service_name: "daa", owning_organisation: user.organisation) + schemes[2].update!(service_name: "baa", owning_organisation: user.organisation) + schemes[3].update!(service_name: "Faa", owning_organisation: user.organisation) + schemes[4].update!(service_name: "Caa", owning_organisation: user.organisation) get "/organisations/#{organisation.id}/schemes", headers:, params: {} - - expect(page.all(".govuk-tag")[1].text).to eq("Incomplete") - expect(page.all(".govuk-tag")[2].text).to eq("Incomplete") - expect(page.all(".govuk-tag")[3].text).to eq("Incomplete") + all_links = page.all(".govuk-link") + scheme_links = all_links.select { |link| link[:href] =~ %r{^/schemes/\d+$} } + + expect(scheme_links[0][:href]).to eq("/schemes/#{schemes[0].id}") + expect(scheme_links[1][:href]).to eq("/schemes/#{schemes[2].id}") + expect(scheme_links[2][:href]).to eq("/schemes/#{schemes[4].id}") + expect(scheme_links[3][:href]).to eq("/schemes/#{schemes[1].id}") + expect(scheme_links[4][:href]).to eq("/schemes/#{schemes[3].id}") end context "with schemes that are not in scope for the user, i.e. that they do not belong to" do diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 50d83a5b3..39710df94 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -201,15 +201,21 @@ RSpec.describe SchemesController, type: :request do assert_select ".govuk-tag", text: /Incomplete/, count: 1 end - it "shows incomplete schemes at the top" do - schemes[0].update!(confirmed: nil) - schemes[2].update!(confirmed: false) - schemes[4].update!(confirmed: false) - get "/schemes" - - expect(page.all(".govuk-tag")[1].text).to eq("Incomplete") - expect(page.all(".govuk-tag")[2].text).to eq("Incomplete") - expect(page.all(".govuk-tag")[3].text).to eq("Incomplete") + it "shows schemes in alpabetical order" do + schemes[0].update!(service_name: "aaa") + schemes[1].update!(service_name: "daa") + schemes[2].update!(service_name: "baa") + schemes[3].update!(service_name: "Faa") + schemes[4].update!(service_name: "Caa") + get "/schemes", headers:, params: {} + all_links = page.all(".govuk-link") + scheme_links = all_links.select { |link| link[:href] =~ %r{^/schemes/\d+$} } + + expect(scheme_links[0][:href]).to eq("/schemes/#{schemes[0].id}") + expect(scheme_links[1][:href]).to eq("/schemes/#{schemes[2].id}") + expect(scheme_links[2][:href]).to eq("/schemes/#{schemes[4].id}") + expect(scheme_links[3][:href]).to eq("/schemes/#{schemes[1].id}") + expect(scheme_links[4][:href]).to eq("/schemes/#{schemes[3].id}") end it "displays a link to check answers page if the scheme is incomplete" do