Browse Source

Filter values correctly

pull/582/head
baarkerlounger 3 years ago
parent
commit
864c809f7d
  1. 2
      app/controllers/case_logs_controller.rb
  2. 2
      app/helpers/filters_helper.rb
  3. 4
      app/models/user.rb
  4. 11
      spec/helpers/filters_helper_spec.rb
  5. 4
      spec/models/user_spec.rb
  6. 8
      spec/requests/case_logs_controller_spec.rb

2
app/controllers/case_logs_controller.rb

@ -127,6 +127,7 @@ private
filters = JSON.parse(session[:case_logs_filters]) filters = JSON.parse(session[:case_logs_filters])
filters.each do |category, values| filters.each do |category, values|
next if Array(values).reject(&:empty?).blank? next if Array(values).reject(&:empty?).blank?
next if category == "organisation" && params["organisation_select"] == "all"
query = query.public_send("filter_by_#{category}", values, current_user) query = query.public_send("filter_by_#{category}", values, current_user)
end end
@ -138,6 +139,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]) : {}
current_user.case_logs_filters.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 = new_filters.except("organisation") if params["organisation_select"] == "all"
session[:case_logs_filters] = new_filters.to_json session[:case_logs_filters] = new_filters.to_json
end end

2
app/helpers/filters_helper.rb

@ -4,7 +4,7 @@ module FiltersHelper
selected_filters = JSON.parse(session[:case_logs_filters]) selected_filters = JSON.parse(session[:case_logs_filters])
return true if selected_filters.blank? && filter == "user" && value == :all return true if selected_filters.blank? && filter == "user" && value == :all
return true if selected_filters.blank? && filter == "organisation_select" && value == :all return true if !selected_filters.key?("organisation") && filter == "organisation_select" && value == :all
return true if selected_filters["organisation"].present? && filter == "organisation_select" && value == :specific_org return true if selected_filters["organisation"].present? && filter == "organisation_select" && value == :specific_org
return false if selected_filters[filter].blank? return false if selected_filters[filter].blank?

4
app/models/user.rb

@ -91,9 +91,9 @@ class User < ApplicationRecord
def case_logs_filters def case_logs_filters
if support? if support?
%i[status years user organisation] %w[status years user organisation]
else else
%i[status years user] %w[status years user]
end end
end end
end end

11
spec/helpers/filters_helper_spec.rb

@ -57,5 +57,16 @@ RSpec.describe FiltersHelper do
expect(filter_selected?("organisation_select", :specific_org)).to be false expect(filter_selected?("organisation_select", :specific_org)).to be false
end end
end end
context "when the specific organisation filter is not set" do
before do
session[:case_logs_filters] = { "status" => [""], "years" => [""], "user" => "all" }.to_json
end
it "marks the all options as checked" do
expect(filter_selected?("organisation_select", :all)).to be true
expect(filter_selected?("organisation_select", :specific_org)).to be false
end
end
end end
end end

4
spec/models/user_spec.rb

@ -99,7 +99,7 @@ RSpec.describe User, type: :model do
end end
it "can filter case logs by user, year and status" do it "can filter case logs by user, year and status" do
expect(user.case_logs_filters).to eq(%i[status years user]) expect(user.case_logs_filters).to eq(%w[status years user])
end end
end end
@ -125,7 +125,7 @@ RSpec.describe User, type: :model do
end end
it "can filter case logs by user, year, status and organisation" do it "can filter case logs by user, year, status and organisation" do
expect(user.case_logs_filters).to eq(%i[status years user organisation]) expect(user.case_logs_filters).to eq(%w[status years user organisation])
end end
end end
end end

8
spec/requests/case_logs_controller_spec.rb

@ -392,6 +392,14 @@ RSpec.describe CaseLogsController, type: :request do
expect(page).not_to have_content(tenant_code_2) expect(page).not_to have_content(tenant_code_2)
end end
end end
context "when the support user has filtered by organisation, then switches back to all organisations" do
it "shows all organisations" do
get "http://localhost:3000/logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&organisation_select=all&organisation=#{org_1.id}", headers:, params: {}
expect(page).to have_content(tenant_code_1)
expect(page).to have_content(tenant_code_2)
end
end
end end
context "when there are more than 20 logs" do context "when there are more than 20 logs" do

Loading…
Cancel
Save