Browse Source

Allow OR filters within scope categories and AND filters across them

pull/466/head
baarkerlounger 3 years ago
parent
commit
355b78ad5e
  1. 12
      app/controllers/case_logs_controller.rb
  2. 10
      app/models/case_log.rb
  3. 32
      spec/models/case_log_spec.rb

12
app/controllers/case_logs_controller.rb

@ -121,18 +121,16 @@ private
end
def filtered_case_logs
user_case_logs = current_user.case_logs
query = CaseLog.for_organisation(current_user.organisation)
if session[:case_logs_filters].present?
filters = JSON.parse(session[:case_logs_filters])
filters.each do |category, values|
next unless values.reject(&:empty?).present?
%w[status year].each do |category|
if filters[category]
filtered_by_category = filters[category].select(&:present?).map { |filter| user_case_logs.public_send("filter_by_#{category}", filter) }.flatten
user_case_logs = CaseLog.where(id: filtered_by_category.map(&:id))
end
query = query.public_send("filter_by_#{category}", values)
end
end
user_case_logs
query.all
end
def set_session_filters

10
app/models/case_log.rb

@ -35,7 +35,15 @@ class CaseLog < ApplicationRecord
scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) }
scope :filter_by_status, ->(status) { where status: status }
scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :filter_by_year, ->(year) {
years = Array(year)
first_year = years.shift
query = where('startdate >= ?', Time.utc(first_year.to_i, 4, 1)).where('startdate <= ?',Time.utc(first_year.to_i + 1, 3, 31).end_of_day)
years.each do |year|
query = query.or(where('startdate >= ?', Time.utc(year.to_i, 4, 1)).where('startdate <= ?',Time.utc(year.to_i + 1, 3, 31).end_of_day))
end
query.all
}
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing tenant_code propcode].freeze

32
spec/models/case_log_spec.rb

@ -1825,4 +1825,36 @@ RSpec.describe CaseLog do
end
end
end
describe "scopes" do
before do
FactoryBot.create(:case_log, :in_progress, startdate: Time.utc(2021, 5, 3))
FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3))
FactoryBot.create(:case_log, startdate: Time.utc(2022, 6, 3))
end
context "when filtering by year" do
it "allows filtering on a single year" do
expect(CaseLog.filter_by_year("2021").count).to eq(2)
end
it "allows filtering by multiple years using OR" do
expect(CaseLog.filter_by_year(["2021", "2022"]).count).to eq(3)
end
it "can filter by year(s) AND status" do
expect(CaseLog.filter_by_year(["2021", "2022"]).filter_by_status("completed").count).to eq(1)
end
end
context "when filtering on status" do
it "allows filtering on a single status" do
expect(CaseLog.filter_by_status("in_progress").count).to eq(2)
end
it "allows filtering on multiple statuses" do
expect(CaseLog.filter_by_status(["in_progress", "completed"]).count).to eq(3)
end
end
end
end

Loading…
Cancel
Save