-
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.
Select contact address from postcode lookup results (#120)
https://eaflood.atlassian.net/browse/WC-112 Once users have entered a postcode to search with, we should let them choose the address they want from the results and save that as their contact address. This should work the same way as the registered address earlier in the form. We should go with the same inheritance model we've used for the people forms, postcode forms and manual address forms. CompanyAddressForm and ContactAddressForm are very similar, so any shared methods should be inherited from the AddressForm instead.
- Loading branch information
1 parent
311632d
commit a9b2600
Showing
17 changed files
with
333 additions
and
108 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddressFormsController < 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
class CompanyAddressFormsController < FormsController | ||
class CompanyAddressFormsController < AddressFormsController | ||
def new | ||
super(CompanyAddressForm, "company_address_form") | ||
end | ||
|
||
def create | ||
super(CompanyAddressForm, "company_address_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
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,68 @@ | ||
class AddressForm < BaseForm | ||
attr_accessor :temp_addresses | ||
attr_accessor :temp_address | ||
attr_accessor :addresses | ||
|
||
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]) | ||
attributes = { addresses: addresses } | ||
|
||
super(attributes, params[:reg_identifier]) | ||
end | ||
|
||
validates :addresses, presence: true | ||
|
||
private | ||
|
||
# Look up addresses based on the temp_postcode | ||
def look_up_addresses | ||
if temp_postcode.present? | ||
address_finder = AddressFinderService.new(temp_postcode) | ||
self.temp_addresses = address_finder.search_by_postcode | ||
else | ||
self.temp_addresses = [] | ||
end | ||
end | ||
|
||
# If an address has already been assigned to the transient registration, pre-select it | ||
def preselect_existing_address | ||
return unless @transient_registration.addresses.present? | ||
return unless saved_address.uprn.present? | ||
selected_address = temp_addresses.detect { |address| address["uprn"] == saved_address.uprn.to_s } | ||
self.temp_address = selected_address["uprn"] if selected_address.present? | ||
end | ||
|
||
def add_or_replace_address(selected_address_uprn) | ||
return if selected_address_uprn.blank? | ||
|
||
data = temp_addresses.detect { |address| address["uprn"] == selected_address_uprn } | ||
address = Address.create_from_os_places_data(data) | ||
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.delete(saved_address) if saved_address | ||
updated_addresses << address | ||
updated_addresses | ||
end | ||
|
||
# Methods which are called in this class but defined in subclasses | ||
# We should throw descriptive errors in case an additional subclass of ManualAddressForm is ever added | ||
|
||
def temp_postcode | ||
implemented_in_subclass | ||
end | ||
|
||
def saved_address | ||
implemented_in_subclass | ||
end | ||
|
||
def address_type | ||
implemented_in_subclass | ||
end | ||
|
||
def implemented_in_subclass | ||
raise NotImplementedError, "This #{self.class} cannot respond to:" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
class ContactAddressForm < BaseForm | ||
# TODO: Define accessible attributes, eg attr_accessor :field | ||
class ContactAddressForm < AddressForm | ||
attr_accessor :temp_contact_postcode | ||
|
||
def initialize(transient_registration) | ||
super | ||
# TODO: Define params to get from transient_registration, eg self.field = @transient_registration.field | ||
self.temp_contact_postcode = @transient_registration.temp_contact_postcode | ||
|
||
look_up_addresses | ||
preselect_existing_address | ||
end | ||
|
||
def submit(params) | ||
# Assign the params for validation and pass them to the BaseForm method for updating | ||
# TODO: Define allowed params, eg self.field = params[:field] | ||
# TODO: Include attributes to update in the attributes hash, eg { field: field } | ||
attributes = {} | ||
private | ||
|
||
def temp_postcode | ||
temp_contact_postcode | ||
end | ||
|
||
def saved_address | ||
@transient_registration.contact_address | ||
end | ||
|
||
super(attributes, params[:reg_identifier]) | ||
def address_type | ||
"CONTACT" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
<%= render("shared/back", back_path: back_contact_address_forms_path(@contact_address_form.reg_identifier)) %> | ||
|
||
<% if @contact_address_form.errors.any? %> | ||
|
||
<h1 class="heading-large"><%= t(".error_heading") %></h1> | ||
|
||
<% @contact_address_form.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
<% else %> | ||
<div class="text"> | ||
<%= form_for(@contact_address_form) do |f| %> | ||
<%= render("shared/errors", object: @contact_address_form) %> | ||
|
||
<h1 class="heading-large"><%= t(".heading") %></h1> | ||
|
||
<div class="form-group"> | ||
<label class="form-label"><%= t(".postcode_label") %></label> | ||
<span class="postcode"><%= @contact_address_form.temp_contact_postcode %></span> | ||
<%= link_to(t(".postcode_change_link"), back_contact_address_forms_path(@contact_address_form.reg_identifier)) %> | ||
</div> | ||
|
||
<%= render("shared/select_address", form: @contact_address_form, f: f) %> | ||
|
||
<div class="form-group"> | ||
<%= link_to(t(".manual_address_link"), skip_to_manual_address_contact_address_forms_path(@contact_address_form.reg_identifier)) %> | ||
</div> | ||
|
||
<%= f.hidden_field :reg_identifier, value: @contact_address_form.reg_identifier %> | ||
<div class="form-group"> | ||
<%= f.submit t(".next_button"), class: "button" %> | ||
</div> | ||
<% end %> | ||
<% end %> | ||
<%= render("shared/os_terms_footer") %> | ||
</div> |
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,13 @@ | ||
<% if form.errors[:addresses].any? %> | ||
<div class="form-group form-group-error"> | ||
<% else %> | ||
<div class="form-group"> | ||
<% end %> | ||
<fieldset id="addresses"> | ||
<%= f.label :temp_address, t(".address_label"), class: "form-label" %> | ||
<%= f.select(:temp_address, | ||
options_for_select(form.temp_addresses.map { |a| [a["partial"], a["uprn"]] }, form.temp_address), | ||
{ include_blank: t(".address_blank_option", count: form.temp_addresses.length ) }, | ||
{ class: "form-control" }) %> | ||
</fieldset> | ||
</div> |
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
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.