Browse Source

Refactor and udpate scopes

CLDC-2640-location-guidance
Kat 1 year ago
parent
commit
4547fb2de4
  1. 12
      app/models/scheme.rb
  2. 22
      spec/requests/organisations_controller_spec.rb
  3. 24
      spec/requests/schemes_controller_spec.rb

12
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 :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_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_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) { left_joins(:locations).where("locations.name ILIKE ?", "%#{name}%") } 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| 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_service_name(param))
.or(search_by_location_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 { scope :order_by_service_name, lambda {
select("schemes.*, lower(service_name) as lowercase_service_name") order("lower(service_name) ASC")
.order("lowercase_service_name ASC")
} }
scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) }
scope :filter_by_status, lambda { |statuses, _user = nil| scope :filter_by_status, lambda { |statuses, _user = nil|

22
spec/requests/organisations_controller_spec.rb

@ -123,15 +123,21 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
it "shows incomplete schemes at the top" do it "shows schemes in alpabetical order" do
schemes[0].update!(confirmed: nil, owning_organisation: user.organisation) schemes[0].update!(service_name: "aaa", owning_organisation: user.organisation)
schemes[2].update!(confirmed: false, owning_organisation: user.organisation) schemes[1].update!(service_name: "daa", owning_organisation: user.organisation)
schemes[4].update!(confirmed: false, 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: {} get "/organisations/#{organisation.id}/schemes", headers:, params: {}
all_links = page.all(".govuk-link")
expect(page.all(".govuk-tag")[1].text).to eq("Incomplete") scheme_links = all_links.select { |link| link[:href] =~ %r{^/schemes/\d+$} }
expect(page.all(".govuk-tag")[2].text).to eq("Incomplete")
expect(page.all(".govuk-tag")[3].text).to eq("Incomplete") 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 end
context "with schemes that are not in scope for the user, i.e. that they do not belong to" do context "with schemes that are not in scope for the user, i.e. that they do not belong to" do

24
spec/requests/schemes_controller_spec.rb

@ -201,15 +201,21 @@ RSpec.describe SchemesController, type: :request do
assert_select ".govuk-tag", text: /Incomplete/, count: 1 assert_select ".govuk-tag", text: /Incomplete/, count: 1
end end
it "shows incomplete schemes at the top" do it "shows schemes in alpabetical order" do
schemes[0].update!(confirmed: nil) schemes[0].update!(service_name: "aaa")
schemes[2].update!(confirmed: false) schemes[1].update!(service_name: "daa")
schemes[4].update!(confirmed: false) schemes[2].update!(service_name: "baa")
get "/schemes" schemes[3].update!(service_name: "Faa")
schemes[4].update!(service_name: "Caa")
expect(page.all(".govuk-tag")[1].text).to eq("Incomplete") get "/schemes", headers:, params: {}
expect(page.all(".govuk-tag")[2].text).to eq("Incomplete") all_links = page.all(".govuk-link")
expect(page.all(".govuk-tag")[3].text).to eq("Incomplete") 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 end
it "displays a link to check answers page if the scheme is incomplete" do it "displays a link to check answers page if the scheme is incomplete" do

Loading…
Cancel
Save