From b8f131442cb0d7e2ea46615786b44919c39e4425 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 12 Sep 2017 05:42:27 +0100 Subject: [PATCH] Refactor petition creation 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. --- app/controllers/petitions_controller.rb | 78 +---- app/helpers/petition_helper.rb | 36 -- app/models/petition_creator.rb | 230 +++++++++++++ app/models/staged/base/creator_signature.rb | 31 -- app/models/staged/base/petition.rb | 17 - app/models/staged/base/signature.rb | 22 -- app/models/staged/petition_creator.rb | 62 ---- app/models/staged/petition_creator/creator.rb | 13 - .../petition_creator/has_creator_signature.rb | 30 -- .../staged/petition_creator/petition.rb | 7 - .../staged/petition_creator/replay_email.rb | 11 - .../petition_creator/replay_petition.rb | 6 - app/models/staged/petition_creator/stages.rb | 78 ----- app/models/staged/petition_signer.rb | 61 ---- .../staged/petition_signer/replay_email.rb | 7 - app/models/staged/petition_signer/signer.rb | 8 - app/models/staged/petition_signer/stages.rb | 54 --- app/models/staged/petition_sponsor.rb | 24 -- app/models/staged/stage.rb | 26 -- app/models/staged/stage_manager.rb | 42 --- app/models/staged/validations/email.rb | 11 - .../staged/validations/petition_details.rb | 13 - .../staged/validations/signer_details.rb | 20 -- app/validators/email_validator.rb | 2 +- app/views/petitions/check.html.erb | 4 +- app/views/petitions/check_results.html.erb | 36 +- .../petitions/create/_creator_stage.html.erb | 54 +++ .../petitions/create/_email_hidden.html.erb | 3 - .../petitions/create/_error_summary.html.erb | 6 +- .../create/_petition_details_hidden.html.erb | 3 - .../create/_petition_details_ui.html.erb | 43 --- .../petitions/create/_petition_stage.html.erb | 43 +++ .../create/_replay_email_stage.html.erb | 22 ++ .../create/_replay_email_ui.html.erb | 11 - ...ml.erb => _replay_petition_stage.html.erb} | 14 +- .../create/_signature_email.html.erb | 5 - .../petitions/create/_signature_form.html.erb | 37 -- .../create/_signature_hidden.html.erb | 4 - .../create/_your_details_hidden.html.erb | 3 - .../create/_your_details_ui.html.erb | 18 - app/views/petitions/new.html.erb | 5 +- .../resend_confirmation_email.html.erb | 10 - .../_results_for_pre_create_petition.html.erb | 11 - features/step_definitions/petition_steps.rb | 2 +- spec/controllers/petitions_controller_spec.rb | 323 +++++++++--------- spec/models/staged/petition_creator_spec.rb | 194 ----------- spec/models/staged/petition_signer_spec.rb | 133 -------- spec/support/staged_object_helpers.rb | 25 -- 48 files changed, 580 insertions(+), 1318 deletions(-) create mode 100644 app/models/petition_creator.rb delete mode 100644 app/models/staged/base/creator_signature.rb delete mode 100644 app/models/staged/base/petition.rb delete mode 100644 app/models/staged/base/signature.rb delete mode 100644 app/models/staged/petition_creator.rb delete mode 100644 app/models/staged/petition_creator/creator.rb delete mode 100644 app/models/staged/petition_creator/has_creator_signature.rb delete mode 100644 app/models/staged/petition_creator/petition.rb delete mode 100644 app/models/staged/petition_creator/replay_email.rb delete mode 100644 app/models/staged/petition_creator/replay_petition.rb delete mode 100644 app/models/staged/petition_creator/stages.rb delete mode 100644 app/models/staged/petition_signer.rb delete mode 100644 app/models/staged/petition_signer/replay_email.rb delete mode 100644 app/models/staged/petition_signer/signer.rb delete mode 100644 app/models/staged/petition_signer/stages.rb delete mode 100644 app/models/staged/petition_sponsor.rb delete mode 100644 app/models/staged/stage.rb delete mode 100644 app/models/staged/stage_manager.rb delete mode 100644 app/models/staged/validations/email.rb delete mode 100644 app/models/staged/validations/petition_details.rb delete mode 100644 app/models/staged/validations/signer_details.rb create mode 100644 app/views/petitions/create/_creator_stage.html.erb delete mode 100644 app/views/petitions/create/_email_hidden.html.erb delete mode 100644 app/views/petitions/create/_petition_details_hidden.html.erb delete mode 100644 app/views/petitions/create/_petition_details_ui.html.erb create mode 100644 app/views/petitions/create/_petition_stage.html.erb create mode 100644 app/views/petitions/create/_replay_email_stage.html.erb delete mode 100644 app/views/petitions/create/_replay_email_ui.html.erb rename app/views/petitions/create/{_replay_petition_ui.html.erb => _replay_petition_stage.html.erb} (63%) delete mode 100644 app/views/petitions/create/_signature_email.html.erb delete mode 100644 app/views/petitions/create/_signature_form.html.erb delete mode 100644 app/views/petitions/create/_signature_hidden.html.erb delete mode 100644 app/views/petitions/create/_your_details_hidden.html.erb delete mode 100644 app/views/petitions/create/_your_details_ui.html.erb delete mode 100644 app/views/petitions/resend_confirmation_email.html.erb delete mode 100644 app/views/petitions/search/_results_for_pre_create_petition.html.erb delete mode 100644 spec/models/staged/petition_creator_spec.rb delete mode 100644 spec/models/staged/petition_signer_spec.rb delete mode 100644 spec/support/staged_object_helpers.rb diff --git a/app/controllers/petitions_controller.rb b/app/controllers/petitions_controller.rb index e411e50f8..406c1950c 100644 --- a/app/controllers/petitions_controller.rb +++ b/app/controllers/petitions_controller.rb @@ -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] @@ -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] @@ -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 @@ -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)) @@ -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 diff --git a/app/helpers/petition_helper.rb b/app/helpers/petition_helper.rb index 78c578431..0cc1be3c3 100644 --- a/app/helpers/petition_helper.rb +++ b/app/helpers/petition_helper.rb @@ -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 @@ -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 diff --git a/app/models/petition_creator.rb b/app/models/petition_creator.rb new file mode 100644 index 000000000..82088efa2 --- /dev/null +++ b/app/models/petition_creator.rb @@ -0,0 +1,230 @@ +require 'postcode_sanitizer' + +class PetitionCreator + extend ActiveModel::Naming + extend ActiveModel::Translation + include ActiveModel::Conversion + + STAGES = %w[petition replay_petition creator replay_email] + + PETITION_PARAMS = [:action, :background, :additional_details] + SIGNATURE_PARAMS = [:name, :email, :postcode, :location_code, :uk_citizenship] + PERMITTED_PARAMS = [:q, :stage, :move_back, :move_next, petition_creator: PETITION_PARAMS + SIGNATURE_PARAMS] + + attr_reader :params, :errors, :request + + def initialize(params, request) + @params = params.permit(*PERMITTED_PARAMS) + @errors = ActiveModel::Errors.new(self) + @request = request + end + + def read_attribute_for_validation(attribute) + public_send(attribute) + end + + def to_partial_path + "petitions/create/#{stage}_stage" + end + + def duplicates + Petition.current.search(q: action, count: 3).presence + end + + def stage + @stage ||= stage_param.in?(STAGES) ? stage_param : STAGES.first + end + + def save + if moving_backwards? + @stage = previous_stage and return false + end + + unless valid? + return false + end + + if done? + @petition = Petition.new do |p| + p.action = action + p.background = background + p.additional_details = additional_details + + p.build_creator do |c| + c.name = name + c.email = email + c.postcode = postcode + c.location_code = location_code + c.uk_citizenship = uk_citizenship + c.constituency_id = constituency_id + c.ip_address = request.remote_ip + end + end + + @petition.save! + send_email_to_gather_sponsors(@petition) + + return true + else + @stage = next_stage and return false + end + end + + def to_param + if @petition && @petition.persisted? + @petition.to_param + else + raise RuntimeError, "PetitionCreator#to_param called before petition was created" + end + end + + def action + (petition_creator_params[:action] || query_param).to_s.strip + end + + def action? + action.present? + end + + def background + petition_creator_params[:background].to_s.strip + end + + def background? + background.present? + end + + def additional_details + petition_creator_params[:additional_details].to_s.strip + end + + def name + petition_creator_params[:name].to_s.strip + end + + def email + petition_creator_params[:email].to_s.strip + end + + def postcode + PostcodeSanitizer.call(petition_creator_params[:postcode]) + end + + def location_code + petition_creator_params[:location_code] || "GB" + end + + def uk_citizenship + petition_creator_params[:uk_citizenship] || "0" + end + + private + + def query_param + @query_param ||= params[:q].to_s.first(255) + end + + def stage_param + @stage_param ||= params[:stage].to_s + end + + def petition_creator_params + params[:petition_creator] || {} + end + + def moving_backwards? + params.key?(:move_back) + end + + def stage_index + STAGES.index(stage) + end + + def previous_stage + STAGES[[stage_index - 1, 0].max] + end + + def next_stage + STAGES[[stage_index + 1, 3].min] + end + + def validate_petition + errors.add(:action, :blank) unless action.present? + errors.add(:action, :too_long, count: 80) if action.length > 80 + errors.add(:background, :blank) unless background.present? + errors.add(:background, :too_long, count: 300) if background.length > 300 + errors.add(:additional_details, :too_long, count: 800) if additional_details.length > 800 + + if errors.any? + @stage = "petition" + end + end + + def validate_creator + errors.add(:name, :blank) unless name.present? + errors.add(:name, :too_long, count: 255) if action.length > 255 + errors.add(:email, :blank) unless email.present? + errors.add(:location_code, :blank) unless location_code.present? + errors.add(:uk_citizenship, :accepted) unless uk_citizenship == "1" + + if email.present? + email_validator.validate(self) + end + + if location_code == "GB" + errors.add(:postcode, :blank) unless postcode.present? + + if postcode.present? + postcode_validator.validate(self) + end + end + + if replay_email? + @stage = "replay_email" + elsif errors.any? + @stage = "creator" + end + end + + def validate + validate_petition + + if errors.empty? && stage_index > 1 + validate_creator + end + end + + def valid? + errors.clear + validate + errors.empty? + end + + def replay_email? + stage == "replay_email" && errors.keys == [:email] + end + + def done? + stage == "replay_email" + end + + def email_validator + EmailValidator.new(attributes: [:email]) + end + + def postcode_validator + PostcodeValidator.new(attributes: [:postcode]) + end + + def constituency + @constituency ||= Constituency.find_by_postcode(postcode) + end + + def constituency_id + constituency.try(:external_id) + end + + def send_email_to_gather_sponsors(petition) + GatherSponsorsForPetitionEmailJob.perform_later(petition) + end +end diff --git a/app/models/staged/base/creator_signature.rb b/app/models/staged/base/creator_signature.rb deleted file mode 100644 index 8e879e087..000000000 --- a/app/models/staged/base/creator_signature.rb +++ /dev/null @@ -1,31 +0,0 @@ -module Staged - module Base - class CreatorSignature - include ActiveModel::Model - include ActiveModel::Validations::Callbacks - - def initialize(petition) - @petition = petition - end - - delegate :id, :to_param, :model_name, :to_key, :name, - :email, :email?, :uk_citizenship, :postcode, - :location_code, :constituency, to: :creator - - def validation_context - :create - end - - private - - attr_reader :petition - - def creator - if petition.creator.nil? - petition.build_creator(location_code: 'GB') - end - petition.creator - end - end - end -end diff --git a/app/models/staged/base/petition.rb b/app/models/staged/base/petition.rb deleted file mode 100644 index 19d4c383a..000000000 --- a/app/models/staged/base/petition.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Staged - module Base - class Petition - include ActiveModel::Model - include ActiveModel::Validations::Callbacks - - attr_reader :petition - def initialize(petition) - @petition = petition - end - - delegate :id, :to_param, :model_name, :to_key, - :action, :background, :additional_details, - :duration, :creator, to: :petition - end - end -end diff --git a/app/models/staged/base/signature.rb b/app/models/staged/base/signature.rb deleted file mode 100644 index aa89a8d60..000000000 --- a/app/models/staged/base/signature.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Staged - module Base - class Signature - include ActiveModel::Model - include ActiveModel::Validations::Callbacks - - attr_reader :signature - - delegate :id, :to_param, :model_name, :to_key, :new_record?, - :name, :email, :email?, :uk_citizenship, :postcode, - :location_code, :petition_id, to: :signature - - def initialize(signature) - @signature = signature - end - - def validation_context - :create - end - end - end -end diff --git a/app/models/staged/petition_creator.rb b/app/models/staged/petition_creator.rb deleted file mode 100644 index 973d17509..000000000 --- a/app/models/staged/petition_creator.rb +++ /dev/null @@ -1,62 +0,0 @@ -module Staged - module PetitionCreator - def self.manager(params, request, stage, move) - Manager.new(params, request, stage, move, self::Stages) - end - - def self.stages - self::Stages.stage_names - end - - class Manager - def initialize(params, request, stage, move, stages) - @params = params - @request = request - @previous_stage = stage - @move = move - @stages = stages - end - - # This is the stage we came from - the UI elements we showed the user - # that generated these params - attr_reader :previous_stage - attr_reader :move - attr_reader :stages - - def petition - @_petition ||= build_petition - end - - def stage - stage_manager.result_stage.name - end - - def stage_object - stage_manager.result_stage.stage_object - end - - def create_petition - sanitize! - stage_manager.create - end - - private - - def stage_manager - @_stage_manager ||= Staged::StageManager.new(stages, previous_stage, move, petition) - end - - def sanitize! - if petition.creator - petition.creator.email.strip! unless petition.creator.email.blank? - petition.creator.ip_address = @request.remote_ip - end - petition.action.strip! unless petition.action.blank? - end - - def build_petition - ::Petition.new(@params) - end - end - end -end diff --git a/app/models/staged/petition_creator/creator.rb b/app/models/staged/petition_creator/creator.rb deleted file mode 100644 index cf12acce7..000000000 --- a/app/models/staged/petition_creator/creator.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Staged - module PetitionCreator - class Creator < Staged::Base::Petition - include Staged::PetitionCreator::HasCreatorSignature - - class CreatorSignature < Staged::Base::CreatorSignature - include Staged::Validations::SignerDetails - include Staged::Validations::Email - end - end - end -end - diff --git a/app/models/staged/petition_creator/has_creator_signature.rb b/app/models/staged/petition_creator/has_creator_signature.rb deleted file mode 100644 index 000ae6452..000000000 --- a/app/models/staged/petition_creator/has_creator_signature.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Staged - module PetitionCreator - module HasCreatorSignature - extend ActiveSupport::Concern - - included do - validate :creator_valid? - - def creator - @_creator ||= self.class::CreatorSignature.new(petition) - end - - private - - def creator_valid? - if creator.valid? - true - else - creator.errors.each do |attribute, message| - attribute = "creator.#{attribute}" - errors[attribute] << message - errors[attribute].uniq! - end - false - end - end - end - end - end -end diff --git a/app/models/staged/petition_creator/petition.rb b/app/models/staged/petition_creator/petition.rb deleted file mode 100644 index e84f09cbe..000000000 --- a/app/models/staged/petition_creator/petition.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Staged - module PetitionCreator - class Petition < Staged::Base::Petition - include Staged::Validations::PetitionDetails - end - end -end diff --git a/app/models/staged/petition_creator/replay_email.rb b/app/models/staged/petition_creator/replay_email.rb deleted file mode 100644 index acc47c408..000000000 --- a/app/models/staged/petition_creator/replay_email.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Staged - module PetitionCreator - class ReplayEmail < Staged::Base::Petition - include Staged::PetitionCreator::HasCreatorSignature - - class CreatorSignature < Staged::Base::CreatorSignature - include Staged::Validations::Email - end - end - end -end diff --git a/app/models/staged/petition_creator/replay_petition.rb b/app/models/staged/petition_creator/replay_petition.rb deleted file mode 100644 index c94b45da0..000000000 --- a/app/models/staged/petition_creator/replay_petition.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Staged - module PetitionCreator - class ReplayPetition < Staged::Base::Petition - end - end -end diff --git a/app/models/staged/petition_creator/stages.rb b/app/models/staged/petition_creator/stages.rb deleted file mode 100644 index fcc1b9c19..000000000 --- a/app/models/staged/petition_creator/stages.rb +++ /dev/null @@ -1,78 +0,0 @@ -module Staged - module PetitionCreator - module Stages - def self.stage_names - ['petition', 'creator', 'replay-petition', 'replay-email', 'done'] - end - - def self.initial_stage - for_name('petition') - end - - def self.for_name(name) - case name - when 'petition' - self::Petition - when 'replay-petition' - self::ReplayPetition - when 'creator' - self::Creator - when 'replay-email' - self::ReplayEmail - when 'done' - self::Done - else - self::Petition - end - end - - class Petition < Staged::Stage - def stage_object - @_stage_object ||= ::Staged::PetitionCreator::Petition.new(model) - end - - def name; 'petition'; end - def go_back; self; end - def go_next; Stages.for_name('replay-petition').new(model); end - end - - class ReplayPetition < Staged::Stage - def stage_object - @_stage_object ||= ::Staged::PetitionCreator::ReplayPetition.new(model) - end - - def name; 'replay-petition'; end - def go_back; Stages.for_name('petition').new(model); end - def go_next; Stages.for_name('creator').new(model); end - end - - class Creator < Staged::Stage - def stage_object - @_stage_object ||= ::Staged::PetitionCreator::Creator.new(model) - end - - def name; 'creator'; end - def go_back; Stages.for_name('replay-petition').new(model); end - def go_next; Stages.for_name('replay-email').new(model); end - end - - class ReplayEmail < Staged::Stage - def stage_object - @_stage_object ||= ::Staged::PetitionCreator::ReplayEmail.new(model) - end - - def name; 'replay-email'; end - def go_back; Stages.for_name('creator').new(model); end - def go_next; Stages.for_name('done').new(model); end - end - - class Done < Staged::Stage - def stage_object; model; end - def name; 'done'; end - def go_back; self; end - def go_next; self; end - def complete?; true; end - end - end - end -end diff --git a/app/models/staged/petition_signer.rb b/app/models/staged/petition_signer.rb deleted file mode 100644 index f60a22209..000000000 --- a/app/models/staged/petition_signer.rb +++ /dev/null @@ -1,61 +0,0 @@ -module Staged - module PetitionSigner - def self.manage(params, request, petition, stage, move) - Manager.new(params, request, petition, stage, move, self::Stages) - end - - def self.stages - self::Stages.stage_names - end - - class Manager - def initialize(params, request, petition, stage, move, stages) - @params = params - @request = request - @petition = petition - @previous_stage = stage - @move = move - @stages = stages - end - - # This is the stage we came from - the UI elements we showed the user - # that generated these params - attr_reader :previous_stage - attr_reader :move - attr_reader :stages - - def signature - @_signature ||= build_signature - end - - def stage - stage_manager.result_stage.name - end - - def stage_object - stage_manager.result_stage.stage_object - end - - def create_signature - sanitize! - stage_manager.create - end - - private - - def stage_manager - @_stage_manager ||= Staged::StageManager.new(stages, previous_stage, move, signature) - end - - def sanitize! - signature.email.strip! unless signature.email.blank? - end - - def build_signature - @petition.signatures.build(@params) do |signature| - signature.ip_address = @request.remote_ip - end - end - end - end -end diff --git a/app/models/staged/petition_signer/replay_email.rb b/app/models/staged/petition_signer/replay_email.rb deleted file mode 100644 index 60499c4fb..000000000 --- a/app/models/staged/petition_signer/replay_email.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Staged - module PetitionSigner - class ReplayEmail < Staged::Base::Signature - include Staged::Validations::Email - end - end -end diff --git a/app/models/staged/petition_signer/signer.rb b/app/models/staged/petition_signer/signer.rb deleted file mode 100644 index 913d9134c..000000000 --- a/app/models/staged/petition_signer/signer.rb +++ /dev/null @@ -1,8 +0,0 @@ -module Staged - module PetitionSigner - class Signer < Staged::Base::Signature - include Staged::Validations::SignerDetails - include Staged::Validations::Email - end - end -end diff --git a/app/models/staged/petition_signer/stages.rb b/app/models/staged/petition_signer/stages.rb deleted file mode 100644 index 9eac65d03..000000000 --- a/app/models/staged/petition_signer/stages.rb +++ /dev/null @@ -1,54 +0,0 @@ -module Staged - module PetitionSigner - module Stages - def self.stage_names - ['signer', 'replay-email', 'done'] - end - - def self.initial_stage - for_name('signer') - end - - def self.for_name(name) - case name - when 'signer' - self::Signer - when 'replay-email' - self::ReplayEmail - when 'done' - self::Done - else - self::Signer - end - end - - class Signer < Staged::Stage - def stage_object - @_stage_object ||= ::Staged::PetitionSigner::Signer.new(model) - end - - def name; 'signer'; end - def go_back; self; end - def go_next; Stages.for_name('replay-email').new(model); end - end - - class ReplayEmail < Staged::Stage - def stage_object - @_stage_object ||= ::Staged::PetitionSigner::ReplayEmail.new(model) - end - - def name; 'replay-email'; end - def go_back; Stages.for_name('signer').new(model); end - def go_next; Stages.for_name('done').new(model); end - end - - class Done < Staged::Stage - def stage_object; model; end - def name; 'done'; end - def go_back; self; end - def go_next; self; end - def complete?; true; end - end - end - end -end diff --git a/app/models/staged/petition_sponsor.rb b/app/models/staged/petition_sponsor.rb deleted file mode 100644 index a4c8ea75a..000000000 --- a/app/models/staged/petition_sponsor.rb +++ /dev/null @@ -1,24 +0,0 @@ -module Staged - module PetitionSponsor - def self.manage(params, sponsor, petition, stage, move) - Manager.new(params, sponsor, petition, stage, move, Staged::PetitionSigner::Stages) - end - - def self.stages - Staged::PetitionSigner::Stages.stage_names - end - - class Manager < Staged::PetitionSigner::Manager - def initialize(params, sponsor, petition, stage, move, stages) - super(params, petition, stage, move, stages) - @sponsor = sponsor - end - - private - - def build_signature - @sponsor.build_signature(@params) - end - end - end -end diff --git a/app/models/staged/stage.rb b/app/models/staged/stage.rb deleted file mode 100644 index f965513b7..000000000 --- a/app/models/staged/stage.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Staged - class Stage < Struct.new(:model) - def complete?; false; end - - def go(move) - case move - when 'back' - go_back - when 'next' - if valid? - go_next - else - stay - end - else - stay - end - end - - def stay; self; end - - def valid? - stage_object.valid? - end - end -end diff --git a/app/models/staged/stage_manager.rb b/app/models/staged/stage_manager.rb deleted file mode 100644 index 6fdf69c06..000000000 --- a/app/models/staged/stage_manager.rb +++ /dev/null @@ -1,42 +0,0 @@ -module Staged - class StageManager < Struct.new(:stages, :previous_stage, :move, :model) - delegate :for_name, :initial_stage, - to: :stages - - def starting_stage - @_starting_stage ||= for_name(previous_stage).new(model) - end - - def result_stage - @_result_stage ||= starting_stage.go(move) - end - - def reset_result_stage - @_result_stage = find_earliest_error - end - - def create - if result_stage.complete? - if model.save - true - else - reset_result_stage - false - end - else - false - end - end - - private - - def find_earliest_error - stage = initial_stage.new(model) - while stage.valid? && !stage.complete? - stage = stage.go('next') - end - stage - end - - end -end diff --git a/app/models/staged/validations/email.rb b/app/models/staged/validations/email.rb deleted file mode 100644 index b349fdfc0..000000000 --- a/app/models/staged/validations/email.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Staged - module Validations - module Email - extend ActiveSupport::Concern - - included do - validates :email, presence: true, email: { allow_blank: true }, on: :create - end - end - end -end diff --git a/app/models/staged/validations/petition_details.rb b/app/models/staged/validations/petition_details.rb deleted file mode 100644 index 80fd0051b..000000000 --- a/app/models/staged/validations/petition_details.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Staged - module Validations - module PetitionDetails - extend ActiveSupport::Concern - - included do - validates :action, presence: true, length: { maximum: 80, allow_blank: true } - validates :background, presence: true, length: { maximum: 300, allow_blank: true } - validates :additional_details, length: { maximum: 800, allow_blank: true } - end - end - end -end diff --git a/app/models/staged/validations/signer_details.rb b/app/models/staged/validations/signer_details.rb deleted file mode 100644 index d972f7cb0..000000000 --- a/app/models/staged/validations/signer_details.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'postcode_validator' - -module Staged - module Validations - module SignerDetails - extend ActiveSupport::Concern - - included do - validates :name, presence: true, length: { maximum: 255 } - validates :location_code, presence: true - validates :postcode, presence: true, postcode: true, if: :united_kingdom? - validates :uk_citizenship, acceptance: true, unless: :persisted?, allow_nil: false - end - - def united_kingdom? - location_code == 'GB' - end - end - end -end diff --git a/app/validators/email_validator.rb b/app/validators/email_validator.rb index 9901de3ea..fd476ff22 100644 --- a/app/validators/email_validator.rb +++ b/app/validators/email_validator.rb @@ -2,7 +2,7 @@ class EmailValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if value =~ EMAIL_REGEX email = parsed_email(value) - record.errors.add :email, :plus_address if plus_address?(email) + record.errors.add attribute, :plus_address if plus_address?(email) else record.errors.add attribute, :invalid end diff --git a/app/views/petitions/check.html.erb b/app/views/petitions/check.html.erb index 807587d56..ed55e71f7 100644 --- a/app/views/petitions/check.html.erb +++ b/app/views/petitions/check.html.erb @@ -4,8 +4,8 @@
- - + +

