Browse Source

Merge branch 'main' into import-temporary

pull/470/head
Stéphane Meny 3 years ago committed by GitHub
parent
commit
d1299f4100
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      app/controllers/case_logs_controller.rb
  2. 8
      app/helpers/filters_helper.rb
  3. 16
      app/models/case_log.rb
  4. 15
      app/views/case_logs/_log_filters.erb
  5. 10
      app/views/filters/_checkbox_filter.html.erb
  6. 2
      config/locales/en.yml
  7. 10
      spec/helpers/filters_helper_spec.rb
  8. 54
      spec/models/case_log_spec.rb
  9. 117
      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]
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, current_user)
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 user].each { |filter| new_filters[filter] = params[filter] if params[filter].present? }
session[:case_logs_filters] = new_filters.to_json
end
end

8
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

16
app/models/case_log.rb

@ -34,7 +34,20 @@ class CaseLog < ApplicationRecord
belongs_to :managing_organisation, class_name: "Organisation"
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, _user = nil) { where status: status }
scope :filter_by_years, lambda { |years, _user = nil|
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)) }
scope :filter_by_user, lambda { |selected_user, user|
if !selected_user.include?("all") && user.present?
where(id: PaperTrail::Version.where(item_type: "CaseLog", event: "create", whodunnit: user.to_global_id.uri.to_s).map(&:item_id))
end
}
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
@ -409,7 +422,6 @@ private
end
self.nocharge = household_charge&.zero? ? 1 : 0
self.underoccupation_benefitcap = 3 if renewal == 1 && collection_start_year == 2021
self.ethnic = ethnic || ethnic_group
self.housingneeds = get_housingneeds
if is_renewal?
self.underoccupation_benefitcap = 2 if collection_start_year == 2021

15
app/views/case_logs/_log_filters.erb

@ -13,17 +13,10 @@
<div class="app-filter__content">
<div class="govuk-form-group app-filter__group">
<%= form_with url: "/logs", html: { method: :get } do |f| %>
<%= f.govuk_check_boxes_fieldset :status, legend: { text: "Status", size: "s"} do %>
<div class="govuk-checkboxes govuk-checkboxes--small" data-module="govuk-checkboxes">
<% statuses = status_filters %>
<% statuses.map do |key, option| %>
<%= f.govuk_check_box "status", "#{key}",
label: { text: option },
checked: filter_selected?(key),
size: "s" %>
<% end %>
</div>
<% end %>
<% years = {"2021": "2021/22", "2022": "2022/23"} %>
<%= 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: {"all": "All", "yours": "Yours"}, label: "Logs", category: "user" } %>
<%= f.govuk_submit "Apply filters", class: "govuk-!-margin-top-4" %>
<% end %>
</div>

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

@ -0,0 +1,10 @@
<%= 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">
<% options.map do |key, option| %>
<%= f.govuk_check_box category, "#{key}",
label: { text: option },
checked: filter_selected?(category, key),
size: "s" %>
<% end %>
</div>
<% end %>

2
config/locales/en.yml

@ -133,7 +133,7 @@ en:
period:
not_in_range: "Basic rent is outside of the expected range based on this period"
charges:
complete_1_of_3: "Answer only one of the following questions: 'total charges', 'care home charges' or answer 'no' to the 'does the household pay rent or charges?' question"
complete_1_of_3: "Answer either the ‘household rent and charges’ question or 'is this accommodation a care home', or select ‘no’ for ‘does the household pay rent or charges for the accommodation?’"
tcharge:
under_10: "Total charge must be at least £10 per week"

10
spec/helpers/filters_helper_spec.rb

@ -3,9 +3,9 @@ require "rails_helper"
RSpec.describe FiltersHelper do
describe "#filter_selected?" do
context "when no filters are selected" do
it "returns true for all filters" do
expect(filter_selected?("completed")).to be_truthy
expect(filter_selected?("in_progress")).to be_truthy
it "returns false for all filters" do
expect(filter_selected?("status", "completed")).to be_falsey
expect(filter_selected?("status", "in_progress")).to be_falsey
end
end
@ -15,11 +15,11 @@ RSpec.describe FiltersHelper do
end
it "returns false for non selected filters" do
expect(filter_selected?("completed")).to be_falsey
expect(filter_selected?("status", "completed")).to be_falsey
end
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

