Module: Workarea::AddressesHelper

Defined in:
app/helpers/workarea/addresses_helper.rb

Instance Method Summary collapse

Instance Method Details

#country_optionsObject



3
4
5
6
7
# File 'app/helpers/workarea/addresses_helper.rb', line 3

def country_options
  Workarea.config.countries.map do |country|
    [country.name, country.alpha2]
  end
end

#formatted_address(address) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/workarea/addresses_helper.rb', line 20

def formatted_address(address)
  pieces = {
    recipient: "#{address.first_name} #{address.last_name}\n#{address.company}".strip,
    street: "#{address.street} #{address.street_2}".strip,
    city: address.city,
    region: address.region_name,
    region_short: address.region,
    postalcode: address.postal_code,
    country: address.country.alpha2
  }

  address_format = address.country.address_format || Country['US'].address_format
  result = pieces.reduce(address_format) do |memo, (name, value)|
    memo.gsub(/{{#{name}}}/, html_escape(value.to_s))
  end

  if address.phone_number.present?
    formatted_phone = number_to_phone(
      address.phone_number,
      extension: address.phone_extension
    )

    result << "\n#{formatted_phone}"
  end

  result.gsub(/\n/, tag(:br)).html_safe
end

#region_optionsObject



9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/workarea/addresses_helper.rb', line 9

def region_options
  @region_options ||= Workarea.config.countries.reduce([]) do |memo, country|
    regions = country.subdivisions
                .map { |id, region| [region.translations[I18n.locale.to_s] || region.name, id] }
                .sort_by { |name, id| name || id }

    memo << [country.name, regions]
    memo
  end
end