80 characters max

<%= render 'petitions/action_help' %>
diff --git a/app/views/petitions/check_results.html.erb b/app/views/petitions/check_results.html.erb index dbcbb45e0..9e6175465 100644 --- a/app/views/petitions/check_results.html.erb +++ b/app/views/petitions/check_results.html.erb @@ -1,20 +1,34 @@ -<%= link_to check_petitions_path, class: "back-page" do %> +<%= link_to check_petitions_path(q: @new_petition.action), class: "back-page" do %> ◀ Back <% end %> -<% if @petitions.empty? %> -

We checked for similar petitions

-

We checked for similar petitions and didn't find any.

-

So you can go ahead and create a new one.

- <% create_button_text = "Continue" -%> -<% else %> +<% if petitions = @new_petition.duplicates %> +

We found some similar petitions

If one of these petitions matches yours, sign it and share it instead

Your petition is more likely to get to <%= Site.formatted_threshold_for_response %> and <%= Site.formatted_threshold_for_debate %> signatures that way.

<%= render 'notification', text: "If there’s already a petition on the same topic, your petition is likely to be rejected" %> - <%= render 'petitions/search/results_for_pre_create_petition', petitions: @petitions %> - <% create_button_text = "My petition is different" -%> + + <% petitions.each do |petition| %> + + <% end %> + + <%= link_to("My petition is different", new_petition_path(q: @new_petition.action), class: "button") %> + +<% else %> + +

