Skip to content

Commit

Permalink
Refactor petition creation
Browse files Browse the repository at this point in the history
Remove the stage manager class and replace it with a simpler
form/service object based on Active Model. This allows us to
flatten the parameter hash and control when and what is
validated much easier than using the Active Record classes.
  • Loading branch information
pixeltrix committed Sep 12, 2017
1 parent a11b37b commit b8f1314
Show file tree
Hide file tree
Showing 48 changed files with 580 additions and 1,318 deletions.
78 changes: 18 additions & 60 deletions app/controllers/petitions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'csv'

class PetitionsController < ApplicationController
include ManagingMoveParameter

before_action :redirect_to_valid_state, only: [:index]
before_action :do_not_cache, except: [:index, :show]

Expand All @@ -12,6 +10,8 @@ class PetitionsController < ApplicationController

before_action :retrieve_petitions, only: [:index]
before_action :retrieve_petition, only: [:show, :count, :gathering_support, :moderation_info]
before_action :build_petition_creator, only: [:check, :check_results, :new, :create]

before_action :redirect_to_stopped_page, if: :stopped?, only: [:moderation_info, :show]
before_action :redirect_to_gathering_support_url, if: :collecting_sponsors?, only: [:moderation_info, :show]
before_action :redirect_to_moderation_info_url, if: :in_moderation?, only: [:gathering_support, :show]
Expand Down Expand Up @@ -41,41 +41,31 @@ def count
end
end

def new
assign_action
assign_stage
@stage_manager = Staged::PetitionCreator.manager(petition_params_for_new, request, params[:stage], params[:move])

def check
respond_to do |format|
format.html
end
end

def create
assign_move
assign_stage
@stage_manager = Staged::PetitionCreator.manager(petition_params_for_create, request, params[:stage], params[:move])
if @stage_manager.create_petition
@stage_manager.petition.creator.store_constituency_id
send_email_to_gather_sponsors(@stage_manager.petition)
redirect_to thank_you_petition_url(@stage_manager.petition)
else
respond_to do |format|
format.html { render :new }
end
def check_results
respond_to do |format|
format.html
end
end

def check
def new
respond_to do |format|
format.html
end
end

def check_results
@petitions = Petition.current.search(params.merge(count: 3))
respond_to do |format|
format.html
def create
if @new_petition.save
redirect_to thank_you_petition_url(@new_petition)
else
respond_to do |format|
format.html { render :new }
end
end
end

Expand Down Expand Up @@ -119,6 +109,10 @@ def retrieve_petition
@petition = Petition.show.find(petition_id)
end

def build_petition_creator
@new_petition = PetitionCreator.new(params, request)
end

def redirect_to_valid_state
if state_present? && !valid_state?
redirect_to petitions_url(search_params(state: :all))
Expand Down Expand Up @@ -169,42 +163,6 @@ def redirect_to_petition_url
redirect_to petition_url(@petition)
end

def petition_params_for_new
params.
fetch('petition', {}).
permit(:action)
end

def petition_params_for_create
params.
require(:petition).
permit(:action, :background, :additional_details, :duration,
creator: [
:name, :email, :email_confirmation,
:postcode, :location_code, :uk_citizenship
]).tap do |sanitized|
if sanitized['creator'].present?
sanitized['creator_attributes'] = sanitized.delete('creator')
end
end
end

def assign_action
return if params[:petition_action].blank?
petition_action = params.delete(:petition_action)
params[:petition] ||= {}
params[:petition][:action] = petition_action
end

def assign_stage
return if Staged::PetitionCreator.stages.include? params[:stage]
params[:stage] = 'petition'
end

def send_email_to_gather_sponsors(petition)
GatherSponsorsForPetitionEmailJob.perform_later(petition)
end

def csv_filename
"#{@petitions.scope}-petitions.csv"
end
Expand Down
36 changes: 0 additions & 36 deletions app/helpers/petition_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
module PetitionHelper
def render_petition_form(stage_manager, form)
capture do
concat render_petition_hidden_details(stage_manager, form)
concat render_petition_ui(stage_manager, form)
end
end

def public_petition_facets_with_counts(petition_search)
petition_search.facets.slice(*public_petition_facets)
end
Expand Down Expand Up @@ -40,33 +33,4 @@ def petition_list_header?
def reveal_government_response?
params[:reveal_response] == "yes"
end

private

def render_petition_hidden_details(stage_manager, form)
capture do
concat hidden_field_tag(:stage, stage_manager.stage)
concat hidden_field_tag(:move, 'next')
concat render('/petitions/create/petition_details_hidden', petition: stage_manager.stage_object, f: form) unless stage_manager.stage == 'petition'
if stage_manager.stage_object.creator.present?
concat render('/petitions/create/your_details_hidden', petition: stage_manager.stage_object, f: form) unless stage_manager.stage == 'creator'
concat render('/petitions/create/email_hidden', petition: stage_manager.stage_object, f: form) unless ['creator', 'replay-email'].include? stage_manager.stage
end
end
end

def render_petition_ui(stage_manager, form)
# NOTE: make sure we skip past the existing tabindex-ed elements on the page, no matter which ui we render
increment(4)
case stage_manager.stage
when 'petition'
render('/petitions/create/petition_details_ui', petition: stage_manager.stage_object, f: form)
when 'creator'
render('/petitions/create/your_details_ui', petition: stage_manager.stage_object, f: form)
when 'replay-petition'
render('/petitions/create/replay_petition_ui', petition: stage_manager.stage_object, f: form)
when 'replay-email'
render('/petitions/create/replay_email_ui', petition: stage_manager.stage_object, f: form)
end
end
end
Loading

0 comments on commit b8f1314

Please sign in to comment.