diff --git a/app/controllers/concerns/findable_foi_request.rb b/app/controllers/concerns/findable_foi_request.rb index b59b65a6..cc98cef6 100644 --- a/app/controllers/concerns/findable_foi_request.rb +++ b/app/controllers/concerns/findable_foi_request.rb @@ -16,6 +16,11 @@ def find_foi_request redirect_if_missing_request end + def find_queued_foi_request + @foi_request = foi_request_from_session(scope: FoiRequest.queued) + redirect_if_missing_request + end + def foi_request_from_session(scope: FoiRequest) scope. includes(:contact). diff --git a/app/controllers/foi/submissions_controller.rb b/app/controllers/foi/submissions_controller.rb index 724260c3..a439b5c4 100644 --- a/app/controllers/foi/submissions_controller.rb +++ b/app/controllers/foi/submissions_controller.rb @@ -9,7 +9,7 @@ class SubmissionsController < ApplicationController include FindableFoiRequest before_action :find_foi_request, only: %i[new create] - before_action :find_foi_request_from_session, only: %i[show] + before_action :find_queued_foi_request, only: %i[show] before_action :redirect_if_missing_contact before_action :new_submission, only: %i[new create] before_action :find_submission, only: %i[show] @@ -28,10 +28,6 @@ def show; end private - def find_foi_request_from_session - @foi_request = foi_request_from_session - end - def redirect_if_missing_contact return if @foi_request.contact redirect_to new_foi_request_contact_path diff --git a/app/models/foi_request.rb b/app/models/foi_request.rb index 8355f387..4ab4a6b3 100644 --- a/app/models/foi_request.rb +++ b/app/models/foi_request.rb @@ -13,4 +13,9 @@ class FoiRequest < ApplicationRecord left_joins(:submission). where(submissions: { state: [nil, Submission::UNQUEUED] }) } + + scope :queued, lambda { + left_joins(:submission). + where.not(submissions: { state: [nil, Submission::UNQUEUED] }) + } end diff --git a/spec/controllers/foi/submissions_controller_spec.rb b/spec/controllers/foi/submissions_controller_spec.rb index b5e5df77..9cafdfda 100644 --- a/spec/controllers/foi/submissions_controller_spec.rb +++ b/spec/controllers/foi/submissions_controller_spec.rb @@ -66,12 +66,6 @@ describe 'GET #show' do subject { get :show, session: { request_id: '1' } } - before do - # temporary - allow(FoiRequest).to receive(:includes). - with(:contact).and_return(foi_request_scope) - end - include_examples 'redirect if missing contact' it 'returns http success' do diff --git a/spec/factories/foi_requests.rb b/spec/factories/foi_requests.rb index 10437607..82d1bd49 100644 --- a/spec/factories/foi_requests.rb +++ b/spec/factories/foi_requests.rb @@ -8,5 +8,9 @@ trait :unqueued do association :submission, :unqueued end + + trait :queued do + association :submission, :queued + end end end diff --git a/spec/factories/submissions.rb b/spec/factories/submissions.rb index 1d8429b8..a9f1ea44 100644 --- a/spec/factories/submissions.rb +++ b/spec/factories/submissions.rb @@ -5,5 +5,9 @@ trait :unqueued do state Submission::UNQUEUED end + + trait :queued do + state Submission::QUEUED + end end end diff --git a/spec/models/foi_request_spec.rb b/spec/models/foi_request_spec.rb index 2f242eb2..5923e356 100644 --- a/spec/models/foi_request_spec.rb +++ b/spec/models/foi_request_spec.rb @@ -36,10 +36,16 @@ describe 'scopes' do let!(:pending) { create(:foi_request) } let!(:unqueued) { create(:foi_request, :unqueued) } + let!(:queued) { create(:foi_request, :queued) } describe '.unqueued' do subject { FoiRequest.unqueued } it { is_expected.to match [pending, unqueued] } end + + describe '.queued' do + subject { FoiRequest.queued } + it { is_expected.to match [queued] } + end end end diff --git a/spec/support/context.rb b/spec/support/context.rb index b3796fe5..4e896094 100644 --- a/spec/support/context.rb +++ b/spec/support/context.rb @@ -11,6 +11,9 @@ before do allow(FoiRequest).to receive(:unqueued). and_return(foi_request_scope) + allow(FoiRequest).to receive(:queued). + and_return(foi_request_scope) + allow(foi_request_scope).to receive(:includes). with(:contact).and_return(foi_request_scope) allow(foi_request_scope).to receive(:references).