Module: AddressesHelper
- Defined in:
- app/helpers/addresses_helper.rb
Overview
View helper: addresses.
Instance Method Summary collapse
-
#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']) %>.
-
#address_type_options ⇒ Array<Array(String, String)>
Options for the address type select (residential, commercial, etc.).
-
#autocomplete_countries_json ⇒ String
Returns autocomplete country restriction with the user's locale country first.
-
#format_for_checkout_display(address) ⇒ String?
Renders an HTML address block for the checkout page, including curbside pickup instructions for warehouse addresses.
-
#format_for_display(address, params = {}) ⇒ String?
Renders a multi-line HTML address block, optionally with an edit link.
-
#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.
-
#google_map_multiple(addresses) ⇒ Array<Hash>?
Builds Google Maps marker data for a collection of addresses.
-
#google_map_multiple_by_ids(address_ids) ⇒ Array<Hash>?
Builds Google Maps marker data for the addresses with the given IDs.
-
#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 } } ].
-
#locale_country_code ⇒ String
Returns the country code from the current locale (e.g., 'us' from 'en-US').
-
#setup_address(address) ⇒ Address
Flags an address as the default billing, shipping, and mailing address when its party has no other addresses.
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']) %>
12 13 14 15 16 17 18 19 20 |
# File 'app/helpers/addresses_helper.rb', line 12 def address_autocomplete_data( = {}) countries = [: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_options ⇒ Array<Array(String, String)>
Options for the address type select (residential, commercial, etc.).
55 56 57 58 59 60 |
# File 'app/helpers/addresses_helper.rb', line 55 def [['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_json ⇒ String
Returns autocomplete country restriction with the user's locale country first.
e.g. en-US → '["us","ca"]', en-CA → '["ca","us"]'
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.
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| content_tag(: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 << content_tag(: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.
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) = { include_recipient: true, with_country: true } .merge!(params) address_block = [] address.render_to() { |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.
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| content_tag(: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 << content_tag(: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.
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.
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 } } ]
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_code ⇒ String
Returns the country code from the current locale (e.g., 'us' from 'en-US').
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.
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 |