Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forms refactoring - Tidy up #487

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/forms/waste_carriers_engine/address_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class AddressForm < BaseForm
attr_accessor :temp_address
attr_accessor :addresses

validates :addresses, presence: true

def submit(params)
# Assign the params for validation and pass them to the BaseForm method for updating
self.addresses = add_or_replace_address(params[:temp_address])
Expand All @@ -14,8 +16,6 @@ def submit(params)
super(attributes, params[:reg_identifier])
end

validates :addresses, presence: true

private

# Look up addresses based on the temp_postcode
Expand Down Expand Up @@ -51,7 +51,7 @@ def add_or_replace_address(selected_address_uprn)
address.assign_attributes(address_type: address_type)

# Update the transient object's nested addresses, replacing any existing address of the same type
updated_addresses = @transient_registration.addresses
updated_addresses = transient_registration.addresses
updated_addresses.delete(saved_address) if saved_address
updated_addresses << address
updated_addresses
Expand Down
2 changes: 1 addition & 1 deletion app/forms/waste_carriers_engine/bank_transfer_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.can_navigate_flexibly?
def initialize(transient_registration)
super

self.total_to_pay = @transient_registration.total_to_pay
self.total_to_pay = transient_registration.total_to_pay
end

def submit(params)
Expand Down
3 changes: 3 additions & 0 deletions app/forms/waste_carriers_engine/base_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def self.can_navigate_flexibly?
true
end

validates :reg_identifier, "waste_carriers_engine/reg_identifier": true
validate :transient_registration_valid?

def initialize(transient_registration)
run_callbacks :initialize do
# Get values from transient registration so form will be pre-filled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

module WasteCarriersEngine
class CannotRenewCompanyNoChangeForm < BaseForm
def initialize(transient_registration)
super
end

# Override BaseForm method as users shouldn't be able to submit this form
def submit; end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

module WasteCarriersEngine
class CannotRenewLowerTierForm < BaseForm
def initialize(transient_registration)
super
end

# Override BaseForm method as users shouldn't be able to submit this form
def submit; end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

module WasteCarriersEngine
class CannotRenewTypeChangeForm < BaseForm
def initialize(transient_registration)
super
end

# Override BaseForm method as users shouldn't be able to submit this form
def submit; end
end
Expand Down
21 changes: 11 additions & 10 deletions app/forms/waste_carriers_engine/cards_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ def self.can_navigate_flexibly?
false
end

validates(
:temp_cards,
numericality: {
only_integer: true,
greater_than_or_equal_to: 0,
less_than_or_equal_to: MAX_TEMP_CARDS
}
)

def initialize(transient_registration)
super
self.temp_cards = @transient_registration.temp_cards || 0

self.temp_cards = transient_registration.temp_cards || 0
cintamani marked this conversation as resolved.
Show resolved Hide resolved
end

def submit(params)
Expand All @@ -26,14 +36,5 @@ def submit(params)

super(attributes, params[:reg_identifier])
end

validates(
:temp_cards,
numericality: {
only_integer: true,
greater_than_or_equal_to: 0,
less_than_or_equal_to: MAX_TEMP_CARDS
}
)
end
end
7 changes: 4 additions & 3 deletions app/forms/waste_carriers_engine/cbd_type_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ module WasteCarriersEngine
class CbdTypeForm < BaseForm
attr_accessor :registration_type

validates :registration_type, "waste_carriers_engine/registration_type": true

def initialize(transient_registration)
super
self.registration_type = @transient_registration.registration_type

self.registration_type = transient_registration.registration_type
end

def submit(params)
Expand All @@ -16,7 +19,5 @@ def submit(params)

super(attributes, params[:reg_identifier])
end

validates :registration_type, "waste_carriers_engine/registration_type": true
end
end
76 changes: 38 additions & 38 deletions app/forms/waste_carriers_engine/check_your_answers_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,6 @@ def self.custom_error_messages(attribute, *errors)
messages
end

def initialize(transient_registration)
super
self.business_type = @transient_registration.business_type
self.company_name = @transient_registration.company_name
self.company_no = @transient_registration.company_no
self.contact_address = @transient_registration.contact_address
self.contact_email = @transient_registration.contact_email
self.declared_convictions = @transient_registration.declared_convictions
self.first_name = @transient_registration.first_name
self.last_name = @transient_registration.last_name
self.location = @transient_registration.location
self.main_people = @transient_registration.main_people
self.phone_number = @transient_registration.phone_number
self.registered_address = @transient_registration.registered_address
self.registration_type = @transient_registration.registration_type
self.relevant_people = @transient_registration.relevant_people
self.tier = @transient_registration.tier

valid?
end

def submit(params)
attributes = {}

super(attributes, params[:reg_identifier])
end

def registration_type_changed?
@transient_registration.registration_type_changed?
end

def contact_name
"#{first_name} #{last_name}"
end

