Module: AddressesHelper

Defined in:
app/helpers/addresses_helper.rb

Overview

View helper: addresses.

Instance Method Summary collapse

Instance Method Details

#address_autocomplete_data(options = {}) ⇒ Hash

Returns data attributes for address autocomplete inputs
Usage: <%= text_field_tag :street, nil, data: address_autocomplete_data %>
Or with options: <%= text_field_tag :street, nil, data: address_autocomplete_data(countries: ['us', 'ca']) %>

Parameters:

  • options (Hash) (defaults to: {})

    autocomplete configuration

Options Hash (options):

  • :countries (Array<String>)

    ISO country codes to restrict autocomplete to
    (default: the current locale's country)

Returns:

  • (Hash)

    data attributes for the input element



12
13
14
15
16
17
18
19
20
# File 'app/helpers/addresses_helper.rb', line 12

def address_autocomplete_data(options = {})
  countries = options[:countries].presence || [locale_country_code]
  {
    controller: 'address-autocomplete',
    address_autocomplete_countries_value: countries.to_json,
    address_autocomplete_check_locale_url_value: check_address_and_locale_my_cart_path,
    geo: 'name'
  }
end

#address_type_optionsArray<Array(String, String)>

Options for the address type select (residential, commercial, etc.).

Returns:

  • (Array<Array(String, String)>)

    [label, value] pairs for options_for_select



55
56
57
58
59
60
# File 'app/helpers/addresses_helper.rb', line 55

def address_type_options
  [['Residential', 'residential'],
   ['Commercial Building with Receiving Dock', 'commercial_building_with_receiving_dock'],
   ['Commercial Building without Receiving Dock', 'commercial_building_without_receiving_dock'],
   ['Construction Site/Limited Access', 'construction_site']]
end

#autocomplete_countries_jsonString

Returns autocomplete country restriction with the user's locale country first.
e.g. en-US → '["us","ca"]', en-CA → '["ca","us"]'

Returns:

  • (String)

    JSON array of ISO country codes



33
34
35
# File 'app/helpers/addresses_helper.rb', line 33

def autocomplete_countries_json
  ([locale_country_code] + %w[us ca]).uniq.to_json
end

#format_for_checkout_display(address) ⇒ String?

Renders an HTML address block for the checkout page, including curbside
pickup instructions for warehouse addresses.

Parameters:

  • address (Address, nil)

    the address to render; returns nil when nil

Returns:

  • (String, nil)

    HTML-safe address block, or nil when address is nil



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/addresses_helper.rb', line 91

def format_for_checkout_display(address)
  return unless address

  a = ["#{'FREE curbside pickup at ' if address.is_warehouse}#{address.company_name}", address.person_name].filter_map(&:presence).uniq.map do |name_part|
    (:span, name_part, class: 'customer-address-recipient d-block fw-semibold')
  end
  a << address.street1.to_s
  a << " #{address.street2}"
  a << address.street3.to_s
  a << ", #{address.city}, #{address.state_code} #{address.zip}" unless address.city.blank? || address.state_code.blank? || address.zip.blank?
  if address.is_warehouse
    pickup_instructions = "Please call (800) 875-5285 #{address.is_warehouse && address.country_iso == 'CA' ? 'x891' : 'x890'} and we will bring your order out to you.   <a href='https://www.google.com/maps/dir//590+Telser+Rd,+Lake+Zurich,+IL/@42.20863,-88.0691266,17z/data=!3m1!4b1!4m8!4m7!1m0!1m5!1m1!1s0x880fa26dbf499557:0x31cf2b1c131bacdd!2m2!1d-88.0669379!2d42.20863?dg=dbrw&newdg=1' target='_blank' rel='noreferrer' class='btn-link btn-sm p-0 fw-semibold'>Get Directions#{fa_icon('angle-right', family: :solid, class: 'ps-1 fa-xs')}</a>".html_safe
    pickup_instructions += "<br/><small>currently disabled due to COVID-19 restrictions</small>".html_safe if address.is_warehouse_and_disabled?
    a << (:span, pickup_instructions, class: 'customer-address-recipient d-block fw-semibold')
  end
  raw a.map(&:presence).uniq.join
end

#format_for_display(address, params = {}) ⇒ String?

Renders a multi-line HTML address block, optionally with an edit link.

Parameters:

  • address (Address, nil)

    the address to render; returns nil when nil

  • params (Hash) (defaults to: {})

    render options forwarded to Address#render_to

Options Hash (params):

  • :show_edit_link (Boolean)

    append an edit link (skipped for warehouses)

  • :edit_link_return_path (String)

    return path for the edit form
    (defaults to the current request's full path)

Returns:

  • (String, nil)

    HTML-safe address block, or nil when address is nil



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/addresses_helper.rb', line 70

def format_for_display(address, params = {})
  return unless address

  show_edit_link = params.delete(:show_edit_link)
  edit_link_return_path = params.delete(:edit_link_return_path)
  options = { include_recipient: true, with_country: true }
  options.merge!(params)
  address_block = []
  address.render_to(options) { |s| address_block << "#{h(s)}<br>" }
  if show_edit_link && !address.is_warehouse
    address_block << link_to(fa_icon('pen-to-square', text: 'Edit'),
                edit_address_path(address, return_path: edit_link_return_path || request.fullpath)).to_s
  end
  raw address_block.filter_map(&:presence).join
end

#format_for_new_checkout_display(address) ⇒ String?

Renders an HTML address block for the redesigned checkout page, with the
street lines right-aligned next to the recipient names.

Parameters:

  • address (Address, nil)

    the address to render; returns nil when nil

Returns:

  • (String, nil)

    HTML-safe address block, or nil when address is nil



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/helpers/addresses_helper.rb', line 114

def format_for_new_checkout_display(address)
  return unless address

  b = []
  a = [address.company_name, address.person_name].filter_map(&:presence).uniq.map do |name_part|
    (:span, name_part, class: 'customer-address-recipient fw-semibold')
  end
  b << address.street1.to_s
  b << " #{address.street2}"
  b << address.street3.to_s
  b << ", #{address.city}, #{address.state_code} #{address.zip}" unless address.city.blank? || address.state_code.blank? || address.zip.blank?
  a << (:span, b.map(&:presence).uniq.join, class: 'customer-address-recipient text-end ms-auto')
  raw a.map(&:presence).uniq.join
end

#google_map_multiple(addresses) ⇒ Array<Hash>?

Builds Google Maps marker data for a collection of addresses.

Parameters:

  • addresses (Array<Address>)

    the addresses to map

Returns:

  • (Array<Hash>, nil)

    marker hashes for the map, or nil when empty

See Also:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/helpers/addresses_helper.rb', line 146

def google_map_multiple(addresses)
  return if addresses.empty?

  results = addresses.map do |address|
    {
      latitude: address.lat,
      longitude: address.lng,
      title: address.party.try(:full_name).to_s.tr!('"', '\''),
      infowindow: render(partial: '/addresses/map_marker', locals: { address: address }),
      json: { party_id: address.party_id }
    }
  end
  google_map_multiple_coordinates results
end

#google_map_multiple_by_ids(address_ids) ⇒ Array<Hash>?

Builds Google Maps marker data for the addresses with the given IDs.

Parameters:

  • address_ids (Array<Integer>)

    IDs of the addresses to map

Returns:

  • (Array<Hash>, nil)

    marker hashes for the map, or nil when no IDs given

See Also:



134
135
136
137
138
139
# File 'app/helpers/addresses_helper.rb', line 134

def google_map_multiple_by_ids(address_ids)
  return if address_ids.blank?

  addresses = Address.where(id: address_ids)
  google_map_multiple(addresses)
end

#google_map_multiple_coordinates(data) ⇒ Array<Hash>

Feed me an array of hashes like this: [ { longitude: 2.11, latitude: 2.11, title: 'Some title', infowindow: 'Detailed when clicked', json: { some_values: 123 } } ]

Parameters:

  • data (Array<Hash>)

    coordinate hashes with :longitude, :latitude,
    :title, :infowindow, and an optional :json payload

Returns:

  • (Array<Hash>)

    marker hashes keyed for the Google Maps JS API



166
167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/addresses_helper.rb', line 166

def google_map_multiple_coordinates(data)
  data.map do |cord|
    {
      infoWindow: { content: cord[:infowindow] },
      title: cord[:title],
      lat: cord[:latitude],
      lng: cord[:longitude],
      party_id: cord.try(:json).try(:party_id)
    }
  end
end

#locale_country_codeString

Returns the country code from the current locale (e.g., 'us' from 'en-US').

Returns:

  • (String)

    downcased ISO country code (defaults to 'us' when no locale is set)



25
26
27
# File 'app/helpers/addresses_helper.rb', line 25

def locale_country_code
  (I18n.locale || 'en-US').to_s.split('-').last.downcase
end

#setup_address(address) ⇒ Address

Flags an address as the default billing, shipping, and mailing address
when its party has no other addresses.

Parameters:

  • address (Address)

    the address to set up

Returns:

  • (Address)

    the same address, with default flags set when applicable



42
43
44
45
46
47
48
49
50
# File 'app/helpers/addresses_helper.rb', line 42

def setup_address(address)
  address.tap do |a|
    if a.party && a.party.addresses.reload.empty?
      a.default_billing = true
      a.default_shipping = true
      a.default_mailing = true
    end
  end
end