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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/workarea/addresses_helper.rb', line 33

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

#is_usa_only?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'app/helpers/workarea/addresses_helper.rb', line 20

def is_usa_only?

  orders = current_order rescue nil
  return false if orders.blank? || orders.items.blank?

  usa_only_attribute = Workarea.config.shipping_attributes[:usa_only]
  return product_details(orders).map{ |item| item[:en][usa_only_attribute] }.flatten.include?("true")
end

#product_details(orders) ⇒ Object



29
30
31
# File 'app/helpers/workarea/addresses_helper.rb', line 29

def product_details(orders)
  @product_details ||= Catalog::Product.in(id: orders.items.to_a.map(&:product_id)).pluck(:details)
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