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

Chore/ruby 3409 mongoid reference one #1574

Merged
merged 9 commits into from
Oct 17, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create
private

def transient_registration_attributes
params.fetch(:company_address_form, {}).permit(company_address: [:uprn])
params.fetch(:company_address_form, {}).permit(registered_address: [:uprn])
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def transient_registration_attributes
params
.fetch(:company_address_manual_form, {})
.permit(
company_address: %i[house_number address_line_1 address_line_2 town_city postcode country]
registered_address: %i[house_number address_line_1 address_line_2 town_city postcode country]
)
end

Expand Down
4 changes: 2 additions & 2 deletions app/forms/waste_carriers_engine/address_lookup_form_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def request_matching_addresses
end

def create_address(uprn, type)
return {} if uprn.blank?
return nil if uprn.blank?

data = temp_addresses.detect { |address| address["uprn"].to_i == uprn.to_i }
return {} unless data
return nil unless data

address = Address.create_from_os_places_data(data)
address.assign_attributes(address_type: type)
Expand Down
11 changes: 5 additions & 6 deletions app/forms/waste_carriers_engine/company_address_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

module WasteCarriersEngine
class CompanyAddressForm < AddressLookupFormBase
delegate :temp_company_postcode, :business_type, :company_address, to: :transient_registration
delegate :temp_company_postcode, :business_type, :registered_address, to: :transient_registration

alias existing_address company_address
alias postcode temp_company_postcode

validates :company_address, "waste_carriers_engine/address": true
validates :registered_address, "waste_carriers_engine/address": true

def submit(params)
company_address_params = params.fetch(:company_address, {})
company_address = create_address(company_address_params[:uprn], "REGISTERED")
address_params = params.fetch(:registered_address, {})
registered_address = create_address(address_params[:uprn], "REGISTERED")

super(company_address: company_address)
super(registered_address:)
end
end
end
13 changes: 7 additions & 6 deletions app/forms/waste_carriers_engine/company_address_manual_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ class CompanyAddressManualForm < ::WasteCarriersEngine::BaseForm
include CanClearAddressFinderError
include CanValidateManualAddress

delegate :overseas?, :company_address, :business_type, to: :transient_registration
delegate :house_number, :address_line_1, :address_line_2, to: :company_address, allow_nil: true
delegate :postcode, :town_city, :country, to: :company_address, allow_nil: true
delegate :overseas?, :registered_address, :business_type, to: :transient_registration
delegate :house_number, :address_line_1, :address_line_2, to: :registered_address, allow_nil: true
delegate :postcode, :town_city, :country, to: :registered_address, allow_nil: true

after_initialize :clean_address, unless: :saved_address_still_valid?

def submit(params)
address = Address.create_from_manual_entry(params[:company_address] || {}, transient_registration.overseas?)
address = Address.create_from_manual_entry(params[:registered_address] || {}, transient_registration.overseas?)
address.assign_attributes(address_type: "REGISTERED")

super(company_address: address)
super(registered_address: address)
end

private

def clean_address
# Prefill the existing address unless the postcode has changed from the existing address's postcode
transient_registration.company_address = Address.new(
transient_registration.registered_address = Address.new(
address_type: "REGISTERED",
postcode: transient_registration.temp_company_postcode
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def submit(params)
def clean_address
# Prefill the existing address unless the postcode has changed from the existing address's postcode
transient_registration.contact_address = Address.new(
address_type: "POSTAL",
postcode: transient_registration.temp_contact_postcode
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module WasteCarriersEngine
module CanHaveRegistrationAttributes
extend ActiveSupport::Concern
include Mongoid::Document
include CanReferenceSingleDocumentInCollection
include CanHaveTier

# Rubocop sees a module as a block, and as such is not very forgiving in how
Expand All @@ -23,11 +22,30 @@ module CanHaveRegistrationAttributes
# rubocop:disable Layout/LineLength
embeds_many :addresses, class_name: "WasteCarriersEngine::Address"

# This is our own custom association. See CanReferenceSingleDocumentInCollection for details
reference_one :contact_address, collection: :addresses, find_by: { address_type: "POSTAL" }
reference_one :company_address, collection: :addresses, find_by: { address_type: "REGISTERED" }
# TODO: Remove this and always use `company_address` rather than `registrered_address`
reference_one :registered_address, collection: :addresses, find_by: { address_type: "REGISTERED" }
# Define helper accessor and assignment methods for each address type
%w[contact registered].each do |address_type|

define_method(:"#{address_type}_address") do
# handle synonyms
address_type = "postal" if address_type == "contact"

addresses.where(address_type: address_type.upcase).first
end

define_method(:"#{address_type}_address=") do |address|
# handle synonyms
address_type = "postal" if address_type == "contact"

unless address.blank? || address.address_type == address_type.upcase
raise ArgumentError, "Attempted to set #{address_type} address with address of type #{address.address_type}"
end

# clear any existing address of this type
addresses.where(address_type: address_type.upcase).first&.destroy!

addresses << address
end
end

embeds_one :conviction_search_result, class_name: "WasteCarriersEngine::ConvictionSearchResult"
embeds_many :conviction_sign_offs, class_name: "WasteCarriersEngine::ConvictionSignOff"
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions app/models/waste_carriers_engine/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Address
field :firstName, as: :first_name, type: String
field :lastName, as: :last_name, type: String

validates :address_type, presence: true

def self.create_from_manual_entry(params, overseas)
address = Address.new

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
go_back_forms_path(@company_address_form.token)) %>
</p>

<%= form.fields_for :company_address do |f| %>
<%= render("waste_carriers_engine/shared/select_address", form: @company_address_form, address: :company_address, f: f) %>
<%= form.fields_for :registered_address do |f| %>
<%= render("waste_carriers_engine/shared/select_address", form: @company_address_form, address: :registered_address, f: f) %>
<% end %>

<p class="govuk-body">
Expand Down
3 changes: 2 additions & 1 deletion config/locales/forms/company_address_forms/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ en:
models:
waste_carriers_engine/company_address_form:
attributes:
company_address:
registered_address:
blank: "Select an address"
should_be_uk: "Provide a UK contact address"
1 change: 1 addition & 0 deletions config/locales/forms/contact_address_forms/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ en:
attributes:
contact_address:
blank: "Select an address"
should_be_uk: "Provide a UK contact address"
2 changes: 1 addition & 1 deletion spec/factories/forms/company_address_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:renewing_registration,
:has_required_data,
workflow_state: "company_address_form",
company_address: build(:address, :has_required_data, :registered),
registered_address: build(:address, :has_required_data, :registered),
temp_company_postcode: "FA4 3HT"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module WasteCarriersEngine
let(:valid_params) do
{
token: company_address_form.token,
company_address: {
registered_address: {
uprn: "340116"
}
}
Expand Down Expand Up @@ -63,7 +63,7 @@ module WasteCarriersEngine

describe "#temp_address" do
it "pre-selects the address" do
expect(company_address_form.company_address.uprn.to_s).to eq(transient_registration.addresses.where(address_type: "REGISTERED").first.uprn.to_s)
expect(company_address_form.registered_address.uprn.to_s).to eq(transient_registration.addresses.where(address_type: "REGISTERED").first.uprn.to_s)
end
end
end
Expand Down
Loading
Loading