Browse Source

Rubocop

pull/466/head
baarkerlounger 3 years ago
parent
commit
2ffc7230f8
  1. 2
      app/controllers/case_logs_controller.rb
  2. 8
      app/models/case_log.rb
  3. 10
      spec/models/case_log_spec.rb

2
app/controllers/case_logs_controller.rb

@ -125,7 +125,7 @@ private
if session[:case_logs_filters].present? if session[:case_logs_filters].present?
filters = JSON.parse(session[:case_logs_filters]) filters = JSON.parse(session[:case_logs_filters])
filters.each do |category, values| filters.each do |category, values|
next unless values.reject(&:empty?).present? next if values.reject(&:empty?).blank?
query = query.public_send("filter_by_#{category}", values) query = query.public_send("filter_by_#{category}", values)
end end

8
app/models/case_log.rb

@ -35,12 +35,12 @@ class CaseLog < ApplicationRecord
scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) }
scope :filter_by_status, ->(status) { where status: status } scope :filter_by_status, ->(status) { where status: status }
scope :filter_by_year, ->(year) { scope :filter_by_year, lambda { |year_s|
years = Array(year) years = Array(year_s)
first_year = years.shift 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) 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| 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)) 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 end
query.all query.all
} }

10
spec/models/case_log_spec.rb

@ -1835,25 +1835,25 @@ RSpec.describe CaseLog do
context "when filtering by year" do context "when filtering by year" do
it "allows filtering on a single year" do it "allows filtering on a single year" do
expect(CaseLog.filter_by_year("2021").count).to eq(2) expect(described_class.filter_by_year("2021").count).to eq(2)
end end
it "allows filtering by multiple years using OR" do it "allows filtering by multiple years using OR" do
expect(CaseLog.filter_by_year(["2021", "2022"]).count).to eq(3) expect(described_class.filter_by_year(%w[2021 2022]).count).to eq(3)
end end
it "can filter by year(s) AND status" do it "can filter by year(s) AND status" do
expect(CaseLog.filter_by_year(["2021", "2022"]).filter_by_status("completed").count).to eq(1) expect(described_class.filter_by_year(%w[2021 2022]).filter_by_status("completed").count).to eq(1)
end end
end end
context "when filtering on status" do context "when filtering on status" do
it "allows filtering on a single status" do it "allows filtering on a single status" do
expect(CaseLog.filter_by_status("in_progress").count).to eq(2) expect(described_class.filter_by_status("in_progress").count).to eq(2)
end end
it "allows filtering on multiple statuses" do it "allows filtering on multiple statuses" do
expect(CaseLog.filter_by_status(["in_progress", "completed"]).count).to eq(3) expect(described_class.filter_by_status(%w[in_progress completed]).count).to eq(3)
end end
end end
end end

Loading…
Cancel
Save