Module: AddressesHelper

Defined in:
app/helpers/addresses_helper.rb

Overview

View helper: addresses.

Instance Method Summary collapse

Instance Method Details

#address_autocomplete_data(options = {}) ⇒ Object

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']) %>



7
8
9
10
11
12
13
14
15
# File 'app/helpers/addresses_helper.rb', line 7

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_optionsObject



38
39
40
41
42
43
# File 'app/helpers/addresses_helper.rb', line 38

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_jsonObject

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



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

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

#format_for_checkout_display(address) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/addresses_helper.rb', line 61

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 = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/addresses_helper.rb', line 45

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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/addresses_helper.rb', line 79

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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/addresses_helper.rb', line 101

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) ⇒ Object



94
95
96
97
98
99
# File 'app/helpers/addresses_helper.rb', line 94

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) ⇒ Object

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 } } ]



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

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_codeObject

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



18
19
20
# File 'app/helpers/addresses_helper.rb', line 18

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

#setup_address(address) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'app/helpers/addresses_helper.rb', line 28

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