Browse Source

add year filter

pull/464/head
Kat 3 years ago
parent
commit
333c05f053
  1. 19
      app/controllers/case_logs_controller.rb
  2. 6
      app/helpers/filters_helper.rb
  3. 3
      app/models/case_log.rb
  4. 13
      app/views/case_logs/_log_filters.erb
  5. 8
      spec/helpers/filters_helper_spec.rb
  6. 30
      spec/requests/case_logs_controller_spec.rb

19
app/controllers/case_logs_controller.rb

@ -7,7 +7,7 @@ class CaseLogsController < ApplicationController
before_action :find_resource, except: %i[create index edit] before_action :find_resource, except: %i[create index edit]
def index def index
set_session_filters if params[:status].present? set_session_filters
@pagy, @case_logs = pagy(filtered_case_logs) @pagy, @case_logs = pagy(filtered_case_logs)
@ -122,13 +122,22 @@ private
def filtered_case_logs def filtered_case_logs
user_case_logs = current_user.case_logs user_case_logs = current_user.case_logs
status_filter = JSON.parse(session[:case_logs_filters])["status"] if session[:case_logs_filters].present? if session[:case_logs_filters].present?
return user_case_logs unless status_filter filters = JSON.parse(session[:case_logs_filters])
user_case_logs.filter_by_status(status_filter) %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
end
end
user_case_logs
end end
def set_session_filters def set_session_filters
session[:case_logs_filters] = { status: params[:status] }.to_json 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? }
session[:case_logs_filters] = new_filters.to_json
end end
end end

6
app/helpers/filters_helper.rb

@ -1,9 +1,11 @@
module FiltersHelper module FiltersHelper
def filter_selected?(filter) def filter_selected?(filter, value)
return true unless session[:case_logs_filters] return true unless session[:case_logs_filters]
selected_filters = JSON.parse(session[:case_logs_filters]) selected_filters = JSON.parse(session[:case_logs_filters])
selected_filters["status"].present? && selected_filters["status"].include?(filter.to_s) return true if selected_filters[filter].blank?
selected_filters[filter].include?(value.to_s)
end end
def status_filters def status_filters

3
app/models/case_log.rb

@ -35,6 +35,7 @@ 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) { where(startdate: Time.zone.local(year, 4, 1)...Time.zone.local(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
@ -51,6 +52,8 @@ class CaseLog < ApplicationRecord
end end
def collection_start_year def collection_start_year
return unless startdate
window_end_date = Time.zone.local(startdate.year, 4, 1) window_end_date = Time.zone.local(startdate.year, 4, 1)
startdate < window_end_date ? startdate.year - 1 : startdate.year startdate < window_end_date ? startdate.year - 1 : startdate.year
end end

13
app/views/case_logs/_log_filters.erb

@ -19,7 +19,18 @@
<% statuses.map do |key, option| %> <% statuses.map do |key, option| %>
<%= f.govuk_check_box "status", "#{key}", <%= f.govuk_check_box "status", "#{key}",
label: { text: option }, label: { text: option },
checked: filter_selected?(key), checked: filter_selected?("status", key),
size: "s" %>
<% end %>
</div>
<% end %>
<%= f.govuk_check_boxes_fieldset :year, legend: { text: "Year", size: "s"} do %>
<div class="govuk-checkboxes govuk-checkboxes--small" data-module="govuk-checkboxes">
<% years = {"2021": "2021", "2022": "2022"} %>
<% years.map do |key, option| %>
<%= f.govuk_check_box "year", "#{key}",
label: { text: option },
checked: filter_selected?("year", key),
size: "s" %> size: "s" %>
<% end %> <% end %>
</div> </div>

8
spec/helpers/filters_helper_spec.rb

@ -4,8 +4,8 @@ RSpec.describe FiltersHelper do
describe "#filter_selected?" do describe "#filter_selected?" do
context "when no filters are selected" do context "when no filters are selected" do
it "returns true for all filters" do it "returns true for all filters" do
expect(filter_selected?("completed")).to be_truthy expect(filter_selected?("status", "completed")).to be_truthy
expect(filter_selected?("in_progress")).to be_truthy expect(filter_selected?("status", "in_progress")).to be_truthy
end end
end end
@ -15,11 +15,11 @@ RSpec.describe FiltersHelper do
end end
it "returns false for non selected filters" do it "returns false for non selected filters" do
expect(filter_selected?("completed")).to be_falsey expect(filter_selected?("status", "completed")).to be_falsey
end end
it "returns true for selected filter" do it "returns true for selected filter" do
expect(filter_selected?("in_progress")).to be_truthy expect(filter_selected?("status", "in_progress")).to be_truthy
end end
end end
end end

30
spec/requests/case_logs_controller_spec.rb

@ -174,6 +174,7 @@ RSpec.describe CaseLogsController, type: :request do
end end
context "when filtering" do context "when filtering" do
context "with status fileter" do
let!(:in_progress_case_log) do let!(:in_progress_case_log) do
FactoryBot.create(:case_log, :in_progress, FactoryBot.create(:case_log, :in_progress,
owning_organisation: organisation, owning_organisation: organisation,
@ -207,6 +208,35 @@ RSpec.describe CaseLogsController, type: :request do
expect(page).not_to have_link(completed_case_log.id.to_s) expect(page).not_to have_link(completed_case_log.id.to_s)
end end
end end
context "with year fileter" do
let!(:case_log_2021) do
FactoryBot.create(:case_log, :in_progress,
owning_organisation: organisation,
startdate: Time.zone.local(2022, 3, 1),
managing_organisation: organisation)
end
let!(:case_log_2022) do
FactoryBot.create(:case_log, :completed,
owning_organisation: organisation,
mrcdate: Time.zone.local(2022, 2, 1),
startdate: Time.zone.local(2022, 12, 1),
managing_organisation: organisation)
end
it "shows case logs for multiple selected statuses" do
get "/logs?year[]=2021&year[]=2022", headers: headers, params: {}
expect(page).to have_link(case_log_2021.id.to_s)
expect(page).to have_link(case_log_2022.id.to_s)
end
it "shows case logs for one selected status" do
get "/logs?year[]=2021", headers: headers, params: {}
expect(page).to have_link(case_log_2021.id.to_s)
expect(page).not_to have_link(case_log_2022.id.to_s)
end
end
end
end end
context "when the user is not a customer support user" do context "when the user is not a customer support user" do

Loading…
Cancel
Save