From fde3c4689a7332f421e016af6ba04de5f137abcf Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 15 Aug 2023 08:43:02 +0100 Subject: [PATCH] Search and filter at the same time (#1836) --- app/models/scheme.rb | 15 +++++++-------- spec/models/scheme_spec.rb | 9 ++++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 4767b92cf..08d7dedd5 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -34,7 +34,6 @@ class Scheme < ApplicationRecord if scopes.any? filtered_records = filtered_records .left_outer_joins(:scheme_deactivation_periods) - .order("scheme_deactivation_periods.created_at DESC") .merge(scopes.reduce(&:or)) end @@ -44,9 +43,9 @@ class Scheme < ApplicationRecord scope :incomplete, lambda { where.not(confirmed: true) .or(where.not(id: Location.select(:scheme_id).where(confirmed: true).distinct)) - .where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id)) - .where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id)) - .where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id)) + .where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id, :service_name, :confirmed)) + .where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id, :service_name, :confirmed)) + .where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id, :service_name, :confirmed)) } scope :deactivated, lambda { @@ -65,10 +64,10 @@ class Scheme < ApplicationRecord } scope :active_status, lambda { - where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id)) - .where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id)) - .where.not(id: incomplete.pluck(:id)) - .where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id)) + where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id, :service_name, :confirmed)) + .where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id, :service_name, :confirmed)) + .where.not(id: incomplete.pluck(:id, :service_name, :confirmed)) + .where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id, :service_name, :confirmed)) } validate :validate_confirmed diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index 049da077a..0001d55f7 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -110,7 +110,7 @@ RSpec.describe Scheme, type: :model do end context "when filtering by status" do - let!(:incomplete_scheme) { FactoryBot.create(:scheme, :incomplete) } + let!(:incomplete_scheme) { FactoryBot.create(:scheme, :incomplete, service_name: "name") } let(:active_scheme) { FactoryBot.create(:scheme) } let(:deactivating_soon_scheme) { FactoryBot.create(:scheme) } let(:deactivated_scheme) { FactoryBot.create(:scheme) } @@ -141,6 +141,13 @@ RSpec.describe Scheme, type: :model do end end + context "when filtering by incomplete status and searching" do + it "returns only incomplete schemes" do + expect(described_class.search_by("name").filter_by_status(%w[incomplete]).count).to eq(1) + expect(described_class.search_by("name").filter_by_status(%w[incomplete]).first).to eq(incomplete_scheme) + end + end + context "when filtering by active status" do it "returns only active schemes" do expect(described_class.filter_by_status(%w[active]).count).to eq(1)