From 9a765a6415b7f7ce74021870db30e8d119dd8e5d Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 12 Apr 2022 12:13:05 +0100 Subject: [PATCH] Add year filter (#464) * add year filter * update text and filter order * fix typo * extract partials * add tests with 2 filters * Allow OR filters within scope categories and AND filters across them * Rubocop * Remove some of the N+1 query madness by preloading required organisations * Refactor scopes * Fix new specs * unticked checkboxes by deault * fix test Co-authored-by: baarkerlounger Co-authored-by: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> --- app/controllers/case_logs_controller.rb | 19 +++- app/helpers/filters_helper.rb | 8 +- app/models/case_log.rb | 9 ++ app/views/case_logs/_log_filters.erb | 20 +--- app/views/filters/_checkbox_filter.html.erb | 10 ++ spec/helpers/filters_helper_spec.rb | 10 +- spec/models/case_log_spec.rb | 32 ++++++ spec/requests/case_logs_controller_spec.rb | 117 +++++++++++++++----- 8 files changed, 172 insertions(+), 53 deletions(-) create mode 100644 app/views/filters/_checkbox_filter.html.erb diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index ca9804056..b4b4f4851 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -7,7 +7,7 @@ class CaseLogsController < ApplicationController before_action :find_resource, except: %i[create index edit] def index - set_session_filters if params[:status].present? + set_session_filters @pagy, @case_logs = pagy(filtered_case_logs) @@ -121,14 +121,21 @@ private end def filtered_case_logs - user_case_logs = current_user.case_logs - status_filter = JSON.parse(session[:case_logs_filters])["status"] if session[:case_logs_filters].present? - return user_case_logs unless status_filter + 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 if values.reject(&:empty?).blank? - user_case_logs.filter_by_status(status_filter) + query = query.public_send("filter_by_#{category}", values) + end + end + query.all.includes(:owning_organisation, :managing_organisation) end 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 years].each { |filter| new_filters[filter] = params[filter] if params[filter].present? } + session[:case_logs_filters] = new_filters.to_json end end diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index aaee74fb6..f448bbeb5 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -1,9 +1,11 @@ module FiltersHelper - def filter_selected?(filter) - return true unless session[:case_logs_filters] + def filter_selected?(filter, value) + return false unless session[:case_logs_filters] selected_filters = JSON.parse(session[:case_logs_filters]) - selected_filters["status"].present? && selected_filters["status"].include?(filter.to_s) + return false if selected_filters[filter].blank? + + selected_filters[filter].include?(value.to_s) end def status_filters diff --git a/app/models/case_log.rb b/app/models/case_log.rb index aa926d5b4..99ea8fdb5 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -35,6 +35,13 @@ 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_years, lambda { |years| + first_year = years.shift + query = filter_by_year(first_year) + years.each { |year| query = query.or(filter_by_year(year)) } + 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 OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing tenant_code propcode].freeze @@ -51,6 +58,8 @@ class CaseLog < ApplicationRecord end def collection_start_year + return unless startdate + window_end_date = Time.zone.local(startdate.year, 4, 1) startdate < window_end_date ? startdate.year - 1 : startdate.year end diff --git a/app/views/case_logs/_log_filters.erb b/app/views/case_logs/_log_filters.erb index 4e5593a62..09432faa7 100644 --- a/app/views/case_logs/_log_filters.erb +++ b/app/views/case_logs/_log_filters.erb @@ -2,9 +2,9 @@

Filters

-