From c4af7c456d8a215ec763486bd20d2beea5ced2cf Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 2 Dec 2022 15:44:23 +0000 Subject: [PATCH] Extract scopes and remove wrong test --- app/controllers/organisations_controller.rb | 2 +- app/controllers/schemes_controller.rb | 2 +- app/models/scheme.rb | 3 +++ spec/models/scheme_spec.rb | 18 ------------------ 4 files changed, 5 insertions(+), 20 deletions(-) diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index c1f5075b7..0121b5e34 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -19,7 +19,7 @@ class OrganisationsController < ApplicationController end def schemes - all_schemes = Scheme.where(owning_organisation: @organisation).order("confirmed ASC NULLS FIRST", service_name: :asc) + all_schemes = Scheme.where(owning_organisation: @organisation).order_by_completion.order_by_service_name @pagy, @schemes = pagy(filtered_collection(all_schemes, search_term)) @searched = search_term.presence diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 5bc50038a..056ce5c4d 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -9,7 +9,7 @@ class SchemesController < ApplicationController def index redirect_to schemes_organisation_path(current_user.organisation) unless current_user.support? - all_schemes = Scheme.order("confirmed ASC NULLS FIRST", service_name: :asc) + all_schemes = Scheme.order_by_completion.order_by_service_name @pagy, @schemes = pagy(filtered_collection(all_schemes, search_term)) @searched = search_term.presence diff --git a/app/models/scheme.rb b/app/models/scheme.rb index bc243d449..67b577ed2 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -18,6 +18,9 @@ class Scheme < ApplicationRecord .or(filter_by_id(param)).distinct } + scope :order_by_completion, -> { order("confirmed ASC NULLS FIRST") } + scope :order_by_service_name, -> { order(service_name: :asc) } + validate :validate_confirmed auto_strip_attributes :service_name diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index 2a71d1047..2f65ba442 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -176,24 +176,6 @@ RSpec.describe Scheme, type: :model do end end - describe "all schemes" do - before do - FactoryBot.create_list(:scheme, 4) - FactoryBot.create_list(:scheme, 3, confirmed: false) - FactoryBot.create_list(:scheme, 2, confirmed: nil) - end - - it "can sort the schemes by status" do - all_schemes = described_class.all.order("confirmed ASC NULLS FIRST", service_name: :asc) - expect(all_schemes.count).to eq(9) - expect(all_schemes[0].status).to eq(:incomplete) - expect(all_schemes[1].status).to eq(:incomplete) - expect(all_schemes[2].status).to eq(:incomplete) - expect(all_schemes[3].status).to eq(:incomplete) - expect(all_schemes[4].status).to eq(:incomplete) - end - end - describe "available_from" do context "when the scheme was created at the start of the 2022/23 collection window" do let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2022, 4, 6)) }