Browse Source

Add user filter (#467)

* Add user filter

* Add unit tests for user scope
pull/471/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
919f25dd74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/controllers/case_logs_controller.rb
  2. 10
      app/models/case_log.rb
  3. 1
      app/views/case_logs/_log_filters.erb
  4. 26
      spec/models/case_log_spec.rb

4
app/controllers/case_logs_controller.rb

@ -127,7 +127,7 @@ private
filters.each do |category, values| filters.each do |category, values|
next if values.reject(&:empty?).blank? next if values.reject(&:empty?).blank?
query = query.public_send("filter_by_#{category}", values) query = query.public_send("filter_by_#{category}", values, current_user)
end end
end end
query.all.includes(:owning_organisation, :managing_organisation) query.all.includes(:owning_organisation, :managing_organisation)
@ -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 years].each { |filter| new_filters[filter] = params[filter] if params[filter].present? } %i[status years user].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

10
app/models/case_log.rb

@ -34,8 +34,8 @@ class CaseLog < ApplicationRecord
belongs_to :managing_organisation, class_name: "Organisation" belongs_to :managing_organisation, class_name: "Organisation"
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, _user = nil) { where status: status }
scope :filter_by_years, lambda { |years| scope :filter_by_years, lambda { |years, _user = nil|
first_year = years.shift first_year = years.shift
query = filter_by_year(first_year) query = filter_by_year(first_year)
years.each { |year| query = query.or(filter_by_year(year)) } years.each { |year| query = query.or(filter_by_year(year)) }
@ -43,6 +43,12 @@ class CaseLog < ApplicationRecord
} }
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_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 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
RENT_TYPE_MAPPING = { 0 => 1, 1 => 2, 2 => 2, 3 => 3, 4 => 3, 5 => 3 }.freeze RENT_TYPE_MAPPING = { 0 => 1, 1 => 2, 2 => 2, 3 => 3, 4 => 3, 5 => 3 }.freeze

1
app/views/case_logs/_log_filters.erb

@ -16,6 +16,7 @@
<% 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: "years" } %> <%= 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" } %>
<%= 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" %> <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-top-4" %>
<% end %> <% end %>
</div> </div>

26
spec/models/case_log_spec.rb

@ -1827,9 +1827,10 @@ RSpec.describe CaseLog do
end end
describe "scopes" do 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 before do
FactoryBot.create(:case_log, :in_progress, startdate: Time.utc(2021, 5, 3))
FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3))
FactoryBot.create(:case_log, startdate: Time.utc(2022, 6, 3)) FactoryBot.create(:case_log, startdate: Time.utc(2022, 6, 3))
end end
@ -1856,5 +1857,26 @@ RSpec.describe CaseLog do
expect(described_class.filter_by_status(%w[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
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
end end

Loading…
Cancel
Save