diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index c21827198..3e88119ce 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -23,7 +23,7 @@ class OrganisationsController < ApplicationController def schemes all_schemes = Scheme.where(owning_organisation: [@organisation] + @organisation.parent_organisations) - @pagy, @schemes = pagy(filter_manager.filtered_schemes(all_schemes, search_term, session_filters).order_by_completion.order_by_service_name) + @pagy, @schemes = pagy(filter_manager.filtered_schemes(all_schemes, search_term, session_filters).order_by_service_name) @searched = search_term.presence @total_count = all_schemes.size @filter_type = "schemes" diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 2c425bb58..fb8281be8 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -15,7 +15,7 @@ class SchemesController < ApplicationController redirect_to schemes_organisation_path(current_user.organisation) unless current_user.support? all_schemes = Scheme.all - @pagy, @schemes = pagy(filter_manager.filtered_schemes(all_schemes, search_term, session_filters).order_by_completion.order_by_service_name) + @pagy, @schemes = pagy(filter_manager.filtered_schemes(all_schemes, search_term, session_filters).order_by_service_name) @searched = search_term.presence @total_count = all_schemes.size @filter_type = "schemes" diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 55d15dfb2..b5faee044 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -11,14 +11,17 @@ class Scheme < ApplicationRecord 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, lambda { |param| - search_by_postcode(param) - .or(search_by_service_name(param)) - .or(search_by_location_name(param)) - .or(filter_by_id(param)).distinct + select("schemes.*, lower(service_name) as lowercase_service_name") + .search_by_postcode(param) + .or(search_by_service_name(param)) + .or(search_by_location_name(param)) + .or(filter_by_id(param)).distinct } - scope :order_by_completion, -> { order("schemes.confirmed ASC NULLS FIRST") } - scope :order_by_service_name, -> { order(service_name: :asc) } + scope :order_by_service_name, lambda { + select("schemes.*, lower(service_name) as lowercase_service_name") + .order("lowercase_service_name ASC") + } scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :filter_by_status, lambda { |statuses, _user = nil| filtered_records = all