54
spec/models/case_log_spec.rb

@ -1817,4 +1817,58 @@ RSpec.describe CaseLog do
end
end
end
describe "scopes" do
let!(:case_log_1) { FactoryBot.create(:case_log, :in_progress, startdate: Time.utc(2021, 5, 3)) }
let!(:case_log_2) { FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3)) }
before do
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(described_class.filter_by_years(%w[2021]).count).to eq(2)
end
it "allows filtering by multiple years using OR" do
expect(described_class.filter_by_years(%w[2021 2022]).count).to eq(3)
end
it "can filter by year(s) AND status" do
expect(described_class.filter_by_years(%w[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(described_class.filter_by_status(%w[in_progress]).count).to eq(2)
end
it "allows filtering on multiple statuses" do
expect(described_class.filter_by_status(%w[in_progress completed]).count).to eq(3)
end
end
context "when filtering by user" do
let!(:user) { FactoryBot.create(:user) }
before do
PaperTrail::Version.find_by(item_id: case_log_1.id, event: "create").update!(whodunnit: user.to_global_id.uri.to_s)
PaperTrail::Version.find_by(item_id: case_log_2.id, event: "create").update!(whodunnit: user.to_global_id.uri.to_s)
end
it "allows filtering on current user" do
expect(described_class.filter_by_user(%w[yours], user).count).to eq(2)
end
it "returns all logs when all logs selected" do
expect(described_class.filter_by_user(%w[all], user).count).to eq(3)
end
it "returns all logs when all and your users selected" do
expect(described_class.filter_by_user(%w[all yours], user).count).to eq(3)
end
end
end
end

117
spec/requests/case_logs_controller_spec.rb

@ -174,37 +174,104 @@ RSpec.describe CaseLogsController, type: :request do
end
context "when filtering" do
let!(:in_progress_case_log) do
FactoryBot.create(:case_log, :in_progress,
owning_organisation: organisation,
managing_organisation: organisation)
end
let!(:completed_case_log) do
FactoryBot.create(:case_log, :completed,
owning_organisation: organisation,
managing_organisation: organisation)
end
context "with status filter" do
let!(:in_progress_case_log) do
FactoryBot.create(:case_log, :in_progress,
owning_organisation: organisation,
managing_organisation: organisation)
end
let!(:completed_case_log) do
FactoryBot.create(:case_log, :completed,
owning_organisation: organisation,
managing_organisation: organisation)
end
it "shows case logs for multiple selected statuses" do
get "/logs?status[]=in_progress&status[]=completed", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).to have_link(completed_case_log.id.to_s)
end
it "shows case logs for multiple selected statuses" do
get "/logs?status[]=in_progress&status[]=completed", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).to have_link(completed_case_log.id.to_s)
it "shows case logs for one selected status" do
get "/logs?status[]=in_progress", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).not_to have_link(completed_case_log.id.to_s)
end
it "does not reset the filters" do
get "/logs?status[]=in_progress", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).not_to have_link(completed_case_log.id.to_s)
get "/logs", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).not_to have_link(completed_case_log.id.to_s)
end
end
it "shows case logs for one selected status" do
get "/logs?status[]=in_progress", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).not_to have_link(completed_case_log.id.to_s)
context "with year filter" 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 years" do
get "/logs?years[]=2021&years[]=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 year" do
get "/logs?years[]=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
it "does not reset the filters" do
get "/logs?status[]=in_progress", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).not_to have_link(completed_case_log.id.to_s)
context "with year and status filter" 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
let!(:case_log_2022_in_progress) do
FactoryBot.create(:case_log, :in_progress,
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 and years" do
get "/logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed", 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)
expect(page).to have_link(case_log_2022_in_progress.id.to_s)
end
get "/logs", headers: headers, params: {}
expect(page).to have_link(in_progress_case_log.id.to_s)
expect(page).not_to have_link(completed_case_log.id.to_s)
it "shows case logs for one selected status" do
get "/logs?years[]=2022&status[]=in_progress", headers: headers, params: {}
expect(page).to have_link(case_log_2022_in_progress.id.to_s)
expect(page).not_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

Loading…
Cancel
Save