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:

  • country Party (first address) -> Customer (+ catalog store fallback) ->
    Contact (+ customer catalog fallback)
  • locale Party (I18n.locale) -> Customer (catalog-derived) ->
    Contact (customer.locale)
  • timezone Party (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.

See Also:

Instance Method Summary collapse

Instance Method Details

#addresses_options_for_selectObject



44
45
46
# File 'app/models/concerns/party_addresses.rb', line 44

def addresses_options_for_select
  addresses.map { |add| [add.full_address(true, ', '), add.id] }
end

#can_change_country?Boolean

Returns:

  • (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_countryObject



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_isoObject



95
96
97
# File 'app/models/concerns/party_addresses.rb', line 95

def catalog_country_iso
  catalog_country&.iso
end

#catalog_country_iso3Object



91
92
93
# File 'app/models/concerns/party_addresses.rb', line 91

def catalog_country_iso3
  catalog_country&.iso3
end

#cityObject



54
55
56
# File 'app/models/concerns/party_addresses.rb', line 54

def city
  first_address&.city || visit_data(:city)
end

#countryObject



74
75
76
# File 'app/models/concerns/party_addresses.rb', line 74

def country
  first_address&.country
end

#country_isoObject



82
83
84
# File 'app/models/concerns/party_addresses.rb', line 82

def country_iso
  country&.iso
end

#country_iso3Object



78
79
80
# File 'app/models/concerns/party_addresses.rb', line 78

def country_iso3
  country&.iso3
end

#first_addressObject



48
49
50
51
52
# File 'app/models/concerns/party_addresses.rb', line 48

def first_address
  addresses.first
rescue StandardError
  nil
end

#flagObject



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_nameObject



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_visitObject



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_addressObject



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

#storeObject



126
127
128
129
130
# File 'app/models/concerns/party_addresses.rb', line 126

def store
  catalog.store
rescue StandardError
  nil
end

#street1Object



62
63
64
# File 'app/models/concerns/party_addresses.rb', line 62

def street1
  first_address&.street1
end

#street2Object



66
67
68
# File 'app/models/concerns/party_addresses.rb', line 66

def street2
  first_address&.street2
end

#street3Object



70
71
72
# File 'app/models/concerns/party_addresses.rb', line 70

def street3
  first_address&.street3
end

#timezoneObject



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_locationObject



109
110
111
# File 'app/models/concerns/party_addresses.rb', line 109

def visit_location
  most_recent_visit&.location_summary
end

#zipObject



58
59
60
# File 'app/models/concerns/party_addresses.rb', line 58

def zip
  first_address&.zip_compact || visit_data(:postal_code)
end