diff --git a/app/lib/filters/action_required_by.rb b/app/lib/filters/action_required_by.rb new file mode 100644 index 0000000000..c0a8163c6b --- /dev/null +++ b/app/lib/filters/action_required_by.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class Filters::ActionRequiredBy < Filters::Base + def apply + return scope if action_required_by.empty? + + scope.where(action_required_by:) + end + + def action_required_by + Array(params[:action_required_by]).compact_blank + end +end diff --git a/spec/lib/filters/action_required_by_spec.rb b/spec/lib/filters/action_required_by_spec.rb new file mode 100644 index 0000000000..0cd2f274e3 --- /dev/null +++ b/spec/lib/filters/action_required_by_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Filters::ActionRequiredBy do + subject(:filtered_scope) { described_class.apply(scope:, params:) } + + context "the params includes a action_required_by" do + let(:params) { { action_required_by: "assessor" } } + let(:scope) { ApplicationForm.all } + + let!(:included) { create(:application_form, :action_required_by_assessor) } + let!(:excluded) { create(:application_form, :action_required_by_admin) } + + it { is_expected.to contain_exactly(included) } + end + + context "the params include multiple action_required_by" do + let(:params) { { action_required_by: %w[assessor external] } } + let(:scope) { ApplicationForm.all } + + let!(:included) { create(:application_form, :action_required_by_assessor) } + let!(:excluded) { create(:application_form, :action_required_by_admin) } + + it { is_expected.to contain_exactly(included) } + end + + context "the params don't include action_required_by" do + let(:params) { {} } + let(:scope) { double } + + it { is_expected.to eq(scope) } + end +end