validates :business_type, "defra_ruby/validators/business_type": {
allow_overseas: true,
messages: custom_error_messages(:business_type, :inclusion)
Expand All @@ -80,28 +45,63 @@ def contact_name

validates_with KeyPeopleValidator

def initialize(transient_registration)
super
self.business_type = transient_registration.business_type
self.company_name = transient_registration.company_name
self.company_no = transient_registration.company_no
self.contact_address = transient_registration.contact_address
self.contact_email = transient_registration.contact_email
self.declared_convictions = transient_registration.declared_convictions
self.first_name = transient_registration.first_name
self.last_name = transient_registration.last_name
self.location = transient_registration.location
self.main_people = transient_registration.main_people
self.phone_number = transient_registration.phone_number
self.registered_address = transient_registration.registered_address
self.registration_type = transient_registration.registration_type
self.relevant_people = transient_registration.relevant_people
self.tier = transient_registration.tier

valid?
end

def submit(params)
attributes = {}

super(attributes, params[:reg_identifier])
end

def registration_type_changed?
transient_registration.registration_type_changed?
end

def contact_name
"#{first_name} #{last_name}"
end

private

def should_be_renewed
business_type_change_valid? && same_company_no?
end

def business_type_change_valid?
return true if @transient_registration.business_type_change_valid?
return true if transient_registration.business_type_change_valid?

errors.add(:business_type, :invalid_change)
false
end

def same_company_no?
return true unless @transient_registration.company_no_changed?
return true unless transient_registration.company_no_changed?

errors.add(:company_no, :changed)
false
end

def company_no_required?
@transient_registration.company_no_required?
transient_registration.company_no_required?
end
end
end
6 changes: 3 additions & 3 deletions app/forms/waste_carriers_engine/company_address_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class CompanyAddressForm < AddressForm
def initialize(transient_registration)
super
# We only use this for the correct microcopy
self.business_type = @transient_registration.business_type
self.temp_company_postcode = @transient_registration.temp_company_postcode
self.business_type = transient_registration.business_type
self.temp_company_postcode = transient_registration.temp_company_postcode

look_up_addresses
preselect_existing_address
Expand All @@ -22,7 +22,7 @@ def temp_postcode
end

def saved_address
@transient_registration.registered_address
transient_registration.registered_address
end

def address_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class CompanyAddressManualForm < ManualAddressForm
private

def saved_temp_postcode
@transient_registration.temp_company_postcode
transient_registration.temp_company_postcode
end

def existing_address
@transient_registration.registered_address
transient_registration.registered_address
end

def address_type
Expand Down
8 changes: 4 additions & 4 deletions app/forms/waste_carriers_engine/company_name_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module WasteCarriersEngine
class CompanyNameForm < BaseForm
attr_accessor :business_type, :company_name

validates :company_name, "waste_carriers_engine/company_name": true

def initialize(transient_registration)
super
# We only use this for the correct microcopy
self.business_type = @transient_registration.business_type
self.company_name = @transient_registration.company_name
self.business_type = transient_registration.business_type
self.company_name = transient_registration.company_name
end

def submit(params)
Expand All @@ -18,7 +20,5 @@ def submit(params)

super(attributes, params[:reg_identifier])
end

validates :company_name, "waste_carriers_engine/company_name": true
end
end
11 changes: 6 additions & 5 deletions app/forms/waste_carriers_engine/company_postcode_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ module WasteCarriersEngine
class CompanyPostcodeForm < PostcodeForm
attr_accessor :business_type, :temp_company_postcode

validates :temp_company_postcode, "waste_carriers_engine/postcode": true

def initialize(transient_registration)
super
self.temp_company_postcode = @transient_registration.temp_company_postcode

self.temp_company_postcode = transient_registration.temp_company_postcode
cintamani marked this conversation as resolved.
Show resolved Hide resolved
# We only use this for the correct microcopy
self.business_type = @transient_registration.business_type
self.business_type = transient_registration.business_type
end

def submit(params)
Expand All @@ -17,11 +20,9 @@ def submit(params)
attributes = { temp_company_postcode: temp_company_postcode }

# While we won't proceed if the postcode isn't valid, we always save it in case it's needed for manual entry
@transient_registration.update_attributes(attributes)
transient_registration.update_attributes(attributes)

super(attributes, params[:reg_identifier])
end

validates :temp_company_postcode, "waste_carriers_engine/postcode": true
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ module WasteCarriersEngine
class ConstructionDemolitionForm < BaseForm
attr_accessor :construction_waste

validates :construction_waste, "waste_carriers_engine/yes_no": true

def initialize(transient_registration)
super
self.construction_waste = @transient_registration.construction_waste

self.construction_waste = transient_registration.construction_waste
cintamani marked this conversation as resolved.
Show resolved Hide resolved
end

def submit(params)
Expand All @@ -16,7 +19,5 @@ def submit(params)

super(attributes, params[:reg_identifier])
end

validates :construction_waste, "waste_carriers_engine/yes_no": true
end
end
4 changes: 2 additions & 2 deletions app/forms/waste_carriers_engine/contact_address_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ContactAddressForm < AddressForm

def initialize(transient_registration)
super
self.temp_contact_postcode = @transient_registration.temp_contact_postcode
self.temp_contact_postcode = transient_registration.temp_contact_postcode

look_up_addresses
preselect_existing_address
Expand All @@ -19,7 +19,7 @@ def temp_postcode
end

def saved_address
@transient_registration.contact_address
transient_registration.contact_address
end

def address_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class ContactAddressManualForm < ManualAddressForm
private

def saved_temp_postcode
@transient_registration.temp_contact_postcode
transient_registration.temp_contact_postcode
end

def existing_address
@transient_registration.contact_address
transient_registration.contact_address
end

def address_type
Expand Down
Loading