We checked for similar petitions

+

We checked for similar petitions and didn't find any.

+

So you can go ahead and create a new one.

+ + <%= link_to("Continue", new_petition_path(q: @new_petition.action), class: "button") %> <% end %> -<%= link_to create_button_text, new_petition_path(:petition_action => @petitions.query.first(255)), - :class => 'button' %> diff --git a/app/views/petitions/create/_creator_stage.html.erb b/app/views/petitions/create/_creator_stage.html.erb new file mode 100644 index 000000000..c4ab61b89 --- /dev/null +++ b/app/views/petitions/create/_creator_stage.html.erb @@ -0,0 +1,54 @@ +<%= form.submit raw("◀ Back"), name: "move_back", class: "back-page" %> + +<%= form.hidden_field :action %> +<%= form.hidden_field :background %> +<%= form.hidden_field :additional_details %> + +

Sign your petition

+

<%= petition.action %>

+ +<%= render "petitions/create/error_summary", form: form %> + +<%= form_row for: [petition, :uk_citizenship], class: "uk-citizen" do %> + + Only British citizens or UK residents have the right to create and sign petitions + + <%= error_messages_for_field petition, :uk_citizenship %> + <%= form.label :uk_citizenship, class: "block-label", for: nil do %> + <%= form.check_box :uk_citizenship, tabindex: increment %> + I am a British citizen or UK resident + <% end %> +<% end %> + +<%= form_row for: [petition, :name] do %> + <%= form.label :name, class: "form-label" %> + <%= error_messages_for_field petition, :name %> + <%= form.text_field :name, tabindex: increment, maxlength: 255, size: nil, class: "form-control" %> + You must use your full name +<% end %> + +<%= form_row for: [petition, :email] do %> + <%= form.label :email, "Email address", class: "form-label" %> + <%= error_messages_for_field petition, :email %> + <%= form.text_field :email, type: "email", tabindex: increment, class: "form-control" %> +<% end %> + +<%= form_row for: [petition, :location_code] do %> + <%= form.label :location_code, "Location", class: "form-label" %> + <%= error_messages_for_field petition, :location_code %> + <%= form.select :location_code, countries_for_select, {}, tabindex: increment, class: "form-control" %> +<% end %> + +<%= form_row for: [petition, :postcode] do %> + <%= form.label :postcode, class: "form-label" %> + <%= error_messages_for_field petition, :postcode %> + <%= form.text_field :postcode, tabindex: increment, class: "form-control small" %> +<% end %> + +<%= form.submit "Continue", name: "move_next", class: "button", tabindex: increment %> + +
+

