Browse Source

Refactor scopes

pull/466/head
baarkerlounger 3 years ago
parent
commit
7cfbfce7c1
  1. 2
      app/controllers/case_logs_controller.rb
  2. 11
      app/models/case_log.rb
  3. 4
      app/views/case_logs/_log_filters.erb
  4. 2
      app/views/filters/_checkbox_filter.html.erb
  5. 8
      spec/models/case_log_spec.rb

2
app/controllers/case_logs_controller.rb

@ -135,7 +135,7 @@ private
def set_session_filters def set_session_filters
new_filters = session[:case_logs_filters].present? ? JSON.parse(session[:case_logs_filters]) : {} new_filters = session[:case_logs_filters].present? ? JSON.parse(session[:case_logs_filters]) : {}
%i[status year].each { |filter| new_filters[filter] = params[filter] if params[filter].present? } %i[status years].each { |filter| new_filters[filter] = params[filter] if params[filter].present? }
session[:case_logs_filters] = new_filters.to_json session[:case_logs_filters] = new_filters.to_json
end end
end end

11
app/models/case_log.rb

@ -35,15 +35,14 @@ 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, lambda { |year_s| scope :filter_by_years, lambda { |years|
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 = filter_by_year(first_year)
years.each do |year| years.each { |year| query = query.or(filter_by_year(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 query.all
} }
scope :filter_by_year, ->(year) { where(startdate: Time.utc(year.to_i, 4, 1)...Time.utc(year.to_i + 1, 4, 1)) }
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze 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 OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing tenant_code propcode].freeze

4
app/views/case_logs/_log_filters.erb

@ -14,8 +14,8 @@
<div class="govuk-form-group app-filter__group"> <div class="govuk-form-group app-filter__group">
<%= form_with url: "/logs", html: { method: :get } do |f| %> <%= form_with url: "/logs", html: { method: :get } do |f| %>
<% years = {"2021": "2021/22", "2022": "2022/23"} %> <% years = {"2021": "2021/22", "2022": "2022/23"} %>
<%= render partial: "filters/checkbox_filter", locals: {f: f, options: years, label: "Collection year", category: "year" } %> <%= render partial: "filters/checkbox_filter", locals: { f: f, options: years, label: "Collection year", category: "years" } %>
<%= render partial: "filters/checkbox_filter", locals: {f: f, options: status_filters, label: "Status", category: "status" } %> <%= render partial: "filters/checkbox_filter", locals: { f: f, options: status_filters, label: "Status", category: "status" } %>
<%= f.govuk_submit "Apply filters", class: "govuk-!-margin-top-4" %> <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-top-4" %>
<% end %> <% end %>
</div> </div>

2
app/views/filters/_checkbox_filter.html.erb

@ -1,4 +1,4 @@
<%= f.govuk_check_boxes_fieldset :year, legend: { text: label, size: "s"} do %> <%= f.govuk_check_boxes_fieldset category.to_sym, legend: { text: label, size: "s"} do %>
<div class="govuk-checkboxes govuk-checkboxes--small" data-module="govuk-checkboxes"> <div class="govuk-checkboxes govuk-checkboxes--small" data-module="govuk-checkboxes">
<% options.map do |key, option| %> <% options.map do |key, option| %>
<%= f.govuk_check_box category, "#{key}", <%= f.govuk_check_box category, "#{key}",

8
spec/models/case_log_spec.rb

@ -1835,21 +1835,21 @@ 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(described_class.filter_by_year("2021").count).to eq(2) expect(described_class.filter_by_years(%w[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(described_class.filter_by_year(%w[2021 2022]).count).to eq(3) expect(described_class.filter_by_years(%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(described_class.filter_by_year(%w[2021 2022]).filter_by_status("completed").count).to eq(1) expect(described_class.filter_by_years(%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(described_class.filter_by_status("in_progress").count).to eq(2) expect(described_class.filter_by_status(%w[in_progress]).count).to eq(2)
end end
it "allows filtering on multiple statuses" do it "allows filtering on multiple statuses" do

Loading…
Cancel
Save