Skip to content

Commit

Permalink
Forms refactoring - Registration Number form (#507)
Browse files Browse the repository at this point in the history
From: https://eaflood.atlassian.net/browse/RUBY-693

Use delegate and strong parameters strategy in Registration Number form
  • Loading branch information
cintamani committed Oct 10, 2019
1 parent 3d78cc3 commit 666b0af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ def new
def create
super(RegistrationNumberForm, "registration_number_form")
end

private

def transient_registration_attributes
params.require(:registration_number_form).permit(:company_no)
end
end
end
18 changes: 5 additions & 13 deletions app/forms/waste_carriers_engine/registration_number_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,23 @@

module WasteCarriersEngine
class RegistrationNumberForm < BaseForm
attr_accessor :company_no, :business_type
delegate :company_no, :business_type, to: :transient_registration

validates :company_no, "defra_ruby/validators/companies_house_number": true

def initialize(transient_registration)
super

self.company_no = transient_registration.company_no
# We only use this for the correct microcopy
self.business_type = transient_registration.business_type
end

def submit(params)
# Assign the params for validation and pass them to the BaseForm method for updating
# If param isn't set, use a blank string instead to avoid errors with the validator
self.company_no = params[:company_no] || ""
self.company_no = process_company_no(company_no) if company_no.present?
attributes = { company_no: company_no }
params[:company_no] = process_company_no(params[:company_no])

super(attributes)
super
end

private

def process_company_no(company_no)
return unless company_no.present?

number = company_no.to_s
# Should be 8 characters, so if it's not, add 0s to the start
number = "0#{number}" while number.length < 8
Expand Down

0 comments on commit 666b0af

Please sign in to comment.