Skip to content

Commit

Permalink
Add Filters::Stage
Browse files Browse the repository at this point in the history
This adds a filter for selecting application forms according to their
stage.
  • Loading branch information
thomasleese committed Sep 24, 2023
1 parent e066920 commit e710cbb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/lib/filters/stage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class Filters::Stage < Filters::Base
def apply
return scope if stage.empty?

scope.where(stage:)
end

private

def stage
Array(params[:stage]).compact_blank
end
end
34 changes: 34 additions & 0 deletions spec/lib/filters/stage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Filters::Stage do
subject(:filtered_scope) { described_class.apply(scope:, params:) }

context "the params includes a stage" do
let(:params) { { stage: "not_started" } }
let(:scope) { ApplicationForm.all }

let!(:included) { create(:application_form, :not_started_stage) }
let!(:excluded) { create(:application_form, :assessment_stage) }

it { is_expected.to contain_exactly(included) }
end

context "the params include multiple stage" do
let(:params) { { stage: %w[not_started assessment] } }
let(:scope) { ApplicationForm.all }

let!(:included) { create(:application_form, :not_started_stage) }
let!(:excluded) { create(:application_form, :completed_stage) }

it { is_expected.to contain_exactly(included) }
end

context "the params don't include stage" do
let(:params) { {} }
let(:scope) { double }

it { is_expected.to eq(scope) }
end
end

0 comments on commit e710cbb

Please sign in to comment.