Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#36, #37] Rework routing and use session to store current active request V2 #64

Merged
merged 12 commits into from
May 1, 2018
26 changes: 19 additions & 7 deletions app/controllers/concerns/findable_foi_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@ module FindableFoiRequest
extend ActiveSupport::Concern

included do
before_action :set_foi_request, :redirect_if_missing_request

private

def set_foi_request
@foi_request = FoiRequest.
includes(:contact).
references(:contact).
find_by(id: params.require(:request_id))
def find_foi_request
@foi_request = foi_request_from_session(scope: FoiRequest.unqueued)
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).
references(:contact).
find_by(id: session[:request_id])
end

def redirect_if_missing_request
return if @foi_request
redirect_to new_foi_request_path
end

def store_foi_request_in_session
session[:request_id] = @foi_request.id
end
end
end
7 changes: 4 additions & 3 deletions app/controllers/foi/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Foi
class ContactsController < ApplicationController
include FindableFoiRequest

before_action :find_foi_request
before_action :redirect_if_exisiting_contact, only: %i[new create]
before_action :new_contact, only: %i[new create]
before_action :find_contact, only: %i[edit update]
Expand All @@ -16,7 +17,7 @@ def new; end

def create
if @contact.update(contact_params)
redirect_to foi_request_preview_path(@foi_request)
redirect_to preview_foi_request_path
else
render :new
end
Expand All @@ -26,7 +27,7 @@ def edit; end

def update
if @contact.update(contact_params)
redirect_to foi_request_preview_path(@foi_request)
redirect_to preview_foi_request_path
else
render :edit
end
Expand All @@ -36,7 +37,7 @@ def update

def redirect_if_exisiting_contact
return unless @foi_request.contact
redirect_to edit_foi_request_contact_path(@foi_request)
redirect_to edit_foi_request_contact_path
end

def new_contact
Expand Down
11 changes: 5 additions & 6 deletions app/controllers/foi/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Foi
# This controller is responsible for creating and updating of FOI requests.
#
class RequestsController < ApplicationController
include FindableFoiRequest

before_action :new_foi_request, only: %i[new create]
before_action :find_foi_request, only: %i[edit update]

Expand All @@ -14,7 +16,8 @@ def new; end

def create
if @foi_request.update(foi_request_params)
redirect_to foi_request_suggestions_path(@foi_request)
store_foi_request_in_session
redirect_to foi_request_suggestions_path
else
render :new
end
Expand All @@ -24,7 +27,7 @@ def edit; end

def update
if @foi_request.update(foi_request_params)
redirect_to foi_request_suggestions_path(@foi_request)
redirect_to foi_request_suggestions_path
else
render :edit
end
Expand All @@ -36,10 +39,6 @@ def new_foi_request
@foi_request = FoiRequest.new
end

def find_foi_request
@foi_request = FoiRequest.find(params.require(:id))
end

def foi_request_params
params.require(:foi_request).permit(:body)
end
Expand Down
20 changes: 18 additions & 2 deletions app/controllers/foi/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ module Foi
class SubmissionsController < ApplicationController
include FindableFoiRequest

before_action :find_foi_request, only: %i[new create]
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]

def new; end

def create
redirect_to foi_request_sent_path(@foi_request)
if @submission.queue
redirect_to sent_foi_request_path
else
render :new
end
end

def show; end
Expand All @@ -22,7 +30,15 @@ def show; end

def redirect_if_missing_contact
return if @foi_request.contact
redirect_to new_foi_request_contact_path(@foi_request)
redirect_to new_foi_request_contact_path
end

def new_submission
@submission = @foi_request.build_submission
end

def find_submission
@submission = @foi_request.submission
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/foi/suggestions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Foi
class SuggestionsController < ApplicationController
include FindableFoiRequest

before_action :find_foi_request

def index; end
end
end
10 changes: 10 additions & 0 deletions app/models/foi_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ class FoiRequest < ApplicationRecord
belongs_to :submission, optional: true, dependent: :destroy

validates :body, presence: true

scope :unqueued, lambda {
left_joins(:submission).
where(submissions: { state: [nil, Submission::UNQUEUED] })
}

scope :queued, lambda {
left_joins(:submission).
where.not(submissions: { state: [nil, Submission::UNQUEUED] })
}
end
7 changes: 7 additions & 0 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
# software.
#
class Submission < ApplicationRecord
UNQUEUED = 'unqueued'
QUEUED = 'queued'

has_one :foi_request, dependent: :destroy

validates :state, presence: true

