Browse Source

Rename org filter

pull/522/head
baarkerlounger 3 years ago
parent
commit
cfaf7c5f7d
  1. 3
      app/controllers/case_logs_controller.rb
  2. 2
      app/models/case_log.rb
  3. 2
      app/models/organisation.rb
  4. 10
      app/models/user.rb
  5. 3
      app/views/case_logs/_log_filters.erb
  6. 16
      spec/models/case_log_spec.rb
  7. 8
      spec/models/user_spec.rb

3
app/controllers/case_logs_controller.rb

@ -137,8 +137,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? } current_user.case_logs_filters.each { |filter| new_filters[filter] = params[filter] if params[filter].present? }
new_filters[:user] = [params[:user]] if params[:user].present?
session[:case_logs_filters] = new_filters.to_json session[:case_logs_filters] = new_filters.to_json
end end

2
app/models/case_log.rb

@ -34,7 +34,7 @@ class CaseLog < ApplicationRecord
belongs_to :managing_organisation, class_name: "Organisation" belongs_to :managing_organisation, class_name: "Organisation"
belongs_to :created_by, class_name: "User" belongs_to :created_by, class_name: "User"
scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) }
scope :filter_by_status, ->(status, _user = nil) { where status: } scope :filter_by_status, ->(status, _user = nil) { where status: }
scope :filter_by_years, lambda { |years, _user = nil| scope :filter_by_years, lambda { |years, _user = nil|
first_year = years.shift first_year = years.shift

2
app/models/organisation.rb

@ -18,7 +18,7 @@ class Organisation < ApplicationRecord
validates :provider_type, presence: true validates :provider_type, presence: true
def case_logs def case_logs
CaseLog.for_organisation(self) CaseLog.filter_by_organisation(self)
end end
def completed_case_logs def completed_case_logs

10
app/models/user.rb

@ -36,7 +36,7 @@ class User < ApplicationRecord
if support? if support?
CaseLog.all CaseLog.all
else else
CaseLog.for_organisation(organisation) CaseLog.filter_by_organisation(organisation)
end end
end end
@ -88,4 +88,12 @@ class User < ApplicationRecord
ROLES.except(:support) ROLES.except(:support)
end end
def case_logs_filters
if support?
%i[status years user organisation]
else
%i[status years user]
end
end
end end

3
app/views/case_logs/_log_filters.erb

@ -10,6 +10,9 @@
<%= 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/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user", } %> <%= render partial: "filters/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user", } %>
<% if @current_user.support? %>
<%= render partial: "filters/checkbox_filter", locals: { f: f, options: {"all": "All", "yours": "Yours"}, label: "Organisation", category: "organisation" } %>
<% end %>
<%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %> <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %>
<% end %> <% end %>
</div> </div>

16
spec/models/case_log_spec.rb

@ -1893,6 +1893,22 @@ RSpec.describe CaseLog do
end end
end end
context "when filtering by organisation" do
let(:organisation_1) { FactoryBot.create(:organisation) }
let(:organisation_2) { FactoryBot.create(:organisation) }
let(:organisation_3) { FactoryBot.create(:organisation) }
let!(:case_log_1) { FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation_1, managing_organisation: organisation_1) }
let!(:case_log_2) { FactoryBot.create(:case_log, :completed, owning_organisation: organisation_1, managing_organisation: organisation_2) }
let!(:case_log_3) { FactoryBot.create(:case_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_1) }
let!(:case_log_4) { FactoryBot.create(:case_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_2) }
it "filters by given organisation" do
expect(described_class.filter_by_organisation([organisation_1]).count).to eq(3)
expect(described_class.filter_by_organisation([organisation_1, organisation_2]).count).to eq(4)
expect(described_class.filter_by_organisation([organisation_3]).count).to eq(0)
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(%w[in_progress]).count).to eq(2) expect(described_class.filter_by_status(%w[in_progress]).count).to eq(2)

8
spec/models/user_spec.rb

@ -97,6 +97,10 @@ RSpec.describe User, type: :model do
data_coordinator: 2, data_coordinator: 2,
}) })
end end
it "can filter case logs by user, year and status" do
expect(user.case_logs_filters).to eq(%i[status years user])
end
end end
context "when the user is a Customer Support person" do context "when the user is a Customer Support person" do
@ -119,6 +123,10 @@ RSpec.describe User, type: :model do
support: 99, support: 99,
}) })
end end
it "can filter case logs by user, year, status and organisation" do
expect(user.case_logs_filters).to eq(%i[status years user organisation])
end
end end
end end

Loading…
Cancel
Save