Updated

app/models / petition_creator.rb

D
236 lines of codes
36 methods
7.3 complexity/method
3 churn
262.63 complexity
0 duplications
require 'postcode_sanitizer' class PetitionCreator
  1. PetitionCreator assumes too much for instance variable '@petition'
  2. PetitionCreator has no descriptive comment
  3. PetitionCreator has at least 5 instance variables
  4. PetitionCreator has at least 36 methods
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, :notify_by_email] 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
  1. PetitionCreator#save has a flog score of 39
  2. PetitionCreator#save has approx 19 statements
if moving_backwards? @stage = previous_stage and return false end unless valid? return false end if done? @petition = Petition.new do |p|
  1. PetitionCreator#save has the variable name 'p'
p.action = action p.background = background p.additional_details = additional_details p.build_creator do |c|
  1. PetitionCreator#save contains iterators nested 2 deep
  2. PetitionCreator#save has the variable name '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.notify_by_email = notify_by_email 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 def notify_by_email petition_creator_params[:notify_by_email] || "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
  1. PetitionCreator#validate_petition has a flog score of 35
  2. PetitionCreator#validate_petition has approx 6 statements
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
  1. PetitionCreator#validate_creator has a flog score of 59
  2. PetitionCreator#validate_creator has approx 11 statements
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?
  1. PetitionCreator#validate_creator calls 'email.present?' 2 times Locations: 0 1
errors.add(:location_code, :blank) unless location_code.present? errors.add(:uk_citizenship, :accepted) unless uk_citizenship == "1" errors.add(:postcode, :too_long, count: 255) if postcode.length > 255 if email.present?
  1. PetitionCreator#validate_creator calls 'email.present?' 2 times Locations: 0 1
email_validator.validate(self) end if location_code == "GB" errors.add(:postcode, :blank) unless postcode.present?
  1. PetitionCreator#validate_creator calls 'postcode.present?' 2 times Locations: 0 1
if postcode.present?
  1. PetitionCreator#validate_creator calls 'postcode.present?' 2 times Locations: 0 1
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
  1. PetitionCreator#email_validator doesn't depend on instance state (maybe move it to another class?)
EmailValidator.new(attributes: [:email]) end def postcode_validator
  1. PetitionCreator#postcode_validator doesn't depend on instance state (maybe move it to another class?)
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)
  1. PetitionCreator#send_email_to_gather_sponsors doesn't depend on instance state (maybe move it to another class?)
GatherSponsorsForPetitionEmailJob.perform_later(petition) end end