11require 'csv'
22
33class PetitionsController < ApplicationController
4- include ManagingMoveParameter
5-
64 before_action :redirect_to_valid_state , only : [ :index ]
75 before_action :do_not_cache , except : [ :index , :show ]
86
@@ -12,6 +10,8 @@ class PetitionsController < ApplicationController
1210
1311 before_action :retrieve_petitions , only : [ :index ]
1412 before_action :retrieve_petition , only : [ :show , :count , :gathering_support , :moderation_info ]
13+ before_action :build_petition_creator , only : [ :check , :check_results , :new , :create ]
14+
1515 before_action :redirect_to_stopped_page , if : :stopped? , only : [ :moderation_info , :show ]
1616 before_action :redirect_to_gathering_support_url , if : :collecting_sponsors? , only : [ :moderation_info , :show ]
1717 before_action :redirect_to_moderation_info_url , if : :in_moderation? , only : [ :gathering_support , :show ]
@@ -41,41 +41,31 @@ def count
4141 end
4242 end
4343
44- def new
45- assign_action
46- assign_stage
47- @stage_manager = Staged ::PetitionCreator . manager ( petition_params_for_new , request , params [ :stage ] , params [ :move ] )
48-
44+ def check
4945 respond_to do |format |
5046 format . html
5147 end
5248 end
5349
54- def create
55- assign_move
56- assign_stage
57- @stage_manager = Staged ::PetitionCreator . manager ( petition_params_for_create , request , params [ :stage ] , params [ :move ] )
58- if @stage_manager . create_petition
59- @stage_manager . petition . creator . store_constituency_id
60- send_email_to_gather_sponsors ( @stage_manager . petition )
61- redirect_to thank_you_petition_url ( @stage_manager . petition )
62- else
63- respond_to do |format |
64- format . html { render :new }
65- end
50+ def check_results
51+ respond_to do |format |
52+ format . html
6653 end
6754 end
6855
69- def check
56+ def new
7057 respond_to do |format |
7158 format . html
7259 end
7360 end
7461
75- def check_results
76- @petitions = Petition . current . search ( params . merge ( count : 3 ) )
77- respond_to do |format |
78- format . html
62+ def create
63+ if @new_petition . save
64+ redirect_to thank_you_petition_url ( @new_petition )
65+ else
66+ respond_to do |format |
67+ format . html { render :new }
68+ end
7969 end
8070 end
8171
@@ -119,6 +109,10 @@ def retrieve_petition
119109 @petition = Petition . show . find ( petition_id )
120110 end
121111
112+ def build_petition_creator
113+ @new_petition = PetitionCreator . new ( params , request )
114+ end
115+
122116 def redirect_to_valid_state
123117 if state_present? && !valid_state?
124118 redirect_to petitions_url ( search_params ( state : :all ) )
@@ -169,42 +163,6 @@ def redirect_to_petition_url
169163 redirect_to petition_url ( @petition )
170164 end
171165
172- def petition_params_for_new
173- params .
174- fetch ( 'petition' , { } ) .
175- permit ( :action )
176- end
177-
178- def petition_params_for_create
179- params .
180- require ( :petition ) .
181- permit ( :action , :background , :additional_details , :duration ,
182- creator : [
183- :name , :email , :email_confirmation ,
184- :postcode , :location_code , :uk_citizenship
185- ] ) . tap do |sanitized |
186- if sanitized [ 'creator' ] . present?
187- sanitized [ 'creator_attributes' ] = sanitized . delete ( 'creator' )
188- end
189- end
190- end
191-
192- def assign_action
193- return if params [ :petition_action ] . blank?
194- petition_action = params . delete ( :petition_action )
195- params [ :petition ] ||= { }
196- params [ :petition ] [ :action ] = petition_action
197- end
198-
199- def assign_stage
200- return if Staged ::PetitionCreator . stages . include? params [ :stage ]
201- params [ :stage ] = 'petition'
202- end
203-
204- def send_email_to_gather_sponsors ( petition )
205- GatherSponsorsForPetitionEmailJob . perform_later ( petition )
206- end
207-
208166 def csv_filename
209167 "#{ @petitions . scope } -petitions.csv"
210168 end
0 commit comments