Your name will be published on this petition as the petition creator.

+

Other than that, we won’t publish your personal details anywhere or use them for anything other than this petition.

+

We will email you about this petition, and nothing else. You can unsubscribe at any time.

+
diff --git a/app/views/petitions/create/_email_hidden.html.erb b/app/views/petitions/create/_email_hidden.html.erb deleted file mode 100644 index 3a21e3e66..000000000 --- a/app/views/petitions/create/_email_hidden.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= f.fields_for :creator, petition.creator do |csf| %> - <%= csf.hidden_field :email %> -<% end %> diff --git a/app/views/petitions/create/_error_summary.html.erb b/app/views/petitions/create/_error_summary.html.erb index e5bc848e8..094850e84 100644 --- a/app/views/petitions/create/_error_summary.html.erb +++ b/app/views/petitions/create/_error_summary.html.erb @@ -1,9 +1,9 @@ -<% if f.object.errors.any? -%> +<% if form.object.errors.any? -%>

We couldn't create this petition

-

<%= t(:prompt, scope: :"petitions.errors", count: f.object.errors.size) %>

+

<%= t(:prompt, scope: :"petitions.errors", count: form.object.errors.size) %>

    - <% f.object.errors.each do |key, error| -%> + <% form.object.errors.each do |key, error| -%>
  • <%= error %>
  • <% end -%>
diff --git a/app/views/petitions/create/_petition_details_hidden.html.erb b/app/views/petitions/create/_petition_details_hidden.html.erb deleted file mode 100644 index dcc7ee983..000000000 --- a/app/views/petitions/create/_petition_details_hidden.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= f.hidden_field :action %> -<%= f.hidden_field :background %> -<%= f.hidden_field :additional_details %> diff --git a/app/views/petitions/create/_petition_details_ui.html.erb b/app/views/petitions/create/_petition_details_ui.html.erb deleted file mode 100644 index 1b53d890f..000000000 --- a/app/views/petitions/create/_petition_details_ui.html.erb +++ /dev/null @@ -1,43 +0,0 @@ -<%= link_to back_url, class: "back-page" do %> - ◀ Back -<% end %> - -

Start a petition

- -

Remember, your petition must meet the <%= link_to 'petition standards', help_url(anchor: 'standards') %>.

- -<%= render 'petitions/create/error_summary', f: f %> - -<%= form_row :for => [petition, :action] do %> - <%= f.label :action, 'What do you want us to do?', class: 'form-label' %> - <%= error_messages_for_field petition, :action %> - <%= f.text_area :action, tabindex: increment, rows: 3, 'data-max-length' => '80', class: 'form-control' %> -

80 characters max

- <%= render 'petitions/action_help' %> -<% end %> - -<%= form_row :for => [petition, :background] do %> - <%= f.label :background, 'Background', class: 'form-label' %> - <%= error_messages_for_field petition, :background %> - <%= f.text_area :background, tabindex: increment, rows: 5, 'data-max-length' => '300', class: 'form-control' %> -

300 characters max

-
- Help writing background -
-

Use this section to back up why you want action. Some people include a personal story here.

-

Remember that people won’t always know that much about the issue you’re petitioning about - explain it.

-
-
-<% end %> - -<%= form_row :for => [petition, :additional_details] do %> - <%= f.label :additional_details, class: 'form-label' do %> - Additional details - Optional - background information, evidence and references - <% end %> - <%= error_messages_for_field petition, :additional_details %> - <%= f.text_area :additional_details, tabindex: increment, rows: 7, 'data-max-length' => '500', class: 'form-control' %> -

500 characters max

-<% end %> - -<%= f.submit 'Preview petition', :name => 'move:next', :class => 'button', :tabindex => increment %> diff --git a/app/views/petitions/create/_petition_stage.html.erb b/app/views/petitions/create/_petition_stage.html.erb new file mode 100644 index 000000000..6192bc1e3 --- /dev/null +++ b/app/views/petitions/create/_petition_stage.html.erb @@ -0,0 +1,43 @@ +<%= link_to check_results_petitions_path(q: petition.action), class: "back-page" do %> + ◀ Back +<% end %> + +

Start a petition

+ +

Remember, your petition must meet the <%= link_to "petition standards", help_url(anchor: "standards") %>.

+ +<%= render "petitions/create/error_summary", form: form %> + +<%= form_row for: [petition, :action] do %> + <%= form.label :action, "What do you want us to do?", class: "form-label" %> + <%= error_messages_for_field petition, :action %> + <%= form.text_area :action, tabindex: increment, rows: 3, data: { max_length: 80 }, class: "form-control" %> +

80 characters max

+ <%= render "petitions/action_help" %> +<% end %> + +<%= form_row for: [petition, :background] do %> + <%= form.label :background, "Background", class: "form-label" %> + <%= error_messages_for_field petition, :background %> + <%= form.text_area :background, tabindex: increment, rows: 5, data: { max_length: 300 }, class: "form-control" %> +

300 characters max

+
+ Help writing background +
+

Use this section to back up why you want action. Some people include a personal story here.

+

Remember that people won’t always know that much about the issue you’re petitioning about - explain it.

+
+
+<% end %> + +<%= form_row for: [petition, :additional_details] do %> + <%= form.label :additional_details, class: "form-label" do %> + Additional details + Optional - background information, evidence and references + <% end %> + <%= error_messages_for_field petition, :additional_details %> + <%= form.text_area :additional_details, tabindex: increment, rows: 7, data: { max_length: 500 }, class: "form-control" %> +

