-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search for a contact address by UK postcode (#117)
* Search for a contact address by UK postcode https://eaflood.atlassian.net/browse/WC-112 "What's the address of the person we should contact?" Add forms to allow UK-based users to enter a postcode so they can search for a contact address. Selection and saving of the contact address will be added in a separate PR to keep things manageable. This should function the same way as the registered address forms earlier in the process. Non-UK users or users who choose to enter their addresses manually will see a separate form (to be worked on as a separate feature). * Set up contact postcode form Currently this is just a copy of the registered address postcode form with names swapped around (and some tweaks to the postcode validator to deal with the two different postcode fields). References to the manual contact address page have been commented out until that page is created. Next step is to refactor to remove masses of duplication. * Rename temp_postcode to temp_company_postcode This is the attribute used for looking up registered addresses. Now that we have two temporary postcodes, I've renamed this to be a bit clearer. * Rename and refactor postcode validator Make it less specific to certain attributes, and also reduce complexity of the validate method. * Add explanatory text to page * Postcode forms now inherit from PostcodeForm
- Loading branch information
1 parent
92f1699
commit 396ff6c
Showing
34 changed files
with
828 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
class CompanyPostcodeFormsController < FormsController | ||
class CompanyPostcodeFormsController < PostcodeFormsController | ||
def new | ||
super(CompanyPostcodeForm, "company_postcode_form") | ||
end | ||
|
||
def create | ||
super(CompanyPostcodeForm, "company_postcode_form") | ||
end | ||
|
||
def skip_to_manual_address | ||
set_transient_registration(params[:reg_identifier]) | ||
|
||
@transient_registration.skip_to_manual_address! if form_matches_state? | ||
redirect_to_correct_form | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class ContactPostcodeFormsController < PostcodeFormsController | ||
def new | ||
super(ContactPostcodeForm, "contact_postcode_form") | ||
end | ||
|
||
def create | ||
super(ContactPostcodeForm, "contact_postcode_form") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class PostcodeFormsController < FormsController | ||
def skip_to_manual_address | ||
set_transient_registration(params[:reg_identifier]) | ||
|
||
@transient_registration.skip_to_manual_address! if form_matches_state? | ||
redirect_to_correct_form | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,24 @@ | ||
class CompanyPostcodeForm < BaseForm | ||
attr_accessor :business_type, :temp_postcode | ||
class CompanyPostcodeForm < PostcodeForm | ||
attr_accessor :business_type, :temp_company_postcode | ||
|
||
def initialize(transient_registration) | ||
super | ||
self.temp_postcode = @transient_registration.temp_postcode | ||
self.temp_company_postcode = @transient_registration.temp_company_postcode | ||
# 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 | ||
self.temp_postcode = params[:temp_postcode] | ||
format_postcode | ||
attributes = { temp_postcode: temp_postcode } | ||
self.temp_company_postcode = params[:temp_company_postcode] | ||
format_postcode(temp_company_postcode) | ||
attributes = { temp_company_postcode: temp_company_postcode } | ||
|
||
# While we won't proceed if the postcode isn't valid, we should always save it in case it's needed for manual entry | ||
@transient_registration.update_attributes(attributes) | ||
|
||
super(attributes, params[:reg_identifier]) | ||
end | ||
|
||
validates_with TempPostcodeValidator | ||
|
||
private | ||
|
||
def format_postcode | ||
return unless temp_postcode.present? | ||
temp_postcode.upcase! | ||
temp_postcode.strip! | ||
end | ||
validates_with PostcodeValidator, fields: [:temp_company_postcode] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class ContactPostcodeForm < PostcodeForm | ||
attr_accessor :temp_contact_postcode | ||
|
||
def initialize(transient_registration) | ||
super | ||
self.temp_contact_postcode = @transient_registration.temp_contact_postcode | ||
end | ||
|
||
def submit(params) | ||
# Assign the params for validation and pass them to the BaseForm method for updating | ||
self.temp_contact_postcode = params[:temp_contact_postcode] | ||
format_postcode(temp_contact_postcode) | ||
attributes = { temp_contact_postcode: temp_contact_postcode } | ||
|
||
# While we won't proceed if the postcode isn't valid, we should always save it in case it's needed for manual entry | ||
@transient_registration.update_attributes(attributes) | ||
|
||
super(attributes, params[:reg_identifier]) | ||
end | ||
|
||
validates_with PostcodeValidator, fields: [:temp_contact_postcode] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class PostcodeForm < BaseForm | ||
private | ||
|
||
def format_postcode(postcode) | ||
return unless postcode.present? | ||
postcode.upcase! | ||
postcode.strip! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require "uk_postcode" | ||
|
||
class PostcodeValidator < ActiveModel::Validator | ||
def validate(record) | ||
return unless options[:fields].any? | ||
options[:fields].each do |field| | ||
validate_postcode_field(record, field) | ||
end | ||
end | ||
|
||
private | ||
|
||
def validate_postcode_field(record, field) | ||
return unless value_is_present?(record, field) | ||
return unless value_uses_correct_format?(record, field) | ||
postcode_returns_results?(record, field) | ||
end | ||
|
||
def value_is_present?(record, field) | ||
return true if record.send(field).present? | ||
record.errors.add(field, :blank) | ||
false | ||
end | ||
|
||
def value_uses_correct_format?(record, field) | ||
return true if UKPostcode.parse(record.send(field)).full_valid? | ||
record.errors.add(field, :wrong_format) | ||
false | ||
end | ||
|
||
def postcode_returns_results?(record, field) | ||
address_finder = AddressFinderService.new(record.send(field)) | ||
case address_finder.search_by_postcode | ||
when :not_found | ||
record.errors.add(field, :no_results) | ||
false | ||
when :error | ||
record.transient_registration.temp_os_places_error = true | ||
true | ||
else | ||
true | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.