Module: AddressesHelper

Defined in:
app/helpers/addresses_helper.rb

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



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

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



36
37
38
39
40
41
# File 'app/helpers/addresses_helper.rb', line 36

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"]'



22
23
24
# File 'app/helpers/addresses_helper.rb', line 22

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

#format_for_checkout_display(address) ⇒ Object



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

def format_for_checkout_display(address)
  return unless address
  a = []
  ["#{address.is_warehouse ? 'FREE curbside pickup at ' : nil}#{address.company_name}", address.person_name].map(&:presence).compact.uniq.each do |name_part|
    a << (: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<i class='fa fa-angle-right ps-1 fa-xs'></i></a>".html_safe
    if address.is_warehouse_and_disabled?
      pickup_instructions += "<br/><small>currently disabled due to COVID-19 restrictions</small>".html_safe
    end
    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



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

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))}"
  end
  raw address_block.map(&:presence).compact.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
  a = []
  b = []
  [address.company_name, address.person_name].map(&:presence).compact.uniq.each do |name_part|
    a << (: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



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

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
100
101
# File 'app/helpers/addresses_helper.rb', line 94

def google_map_multiple_by_ids(address_ids)
  if address_ids.present?
    addresses = Address.where(id: address_ids)
    google_map_multiple(addresses)
  else
    nil
  end
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 } } ]



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

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



16
17
18
# File 'app/helpers/addresses_helper.rb', line 16

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

#setup_address(address) ⇒ Object



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

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