500 characters max

+<% end %> + +<%= form.submit "Preview petition", name: "move_next", class: "button", tabindex: increment %> diff --git a/app/views/petitions/create/_replay_email_stage.html.erb b/app/views/petitions/create/_replay_email_stage.html.erb new file mode 100644 index 000000000..a7cbe348a --- /dev/null +++ b/app/views/petitions/create/_replay_email_stage.html.erb @@ -0,0 +1,22 @@ +<%= form.submit raw("◀ Back"), name: "move_back", class: "back-page" %> + +<%= form.hidden_field :action %> +<%= form.hidden_field :background %> +<%= form.hidden_field :additional_details %> + +<%= form.hidden_field :name %> +<%= form.hidden_field :uk_citizenship %> +<%= form.hidden_field :postcode %> +<%= form.hidden_field :location_code %> + +

Make sure this is right

+ +<%= form_row for: [petition, :email] do %> + <%= form.label :email, "Email address", class: "form-label" %> + <%= error_messages_for_field petition, :email %> + <%= form.text_field :email, type: "email", tabindex: increment, class: "form-control" %> +<% end %> + +<%= form.submit "Yes – this is my email address", name: "move_next", class: "button", tabindex: increment %> + +

If your email address is wrong, just change it above

diff --git a/app/views/petitions/create/_replay_email_ui.html.erb b/app/views/petitions/create/_replay_email_ui.html.erb deleted file mode 100644 index 460c7302d..000000000 --- a/app/views/petitions/create/_replay_email_ui.html.erb +++ /dev/null @@ -1,11 +0,0 @@ -<%= f.submit raw('◀ Back'), :name => 'move:back', :class => 'back-page' %> - -

Make sure this is right

- -<%= f.fields_for :creator, petition.creator do |csf| %> - <%= render 'petitions/create/signature_email', f: csf, signature: csf.object, hide_label: true %> -<% end %> - -<%= f.submit 'Yes – this is my email address', :name => 'move:next', :class => 'button', :tabindex => increment %> - -

If your email address is wrong, just change it above

diff --git a/app/views/petitions/create/_replay_petition_ui.html.erb b/app/views/petitions/create/_replay_petition_stage.html.erb similarity index 63% rename from app/views/petitions/create/_replay_petition_ui.html.erb rename to app/views/petitions/create/_replay_petition_stage.html.erb index 3b84458ad..a368ca115 100644 --- a/app/views/petitions/create/_replay_petition_ui.html.erb +++ b/app/views/petitions/create/_replay_petition_stage.html.erb @@ -1,21 +1,25 @@ -<%= f.submit raw('◀ Back'), :name => 'move:back', :class => 'back-page' %> +<%= form.submit raw("◀ Back"), name: "move_back", class: "back-page" %> + +<%= form.hidden_field :action %> +<%= form.hidden_field :background %> +<%= form.hidden_field :additional_details %>

Check your petition

<%= petition.action %>

-
<%= auto_link(simple_format(h(petition.background)), html: { rel: 'nofollow' }) %>
+
<%= auto_link(simple_format(h(petition.background)), html: { rel: "nofollow" }) %>
<% if petition.additional_details.present? %>
More details -
<%= auto_link(simple_format(h(petition.additional_details)), html: { rel: 'nofollow' }) %>
+
<%= auto_link(simple_format(h(petition.additional_details)), html: { rel: "nofollow" }) %>
<% end %>
-<%= f.submit 'This looks good', :name => 'move:next', :class => 'button', :tabindex => increment %> +<%= form.submit "This looks good", name: "move_next", class: "button", tabindex: increment %> -<%= f.submit 'Go back and make changes', :name => 'move:back', :class => 'button-secondary', :tabindex => increment %> +<%= form.submit "Go back and make changes", name: "move_back", class: "button-secondary", tabindex: increment %>

You can’t change your petition after this point.

If you’ve included personal information, such as details about your health, your family or your job, think carefully about whether you are happy for this to be published.

diff --git a/app/views/petitions/create/_signature_email.html.erb b/app/views/petitions/create/_signature_email.html.erb deleted file mode 100644 index 3ddb4823f..000000000 --- a/app/views/petitions/create/_signature_email.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<%= form_row :for => [signature, :email] do %> - <%= f.label :email, 'Email address', :class => 'form-label' + (hide_label ? ' visuallyhidden' : '') %> - <%= error_messages_for_field signature, :email %> - <%= f.text_field :email, :type => 'email', :tabindex => increment, :class => 'form-control' %> -<% end %> diff --git a/app/views/petitions/create/_signature_form.html.erb b/app/views/petitions/create/_signature_form.html.erb deleted file mode 100644 index 3f333b162..000000000 --- a/app/views/petitions/create/_signature_form.html.erb +++ /dev/null @@ -1,37 +0,0 @@ -<%= form_row :for => [signature, :uk_citizenship], :class => 'uk-citizen' do %> - - <% if context == 'create-petition' %> - Only British citizens or UK residents have the right to create and sign petitions - <% else %> - Only British citizens or UK residents have the right to sign - <% end %> - - <%= error_messages_for_field signature, :uk_citizenship %> - <%= f.label :uk_citizenship, :class => 'block-label', :for => nil do %> - <%= f.check_box :uk_citizenship, :tabindex => increment %> - I am a British citizen or UK resident - <% end %> -<% end %> - -<%= form_row :for => [signature, :name] do %> - <%= f.label :name, :class => 'form-label' %> - <%= error_messages_for_field signature, :name %> - <%= f.text_field :name, :tabindex => increment, :maxlength => 255, :size => nil, :class => 'form-control' %> - <% if context == 'create-petition' %> - You must use your full name - <% end %> -<% end %> - -<%= render 'petitions/create/signature_email', f: f, signature: signature, hide_label: false %> - -<%= form_row :for => [signature, :location_code] do %> - <%= f.label :location_code, 'Location', :class => 'form-label' %> - <%= error_messages_for_field signature, :location_code %> - <%= f.select :location_code, countries_for_select, {}, :tabindex => increment, :class => 'form-control' %> -<% end %> - -<%= form_row :for => [signature, :postcode] do %> - <%= f.label :postcode, :class => 'form-label' %> - <%= error_messages_for_field signature, :postcode %> - <%= f.text_field :postcode, :tabindex => increment, :class => 'form-control small' %> -<% end %> diff --git a/app/views/petitions/create/_signature_hidden.html.erb b/app/views/petitions/create/_signature_hidden.html.erb deleted file mode 100644 index b50465686..000000000 --- a/app/views/petitions/create/_signature_hidden.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -<%= f.hidden_field :name %> -<%= f.hidden_field :uk_citizenship %> -<%= f.hidden_field :postcode %> -<%= f.hidden_field :location_code %> diff --git a/app/views/petitions/create/_your_details_hidden.html.erb b/app/views/petitions/create/_your_details_hidden.html.erb deleted file mode 100644 index a6e3ec1b4..000000000 --- a/app/views/petitions/create/_your_details_hidden.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= f.fields_for :creator, petition.creator do |csf| %> - <%= render 'petitions/create/signature_hidden', f: csf, signature: csf.object %> -<% end %> diff --git a/app/views/petitions/create/_your_details_ui.html.erb b/app/views/petitions/create/_your_details_ui.html.erb deleted file mode 100644 index 6411eea41..000000000 --- a/app/views/petitions/create/_your_details_ui.html.erb +++ /dev/null @@ -1,18 +0,0 @@ -<%= f.submit raw('◀ Back'), :name => 'move:back', :class => 'back-page' %> - -

Sign your petition

-

<%= petition.action %>

- -<%= render 'petitions/create/error_summary', f: f %> - -<%= f.fields_for :creator, petition.creator do |csf| %> - <%= render 'petitions/create/signature_form', :f => csf, :signature => csf.object, :context => 'create-petition' %> -<% end %> - -<%= f.submit 'Continue', :name => 'move:next', :class => 'button', :tabindex => increment %> - -
-

Your name will be published on this petition as the petition creator.

-

Other than that, we won’t publish your personal details anywhere or use them for anything other than this petition.

-

We will email you about this petition, and nothing else. You can unsubscribe at any time.

-
diff --git a/app/views/petitions/new.html.erb b/app/views/petitions/new.html.erb index 6749c9f6d..d1cf873d2 100644 --- a/app/views/petitions/new.html.erb +++ b/app/views/petitions/new.html.erb @@ -1,3 +1,4 @@ -<%= form_for @stage_manager.stage_object, url: new_petition_path do |f| %> - <%= render_petition_form @stage_manager, f %> +<%= form_for @new_petition, url: new_petition_path do |form| %> +<%= hidden_field_tag :stage, @new_petition.stage %> +<%= render @new_petition, form: form, petition: form.object %> <% end %> diff --git a/app/views/petitions/resend_confirmation_email.html.erb b/app/views/petitions/resend_confirmation_email.html.erb deleted file mode 100644 index 5dc8ccf09..000000000 --- a/app/views/petitions/resend_confirmation_email.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -
-

