Browse Source

Add a filter for status

pull/449/head
Kat 3 years ago
parent
commit
08ec80d6da
  1. 15
      app/controllers/case_logs_controller.rb
  2. 8
      app/helpers/filters_helper.rb
  3. 1
      app/models/case_log.rb
  4. 27
      app/views/case_logs/_log_list.html.erb
  5. 2
      config/routes.rb
  6. 26
      spec/helpers/filters_helper_spec.rb
  7. 47
      spec/requests/case_logs_controller_spec.rb

15
app/controllers/case_logs_controller.rb

@ -7,7 +7,7 @@ class CaseLogsController < ApplicationController
before_action :find_resource, except: %i[create index edit] before_action :find_resource, except: %i[create index edit]
def index def index
@pagy, @case_logs = pagy(current_user.case_logs) @pagy, @case_logs = pagy(filtered_case_logs)
respond_to do |format| respond_to do |format|
format.html format.html
@ -78,6 +78,11 @@ class CaseLogsController < ApplicationController
end end
end end
def filter
cookies[:case_logs_filters] = { status: params[:status] }.to_json
redirect_back(fallback_location: root_path)
end
private private
API_ACTIONS = %w[create show update destroy].freeze API_ACTIONS = %w[create show update destroy].freeze
@ -117,4 +122,12 @@ private
def find_resource def find_resource
@case_log = CaseLog.find_by(id: params[:id]) @case_log = CaseLog.find_by(id: params[:id])
end end
def filtered_case_logs
user_case_logs = current_user.case_logs
status_filter = JSON.parse(cookies[:case_logs_filters])["status"] if cookies[:case_logs_filters].present?
return user_case_logs unless status_filter
user_case_logs.filter_by_status(status_filter)
end
end end

8
app/helpers/filters_helper.rb

@ -0,0 +1,8 @@
module FiltersHelper
def filter_selected?(filter)
return true unless cookies[:case_logs_filters]
selected_filters = JSON.parse(cookies[:case_logs_filters])
selected_filters["status"].present? && selected_filters["status"].include?(filter.to_s)
end
end

1
app/models/case_log.rb

@ -34,6 +34,7 @@ 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 }
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

27
app/views/case_logs/_log_list.html.erb

@ -1,3 +1,28 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-one-quarter">
<div class="app-filter">
<div class="app-filter__header">
<h2 class="govuk-heading-m">Filters</h2>
</div>
<div class="app-filter__content">
<div class="govuk-form-group app-filter__group">
<%= form_with url: "/logs/filter", html: { method: :post }, builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_check_boxes_fieldset :status do %>
<% statuses = {in_progress: "In progress", submitted: "Completed", archived: "Not started"} %>
<% statuses.map do |key, option| %>
<%= f.govuk_check_box "status", "#{key}",
label: { text: option },
checked: filter_selected?(key),
class: "govuk-checkboxes govuk-checkboxes--small" %>
<% end %>
<% end %>
<%= f.govuk_submit "Apply filters"%>
<% end %>
</div>
</div>
</div>
</div>
<div class="govuk-grid-column-three-quarters">
<figure class="app-figure"> <figure class="app-figure">
<figcaption id="<%= title.dasherize %>" class="app-figure__caption"> <figcaption id="<%= title.dasherize %>" class="app-figure__caption">
<span class="govuk-!-margin-right-4"> <span class="govuk-!-margin-right-4">
@ -59,3 +84,5 @@
</table> </table>
</section> </section>
</figure> </figure>
</div>
</div>

2
config/routes.rb

@ -90,6 +90,8 @@ Rails.application.routes.draw do
end end
end end
post "/logs/filter", to: "case_logs#filter"
scope via: :all do scope via: :all do
match "/404", to: "errors#not_found" match "/404", to: "errors#not_found"
match "/429", to: "errors#too_many_requests", status: 429 match "/429", to: "errors#too_many_requests", status: 429

26
spec/helpers/filters_helper_spec.rb

@ -0,0 +1,26 @@
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
end
end
context "one filter is selected" do
before do
cookies[:case_logs_filters] = { "status": "in_progress" }.to_json
end
it "returns false for non selected filters" do
expect(filter_selected?("completed")).to be_falsey
end
it "returns true for selected filter" do
expect(filter_selected?("in_progress")).to be_truthy
end
end
end
end

47
spec/requests/case_logs_controller_spec.rb

@ -172,6 +172,34 @@ RSpec.describe CaseLogsController, type: :request do
expect(page).to have_content("Owning organisation") expect(page).to have_content("Owning organisation")
expect(page).to have_content("Managing organisation") expect(page).to have_content("Managing organisation")
end 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
it "only shows case logs for selected status" do
in_progress_case_row_log = "<a class=\"govuk-link\" href=\"/logs/#{in_progress_case_log.id}\">#{in_progress_case_log.id}</a>"
completed_case_row_log = "<a class=\"govuk-link\" href=\"/logs/#{completed_case_log.id}\">#{completed_case_log.id}</a>"
cookies[:case_logs_filters] = { status: %w[in_progress completed] }.to_json
get "/logs", headers: headers, params: {}
expect(CGI.unescape_html(response.body)).to include(in_progress_case_row_log)
expect(CGI.unescape_html(response.body)).to include(completed_case_row_log)
cookies[:case_logs_filters] = { status: %w[in_progress] }.to_json
get "/logs", headers: headers, params: {}
expect(CGI.unescape_html(response.body)).to include(in_progress_case_row_log)
expect(CGI.unescape_html(response.body)).not_to include(completed_case_row_log)
end
end
end end
context "when the user is not a customer support user" do context "when the user is not a customer support user" do
@ -626,4 +654,23 @@ RSpec.describe CaseLogsController, type: :request do
end end
end end
end end
describe "POST #filter" do
let(:user) { FactoryBot.create(:user) }
let(:params) do
{
"status": ["In progress"],
}
end
before do
sign_in user
end
it "sets status filter in the cookies" do
expect(cookies[:case_logs_filters]).to be_nil
post "/logs/filter", headers: headers, params: params.to_json
expect(JSON.parse(cookies[:case_logs_filters])["status"]).to eq(["In progress"])
end
end
end end

Loading…
Cancel
Save