Module: PartyAddresses
- Extended by:
- ActiveSupport::Concern
- Included in:
- Party
- Defined in:
- app/models/concerns/party_addresses.rb
Overview
Address delegation, catalog/store helpers, and visit-derived location fields.
== Country / locale / timezone resolution chain
These three derived attributes have multi-level overrides that depend on the
concrete Party subclass and which underlying store/customer is reachable:
countryParty (first address) -> Customer (+ catalog store fallback) ->
Contact (+ customer catalog fallback)localeParty (I18n.locale) -> Customer (catalog-derived) ->
Contact (customer.locale)timezoneParty (timezone_name column) -> Contact (+ customer.timezone fallback)
Customer overrides are in CustomerDisplay; Contact overrides are in
ContactDisplay. The Party-level methods here serve as the bottom of the chain
and the default for any Party that is neither a Customer nor a Contact.
Instance Method Summary collapse
- #addresses_options_for_select ⇒ Object
- #can_change_country? ⇒ Boolean
- #catalog_country ⇒ Object
- #catalog_country_iso ⇒ Object
- #catalog_country_iso3 ⇒ Object
- #city ⇒ Object
- #country ⇒ Object
- #country_iso ⇒ Object
- #country_iso3 ⇒ Object
- #first_address ⇒ Object
- #flag ⇒ Object
- #location_name ⇒ Object
- #most_recent_visit ⇒ Object
- #new_address ⇒ Object
- #store ⇒ Object
- #street1 ⇒ Object
- #street2 ⇒ Object
- #street3 ⇒ Object
- #timezone ⇒ Object
- #unique_addresses(include_address_id = nil) ⇒ Object
- #visit_data(attribute) ⇒ Object
- #visit_location ⇒ Object
- #zip ⇒ Object
Instance Method Details
#addresses_options_for_select ⇒ Object
44 45 46 |
# File 'app/models/concerns/party_addresses.rb', line 44 def addresses.map { |add| [add.full_address(true, ', '), add.id] } end |
#can_change_country? ⇒ Boolean
132 133 134 |
# File 'app/models/concerns/party_addresses.rb', line 132 def can_change_country? bot_id.blank? || state == 'guest' || (state == 'lead_qualify' && main_address.nil?) end |
#catalog_country ⇒ Object
86 87 88 89 |
# File 'app/models/concerns/party_addresses.rb', line 86 def catalog_country cat = catalog || try(:customer)&.catalog cat&.country end |
#catalog_country_iso ⇒ Object
95 96 97 |
# File 'app/models/concerns/party_addresses.rb', line 95 def catalog_country_iso catalog_country&.iso end |
#catalog_country_iso3 ⇒ Object
91 92 93 |
# File 'app/models/concerns/party_addresses.rb', line 91 def catalog_country_iso3 catalog_country&.iso3 end |
#city ⇒ Object
54 55 56 |
# File 'app/models/concerns/party_addresses.rb', line 54 def city first_address&.city || visit_data(:city) end |
#country ⇒ Object
74 75 76 |
# File 'app/models/concerns/party_addresses.rb', line 74 def country first_address&.country end |
#country_iso ⇒ Object
82 83 84 |
# File 'app/models/concerns/party_addresses.rb', line 82 def country_iso country&.iso end |
#country_iso3 ⇒ Object
78 79 80 |
# File 'app/models/concerns/party_addresses.rb', line 78 def country_iso3 country&.iso3 end |
#first_address ⇒ Object
48 49 50 51 52 |
# File 'app/models/concerns/party_addresses.rb', line 48 def first_address addresses.first rescue StandardError nil end |
#flag ⇒ Object
121 122 123 124 |
# File 'app/models/concerns/party_addresses.rb', line 121 def flag cat = catalog || try(:customer)&.catalog || Catalog.us_catalog { 1 => 'us', 2 => 'ca' }[cat.store_id] end |
#location_name ⇒ Object
105 106 107 |
# File 'app/models/concerns/party_addresses.rb', line 105 def location_name (addr = main_address) ? "#{addr.city}, #{addr.state_code}" : nil end |
#most_recent_visit ⇒ Object
113 114 115 |
# File 'app/models/concerns/party_addresses.rb', line 113 def most_recent_visit @most_recent_visit ||= visits.order(started_at: :desc).first end |
#new_address ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'app/models/concerns/party_addresses.rb', line 24 def new_address addr = Address.new addr.party = self addr.country = store.country copy_first_address_defaults!(addr) addr.is_residential = is_homeowner? addr end |
#store ⇒ Object
126 127 128 129 130 |
# File 'app/models/concerns/party_addresses.rb', line 126 def store catalog.store rescue StandardError nil end |
#street1 ⇒ Object
62 63 64 |
# File 'app/models/concerns/party_addresses.rb', line 62 def street1 first_address&.street1 end |
#street2 ⇒ Object
66 67 68 |
# File 'app/models/concerns/party_addresses.rb', line 66 def street2 first_address&.street2 end |
#street3 ⇒ Object
70 71 72 |
# File 'app/models/concerns/party_addresses.rb', line 70 def street3 first_address&.street3 end |
#timezone ⇒ Object
99 100 101 102 103 |
# File 'app/models/concerns/party_addresses.rb', line 99 def timezone ActiveSupport::TimeZone.find_tzinfo(timezone_name) rescue TZInfo::InvalidTimezoneIdentifier ErrorReporting.warning("Invalid timezone #{timezone_name} for party id #{id}") end |
#unique_addresses(include_address_id = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/concerns/party_addresses.rb', line 33 def unique_addresses(include_address_id = nil) all_addresses = addresses.order(:street1) res = [] all_addresses.each do |address| (res << address) && next if address.id == include_address_id res << address unless res.any?(address) end res end |
#visit_data(attribute) ⇒ Object
117 118 119 |
# File 'app/models/concerns/party_addresses.rb', line 117 def visit_data(attribute) most_recent_visit.try(attribute.to_sym) end |
#visit_location ⇒ Object
109 110 111 |
# File 'app/models/concerns/party_addresses.rb', line 109 def visit_location most_recent_visit&.location_summary end |
#zip ⇒ Object
58 59 60 |
# File 'app/models/concerns/party_addresses.rb', line 58 def zip first_address&.zip_compact || visit_data(:postal_code) end |