Confirmation email re-sent

-
- -
-

- If you signed the petition with this email address, you will shortly receive - an email to confirm your signature. -

-
diff --git a/app/views/petitions/search/_results_for_pre_create_petition.html.erb b/app/views/petitions/search/_results_for_pre_create_petition.html.erb deleted file mode 100644 index 001660ce7..000000000 --- a/app/views/petitions/search/_results_for_pre_create_petition.html.erb +++ /dev/null @@ -1,11 +0,0 @@ -<% petitions.each do |petition| %> -
    -
  • -

    <%= link_to petition.action, petition_path(petition) %>

    - <% unless petition.rejected? -%> -

    <%= signature_count(:default, petition.signature_count) %>

    - <% end %> -

    <%= petition.background %>

    -
  • -
-<% end %> diff --git a/features/step_definitions/petition_steps.rb b/features/step_definitions/petition_steps.rb index 8c0910254..ef1efe2c1 100644 --- a/features/step_definitions/petition_steps.rb +++ b/features/step_definitions/petition_steps.rb @@ -269,7 +269,7 @@ # NOTE: we do this to remove the maxlength attribtue on the petition # action input because any modern browser/driver will not let us enter # values longer than maxlength and so we can't test our JS validation - page.execute_script "document.getElementById('petition_action').removeAttribute('maxlength');" + page.execute_script "document.getElementById('petition_creator_action').removeAttribute('maxlength');" end Then(/^the petition with action: "(.*?)" should have requested a government response email after "(.*?)"$/) do |petition_action, timestamp| diff --git a/spec/controllers/petitions_controller_spec.rb b/spec/controllers/petitions_controller_spec.rb index 61e661cc5..0571927d1 100644 --- a/spec/controllers/petitions_controller_spec.rb +++ b/spec/controllers/petitions_controller_spec.rb @@ -1,22 +1,20 @@ require 'rails_helper' RSpec.describe PetitionsController, type: :controller do - describe "new" do - it "should assign a new stage_manager with a petition" do + describe "GET /petitions/new" do + it "should assign a petition creator" do get :new - expect(assigns[:stage_manager]).not_to be_nil - expect(assigns[:stage_manager].petition).not_to be_nil + expect(assigns[:new_petition]).not_to be_nil end it "is on stage 'petition'" do get :new - expect(assigns[:stage_manager].stage).to eq 'petition'; + expect(assigns[:new_petition].stage).to eq "petition"; end - it "fills in the action if given as petition_action" do - action = "my fancy new action" - get :new, :petition_action => action - expect(assigns[:stage_manager].petition.action).to eq action + it "fills in the action if given as query parameter 'q'" do + get :new, q: "my fancy new action" + expect(assigns[:new_petition].action).to eq("my fancy new action") end context "when parliament is dissolved" do @@ -42,197 +40,213 @@ end end - describe "create" do - let(:creator_attributes) do + describe "POST /petitions/new" do + let(:params) do { - :name => 'John Mcenroe', :email => 'john@example.com', - :postcode => 'SE3 4LL', :location_code => 'GB', - :uk_citizenship => '1' - } - end - let(:petition_attributes) do - { - :action => 'Save the planet', - :background => 'Limit temperature rise at two degrees', - :additional_details => 'Global warming is upon us', - :creator => creator_attributes + action: "Save the planet", + background: "Limit temperature rise at two degrees", + additional_details: "Global warming is upon us", + name: "John Mcenroe", email: "john@example.com", + postcode: "SE3 4LL", location_code: "GB", + uk_citizenship: "1" } end let(:constituency) do - FactoryGirl.create( - :constituency, external_id: '54321', name: 'North Creatorshire' - ) + FactoryGirl.create(:constituency, external_id: "54321", name: "North Creatorshire") end - def do_post(options = {}) - params = { - :stage => 'replay-email', - :move => 'next', - :petition => petition_attributes - }.merge(options) - + before do allow(Constituency).to receive(:find_by_postcode).with("SE34LL").and_return(constituency) - - perform_enqueued_jobs do - post :create, params - end - end - - context 'managing the "move" parameter' do - it 'defaults to "next" if it is not present' do - do_post :move => nil - expect(controller.params['move']).to eq 'next' - end - - it 'defaults to "next" if it is present but blank' do - do_post :move => '' - expect(controller.params['move']).to eq 'next' - end - - it 'overrides it to "next" if it is present but not "next" or "back"' do - do_post :move => 'blah' - expect(controller.params['move']).to eq 'next' - end - - it 'overrides it to "next" if "move:next" is present' do - do_post :move => 'blah', :'move:next' => 'Onwards!' - expect(controller.params['move']).to eq 'next' - end - - it 'overrides it to "back" if "move:back" is present' do - do_post :move => 'blah', :'move:back' => 'Backwards!' - expect(controller.params['move']).to eq 'back' - end - - it 'overrides it to "next" if both "move:next" and "move:back" are present' do - do_post :move => 'blah', :'move:next' => 'Onwards!', :'move:back' => 'Backwards!' - expect(controller.params['move']).to eq 'next' - end end context "valid post" do + let(:petition) { Petition.find_by_action("Save the planet") } + it "should successfully create a new petition and a signature" do - do_post - petition = Petition.find_by_action!('Save the planet') + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params + end + expect(petition.creator).not_to be_nil expect(response).to redirect_to("https://petition.parliament.uk/petitions/#{petition.id}/thank-you") end it "should successfully create a new petition and a signature even when email has white space either end" do - creator_attributes[:email] = ' john@example.com ' - do_post - petition = Petition.find_by_action!('Save the planet') + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(email: " john@example.com ") + end + + expect(petition).not_to be_nil + expect(response).to redirect_to("https://petition.parliament.uk/petitions/#{petition.id}/thank-you") end it "should strip a petition action on petition creation" do - petition_attributes[:action] = ' Save the planet' - do_post - petition = Petition.find_by_action!('Save the planet') + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(action: " Save the planet") + end + + expect(petition).not_to be_nil + expect(response).to redirect_to("https://petition.parliament.uk/petitions/#{petition.id}/thank-you") end it "should send gather sponsors email to petition's creator" do - ActionMailer::Base.deliveries.clear - do_post - email = ActionMailer::Base.deliveries.detect { |email| email.subject =~ /Action required: Petition/ } - expect(email).to be_present - expect(email.from).to eq(["no-reply@petition.parliament.uk"]) - expect(email.to).to eq(["john@example.com"]) + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params + end + + expect(last_email_sent).to deliver_to("john@example.com") + expect(last_email_sent).to deliver_from(%{"Petitions: UK Government and Parliament" }) + expect(last_email_sent).to have_subject("Action required: Petition “Save the planet”") end it "should successfully point the signature at the petition" do - do_post - petition = Petition.find_by_action!('Save the planet') + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params + end + expect(petition.creator.petition).to eq(petition) end it "should set user's ip address on signature" do - do_post - petition = Petition.find_by_action!('Save the planet') + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params + end + expect(petition.creator.ip_address).to eq("0.0.0.0") end it "should not be able to set the state of a new petition" do - petition_attributes[:state] = Petition::VALIDATED_STATE - do_post - petition = Petition.find_by_action!('Save the planet') + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(state: Petition::VALIDATED_STATE) + end + expect(petition.state).to eq(Petition::PENDING_STATE) end it "should not be able to set the state of a new signature" do - creator_attributes[:state] = Signature::VALIDATED_STATE - do_post - petition = Petition.find_by_action!('Save the planet') + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(state: Signature::VALIDATED_STATE) + end + expect(petition.creator.state).to eq(Signature::PENDING_STATE) end it "should set notify_by_email to true on the creator signature" do - do_post - expect(Petition.find_by_action!('Save the planet').creator.notify_by_email).to be_truthy + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(state: Signature::VALIDATED_STATE) + end + + expect(petition.creator.notify_by_email).to be_truthy end it "sets the constituency_id on the creator signature, based on the postcode" do - do_post - expect(Petition.find_by_action!('Save the planet').creator.constituency_id).to eq("54321") + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(state: Signature::VALIDATED_STATE) + end + + expect(petition.creator.constituency_id).to eq("54321") end context "invalid post" do it "should not create a new petition if no action is given" do - petition_attributes[:action] = '' - do_post - expect(Petition.find_by_action('Save the planet')).to be_nil - expect(assigns[:stage_manager].petition.errors[:action]).not_to be_blank + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(action: "") + end + + expect(petition).to be_nil + expect(assigns[:new_petition].errors[:action]).not_to be_blank expect(response).to be_success end it "should not create a new petition if email is invalid" do - creator_attributes[:email] = 'not much of an email' - do_post - expect(Petition.find_by_action('Save the planet')).to be_nil + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(email: "not much of an email") + end + + expect(petition).to be_nil expect(response).to be_success end it "should not create a new petition if UK citizenship is not confirmed" do - creator_attributes[:uk_citizenship] = '0' - do_post - expect(Petition.find_by_action('Save the planet')).to be_nil + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(uk_citizenship: "0") + end + + expect(petition).to be_nil expect(response).to be_success end - it "has stage of 'petition' if there are errors on action, background, or additional_details" do - do_post :petition => petition_attributes.merge(:action => '') - expect(assigns[:stage_manager].stage).to eq 'petition' - do_post :petition => petition_attributes.merge(:background => '') - expect(assigns[:stage_manager].stage).to eq 'petition' - do_post :petition => petition_attributes.merge(:additional_details => 'a'*801) - expect(assigns[:stage_manager].stage).to eq 'petition' + it "has stage of 'petition' if there is an error on action" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(action: "") + end + + expect(assigns[:new_petition].stage).to eq "petition" end - it "has stage of 'creator' if there are errors on name, uk_citizenship, postcode or country" do - do_post :petition => petition_attributes.merge(:creator => creator_attributes.merge(:name => '')) - expect(assigns[:stage_manager].stage).to eq 'creator' - do_post :petition => petition_attributes.merge(:creator => creator_attributes.merge(:uk_citizenship => '')) - expect(assigns[:stage_manager].stage).to eq 'creator' - do_post :petition => petition_attributes.merge(:creator => creator_attributes.merge(:postcode => '')) - expect(assigns[:stage_manager].stage).to eq 'creator' - do_post :petition => petition_attributes.merge(:creator => creator_attributes.merge(:location_code => '')) - expect(assigns[:stage_manager].stage).to eq 'creator' + it "has stage of 'petition' if there is an error on background" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(background: "") + end + + expect(assigns[:new_petition].stage).to eq "petition" end - it "has stage of 'replay-email' if there are errors on email and we came from 'replay-email' stage" do - new_creator_attribtues = creator_attributes.merge(:email => 'foo@') - new_petition_attributes = petition_attributes.merge(:creator => new_creator_attribtues) - do_post :stage => 'replay-email', - :petition => new_petition_attributes - expect(assigns[:stage_manager].stage).to eq 'replay-email' + it "has stage of 'petition' if there is an error on additional_details" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(additional_details: "a" * 801) + end + + expect(assigns[:new_petition].stage).to eq "petition" end - it "has stage of 'creator' if there are errors on email and we came from 'creator' stage" do - new_creator_attribtues = creator_attributes.merge(:email => 'foo@') - new_petition_attributes = petition_attributes.merge(:creator => new_creator_attribtues) - do_post :stage => 'creator', - :petition => new_petition_attributes - expect(assigns[:stage_manager].stage).to eq 'creator' + it "has stage of 'creator' if there is an error on name" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(name: "") + end + + expect(assigns[:new_petition].stage).to eq "creator" + end + + it "has stage of 'creator' if there is an error on uk_citizenship" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(uk_citizenship: "0") + end + + expect(assigns[:new_petition].stage).to eq "creator" + end + + it "has stage of 'creator' if there is an error on postcode" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(postcode: "") + end + + expect(assigns[:new_petition].stage).to eq "creator" + end + + it "has stage of 'creator' if there is an error on location_code" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(location_code: "") + end + + expect(assigns[:new_petition].stage).to eq "creator" + end + + it "has stage of 'replay_email' if there are errors on email and we came from the 'replay_email' stage" do + perform_enqueued_jobs do + post :create, stage: "replay_email", petition_creator: params.merge(email: "") + end + + expect(assigns[:new_petition].stage).to eq "replay_email" + end + + it "has stage of 'creator' if there are errors on email and we came from the 'creator' stage" do + perform_enqueued_jobs do + post :create, stage: "creator", petition_creator: params.merge(email: "") + end + + expect(assigns[:new_petition].stage).to eq "creator" end end end @@ -260,23 +274,24 @@ def do_post(options = {}) end end - describe "show" do + describe "GET /petitions/:id" do let(:petition) { double } + it "assigns the given petition" do allow(petition).to receive(:stopped?).and_return(false) allow(petition).to receive(:collecting_sponsors?).and_return(false) allow(petition).to receive(:in_moderation?).and_return(false) allow(petition).to receive(:moderated?).and_return(true) - allow(Petition).to receive_message_chain(:show, :find => petition) + allow(Petition).to receive_message_chain(:show, find: petition) - get :show, :id => 1 + get :show, id: 1 expect(assigns(:petition)).to eq(petition) end it "does not allow hidden petitions to be shown" do expect { allow(Petition).to receive_message_chain(:visible, :find).and_raise ActiveRecord::RecordNotFound - get :show, :id => 1 + get :show, id: 1 }.to raise_error(ActiveRecord::RecordNotFound) end @@ -315,8 +330,8 @@ def do_post(options = {}) end end - describe "GET #index" do - context 'when no state param is provided' do + describe "GET /petitions" do + context "when no state param is provided" do it "is successful" do get :index expect(response).to be_success @@ -328,27 +343,27 @@ def do_post(options = {}) end end - context 'when a state param is provided' do - context 'but it is not a public facet from the locale file' do - it 'redirects to itself with state=all' do - get :index, state: 'awaiting_monkey' - expect(response).to redirect_to 'https://petition.parliament.uk/petitions?state=all' + context "when a state param is provided" do + context "but it is not a public facet from the locale file" do + it "redirects to itself with state=all" do + get :index, state: "awaiting_monkey" + expect(response).to redirect_to "https://petition.parliament.uk/petitions?state=all" end - it 'preserves other params when it redirects' do - get :index, q: 'what is clocks', state: 'awaiting_monkey' - expect(response).to redirect_to 'https://petition.parliament.uk/petitions?q=what+is+clocks&state=all' + it "preserves other params when it redirects" do + get :index, q: "what is clocks", state: "awaiting_monkey" + expect(response).to redirect_to "https://petition.parliament.uk/petitions?q=what+is+clocks&state=all" end end - context 'and it is a public facet from the locale file' do - it 'is successful' do - get :index, state: 'open' + context "and it is a public facet from the locale file" do + it "is successful" do + get :index, state: "open" expect(response).to be_success end it "exposes a search scoped to the state param" do - get :index, state: 'open' + get :index, state: "open" expect(assigns(:petitions).scope).to eq :open end end @@ -366,7 +381,7 @@ def do_post(options = {}) end end - describe "GET #check" do + describe "GET /petitions/check" do it "is successful" do get :check expect(response).to be_success @@ -395,7 +410,7 @@ def do_post(options = {}) end end - describe "GET #check_results" do + describe "GET /petitions/check_results" do it "is successful" do get :check_results, q: "action" expect(response).to be_success diff --git a/spec/models/staged/petition_creator_spec.rb b/spec/models/staged/petition_creator_spec.rb deleted file mode 100644 index dfab3757e..000000000 --- a/spec/models/staged/petition_creator_spec.rb +++ /dev/null @@ -1,194 +0,0 @@ -require 'rails_helper' - -RSpec.describe Staged::PetitionCreator, type: :model do - let(:petition_params) { {} } - let(:move) { '' } - let(:stage) { '' } - let(:request) { double(remote_ip: '192.168.0.1') } - subject { described_class.manager(petition_params, request, stage, move) } - let(:petition) { subject.petition } - - describe '#create_petition' do - it 'strips trailing whitespace from the petition action' do - petition_params[:action] = ' woo ' - subject.create_petition - expect(petition.action).to eq 'woo' - end - - context 'when creator attributes are present' do - it 'strips trailing whitespace from the creator signature email' do - petition_params[:creator_attributes] = { email: ' woo@example.com ' } - subject.create_petition - expect(petition.creator.email).to eq 'woo@example.com' - end - - it 'assigns the remote_ip of the request to the ip_address of the creator signature' do - petition_params[:creator_attributes] = { email: ' woo@example.com ' } - subject.create_petition - expect(petition.creator.ip_address).to eq '192.168.0.1' - end - - it 'assumes that the creator wants notify_by_email to be true' do - petition_params[:creator_attributes] = { email: ' woo@example.com ' } - subject.create_petition - expect(petition.creator.notify_by_email).to eq true - end - end - - context 'when creator attributes are not present' do - it 'does not cause a creator to appear' do - subject.create_petition - expect(petition.creator).to be_nil - end - end - - describe 'when the stage is "done"' do - let(:stage) { 'done' } - - it 'tries to save the petition object' do - expect(petition).to receive(:save) - subject.create_petition - end - - it 'returns true if the petition object can be saved' do - allow(petition).to receive(:save).and_return true - expect(subject.create_petition).to eq true - end - - it 'returns false if the petition object can not be saved' do - allow(petition).to receive(:save).and_return false - expect(subject.create_petition).to eq false - end - end - - describe 'when the stage is not "done"' do - let(:stage) { 'replay-petition' } - - it 'returns false' do - expect(subject.create_petition).to eq false - end - - it 'does not try to save the petition' do - expect(subject.petition).not_to receive(:save) - subject.create_petition - end - end - end - - describe 'stages' do - extend StagedObjectHelpers - - let(:creator_params) do - { - :name => 'John Mcenroe', :email => 'john@example.com', - :postcode => 'SE3 4LL', :location_code => 'GB', - :uk_citizenship => '1' - } - end - let(:petition_params) do - { - :action => 'Save the planet', - :background => 'Limit temperature rise at two degrees', - :additional_details => 'Global warming is upon us', - :creator_attributes => creator_params - } - end - - describe '#stage' do - context 'when there are no errors on the petition' do - context 'before attempting to create the petition' do - for_stage 'petition', next_is: 'replay-petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'creator', next_is: 'replay-email', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-email', next_is: 'done', back_is: 'creator', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - - context 'after attempting to create the petition' do - before { subject.create_petition } - - for_stage 'petition', next_is: 'replay-petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'creator', next_is: 'replay-email', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-email', next_is: 'done', back_is: 'creator', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - end - - context 'when there are errors on the petition' do - context 'around the "petition" UI' do - before { petition_params.delete(:action) } - - context 'before attempting to create the petition' do - for_stage 'petition', next_is: 'petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'creator', next_is: 'replay-email', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-email', next_is: 'done', back_is: 'creator', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - - context 'after attempting to create the petition' do - before { subject.create_petition } - - for_stage 'petition', next_is: 'petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'creator', next_is: 'replay-email', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'replay-email', next_is: 'petition', back_is: 'creator', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'petition', back_is: 'petition', not_moving_is: 'petition' - end - end - - context 'around the "creator" UI' do - before { creator_params.delete(:postcode) } - - context 'before attempting to create the petition' do - for_stage 'petition', next_is: 'replay-petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'creator', next_is: 'creator', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-email', next_is: 'done', back_is: 'creator', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - - context 'after attempting to create the petition' do - before { subject.create_petition } - - for_stage 'petition', next_is: 'replay-petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'creator', next_is: 'creator', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-email', next_is: 'creator', back_is: 'creator', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'creator', back_is: 'creator', not_moving_is: 'creator' - end - end - - context 'around the "replay email" UI' do - before { creator_params.delete(:email) } - - # NOTE: the creator stage also checks for errors on email, so - # we need to be aware that errors on the "replay email" UI will - # also trigger the creator stage. - - context 'before attempting to create the petition' do - for_stage 'petition', next_is: 'replay-petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'creator', next_is: 'creator', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-email', next_is: 'replay-email', back_is: 'creator', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - - context 'after attempting to create the petition' do - before { subject.create_petition } - for_stage 'petition', next_is: 'replay-petition', back_is: 'petition', not_moving_is: 'petition' - for_stage 'creator', next_is: 'creator', back_is: 'replay-petition', not_moving_is: 'creator' - for_stage 'replay-petition', next_is: 'creator', back_is: 'petition', not_moving_is: 'replay-petition' - for_stage 'replay-email', next_is: 'replay-email', back_is: 'creator', not_moving_is: 'replay-email' - # NOTE: ideally this would be 'replay-email', but we can't - # tell the difference between a 'creator' failure and a - # 'replay-email' failure if we started from 'done'. - # TODO: make it so we can? - for_stage 'done', next_is: 'creator', back_is: 'creator', not_moving_is: 'creator' - end - end - end - end - end -end diff --git a/spec/models/staged/petition_signer_spec.rb b/spec/models/staged/petition_signer_spec.rb deleted file mode 100644 index b24d1255d..000000000 --- a/spec/models/staged/petition_signer_spec.rb +++ /dev/null @@ -1,133 +0,0 @@ -require 'rails_helper' - -RSpec.describe Staged::PetitionSigner, type: :model do - let(:signature_params) { {} } - let(:request) { double(remote_ip: '192.168.0.1') } - let(:move) { '' } - let(:stage) { '' } - let(:petition) { FactoryGirl.create(:open_petition) } - subject { described_class.manage(signature_params, request, petition, stage, move) } - let(:signature) { subject.signature } - - describe '#create_signature' do - it 'strips trailing whitespace from the email' do - signature_params[:email] = ' woo@example.com ' - subject.create_signature - expect(signature.email).to eq 'woo@example.com' - end - - describe 'when the stage is "done"' do - let(:stage) { 'done' } - - it 'tries to save the signature object' do - expect(signature).to receive(:save) - subject.create_signature - end - - it 'returns true if the signature object can be saved' do - allow(signature).to receive(:save).and_return true - expect(subject.create_signature).to eq true - end - - it 'returns false if the signature object can not be saved' do - allow(signature).to receive(:save).and_return false - expect(subject.create_signature).to eq false - end - - it 'assigns the remote_ip of the request to the ip_address of the signature' do - allow(signature).to receive(:save).and_return true - expect(subject.create_signature).to eq true - expect(signature.ip_address).to eq '192.168.0.1' - end - end - - describe 'when the stage is not "done"' do - let(:stage) { 'replay-email' } - - it 'returns false' do - expect(subject.create_signature).to eq false - end - - it 'does not try to save the signature' do - expect(subject.signature).not_to receive(:save) - subject.create_signature - end - end - end - - describe 'stages' do - let(:signature_params) do - { - :name => 'John Mcenroe', :email => 'john@example.com', - :postcode => 'SE3 4LL', :location_code => 'GB', - :uk_citizenship => '1' - } - end - - describe '#stage' do - extend StagedObjectHelpers - - context 'when there are no errors on the signature' do - context 'before attempting to create the signature' do - for_stage 'signer', next_is: 'replay-email', back_is: 'signer', not_moving_is: 'signer' - for_stage 'replay-email', next_is: 'done', back_is: 'signer', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - - context 'after attempting to create the signature' do - before { subject.create_signature } - - for_stage 'signer', next_is: 'replay-email', back_is: 'signer', not_moving_is: 'signer' - for_stage 'replay-email', next_is: 'done', back_is: 'signer', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - end - - context 'when there are errors on the signature' do - context 'around the "signer" UI' do - before { signature_params.delete(:name) } - - context 'before attempting to create the petition' do - for_stage 'signer', next_is: 'signer', back_is: 'signer', not_moving_is: 'signer' - for_stage 'replay-email', next_is: 'done', back_is: 'signer', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - - context 'after attempting to create the signature' do - before { subject.create_signature } - - for_stage 'signer', next_is: 'signer', back_is: 'signer', not_moving_is: 'signer' - for_stage 'replay-email', next_is: 'signer', back_is: 'signer', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'signer', back_is: 'signer', not_moving_is: 'signer' - end - end - - context 'around the "replay email" UI' do - before { signature_params.delete(:email) } - - # NOTE: the signer stage also checks for errors on email, so - # we need to be aware that errors on the "replay email" UI will - # also trigger the signer stage. - - context 'before attempting to create the petition' do - for_stage 'signer', next_is: 'signer', back_is: 'signer', not_moving_is: 'signer' - for_stage 'replay-email', next_is: 'replay-email', back_is: 'signer', not_moving_is: 'replay-email' - for_stage 'done', next_is: 'done', back_is: 'done', not_moving_is: 'done' - end - - context 'after attempting to create the petition' do - before { subject.create_signature } - - for_stage 'signer', next_is: 'signer', back_is: 'signer', not_moving_is: 'signer' - for_stage 'replay-email', next_is: 'replay-email', back_is: 'signer', not_moving_is: 'replay-email' - # NOTE: ideally this would be 'replay-email', but we can't - # tell the difference between a 'signer' failure and a - # 'replay-email' failure if we started from 'done'. - # TODO: make it so we can? - for_stage 'done', next_is: 'signer', back_is: 'signer', not_moving_is: 'signer' - end - end - end - end - end -end diff --git a/spec/support/staged_object_helpers.rb b/spec/support/staged_object_helpers.rb deleted file mode 100644 index 43c25893c..000000000 --- a/spec/support/staged_object_helpers.rb +++ /dev/null @@ -1,25 +0,0 @@ -module StagedObjectHelpers - def for_stage(name, next_is:, back_is:, not_moving_is:) - context "and the previous_stage was \"#{name}\"" do - let(:stage) { name } - context 'and we are moving forwards' do - let(:move) { 'next' } - it "is \"#{next_is}\"" do - expect(subject.stage).to eq next_is - end - end - context 'and we are moving backwards' do - let(:move) { 'back' } - it "is \"#{back_is}\"" do - expect(subject.stage).to eq back_is - end - end - context 'and we are not moving' do - let(:move) { nil } - it "is \"#{not_moving_is}\"" do - expect(subject.stage).to eq not_moving_is - end - end - end - end -end