def queue
update(state: QUEUED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have some unit tests

end
end
4 changes: 2 additions & 2 deletions app/views/foi/submissions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
</aside>

<div class="cta-section">
<%= link_to 'Edit your request', edit_foi_request_path(@foi_request), class: 'button button-secondary' %>
<%= form_for :submission, url: foi_request_send_path(@foi_request) do |f| %>
<%= link_to 'Edit your request', edit_foi_request_path, class: 'button button-secondary' %>
<%= form_for :submission, url: send_foi_request_path do |f| %>
<%= f.submit 'Send now', class: 'button' %>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/foi/suggestions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
</p>

<p class="cta-section">
<%= link_to 'Continue with request', new_foi_request_contact_path(@foi_request), class: 'button' %>
<%= link_to 'Continue with request', new_foi_request_contact_path, class: 'button' %>
</p>
11 changes: 7 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# frozen_string_literal: true

Rails.application.routes.draw do
root to: redirect('foi/requests')
root to: redirect('foi')

namespace :foi do
root to: redirect('foi/requests')
resources :requests, except: %i[show destroy] do
root to: 'requests#index'
resource :request, except: %i[show destroy] do
root to: redirect('foi/request/new')
resources :suggestions, only: %i[index]
get 'contact', to: redirect('foi/requests/%{request_id}/contact/new')
get 'contact', to: redirect('foi/request/contact/new')
resource :contact, except: %i[show destroy]
get 'preview', to: 'submissions#new', as: 'preview'
post 'send', to: 'submissions#create', as: 'send'
get 'sent', to: 'submissions#show', as: 'sent'
end
end

resolve('FoiRequest') { %i[foi request] }
end
8 changes: 5 additions & 3 deletions spec/controllers/concerns/findable_foi_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
controller(ApplicationController) do
include FindableFoiRequest

before_action :find_foi_request

def index
redirect_to edit_foi_request_path(@foi_request)
redirect_to edit_foi_request_path
end
end

describe 'GET #index' do
subject { get :index, params: { request_id: '1' } }
subject { get :index, session: { request_id: '1' } }

context 'with foi_request' do
it 'finds FOI request and sets instance variable' do
expect(foi_request_scope).to receive(:find_by).
with(id: '1').and_return(foi_request)
is_expected.to redirect_to(edit_foi_request_path(foi_request))
is_expected.to redirect_to(edit_foi_request_path)
end
end

Expand Down
28 changes: 15 additions & 13 deletions spec/controllers/foi/contacts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
end

describe 'GET #new' do
subject { get :new, params: { request_id: '1' } }
subject { get :new, session: { request_id: '1' } }
before { allow(foi_request).to receive(:contact).and_return(nil) }

context 'existing contact' do
before { allow(foi_request).to receive(:contact).and_return(double) }

it 'redirects to edit contact' do
is_expected.to redirect_to(edit_foi_request_contact_path(foi_request))
is_expected.to redirect_to(edit_foi_request_contact_path)
end
end

Expand All @@ -41,9 +41,7 @@
end

context 'existing contact' do
subject do
post :create, params: { request_id: '1' }
end
subject { post :create, session: { request_id: '1' } }
before { allow(foi_request).to receive(:contact).and_return(double) }

it 'does not receive attributes' do
Expand All @@ -52,13 +50,14 @@
end

it 'redirects to edit contact' do
is_expected.to redirect_to(edit_foi_request_contact_path(foi_request))
is_expected.to redirect_to(edit_foi_request_contact_path)
end
end

context 'valid parameters' do
subject do
post :create, params: { request_id: '1', contact: valid_params }
post :create, params: { contact: valid_params },
session: { request_id: '1' }
end
before { allow(contact).to receive(:update).and_return(true) }

Expand All @@ -69,13 +68,14 @@
end

it 'redirects to foi_request' do
is_expected.to redirect_to(foi_request_preview_path(foi_request))
is_expected.to redirect_to(preview_foi_request_path)
end
end

context 'invalid parameters' do
subject do
post :create, params: { request_id: '1', contact: invalid_params }
post :create, params: { contact: invalid_params },
session: { request_id: '1' }
end
before { allow(contact).to receive(:update).and_return(false) }

Expand All @@ -86,7 +86,7 @@
end

describe 'GET #edit' do
subject { get :edit, params: { request_id: '1' } }
subject { get :edit, session: { request_id: '1' } }

it 'returns http success' do
is_expected.to have_http_status(200)
Expand All @@ -98,7 +98,8 @@

context 'valid parameters' do
subject do
put :update, params: { request_id: '1', contact: valid_params }
put :update, params: { contact: valid_params },
session: { request_id: '1' }
end
before { allow(contact).to receive(:update).and_return(true) }

Expand All @@ -109,13 +110,14 @@
end

it 'redirects to foi_request' do
is_expected.to redirect_to(foi_request_preview_path(foi_request))
is_expected.to redirect_to(preview_foi_request_path)
end
end

context 'invalid parameters' do
subject do
put :update, params: { request_id: '1', contact: invalid_params }
put :update, params: { contact: invalid_params },
session: { request_id: '1' }
end
before { allow(contact).to receive(:update).and_return(false) }

Expand Down
Loading