Module: EffectiveAddressesHelper

Defined in:
app/helpers/effective_addresses_helper.rb

Instance Method Summary collapse

Instance Method Details

#effective_address_fields(form, method = 'billing', options = {}) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/effective_addresses_helper.rb', line 4

def effective_address_fields(form, method = 'billing', options = {})
  method = (method.to_s.include?('_address') ? method.to_s : "#{method}_address")

  required = (form.object._validators[method.to_sym].any? { |v| v.kind_of?(ActiveRecord::Validations::PresenceValidator) && (v.options[:if].blank? || (v.options[:if].respond_to?(:call) ? f.object.instance_exec(&v.options[:if]) : v.options[:if])) } rescue true)

  address = form.object.send(method) || form.object.addresses.build(category: method.to_s.gsub('_address', ''))
  effective_address_pre_select(address) if address.new_record?

  opts = { required: required, field_order: [:full_name, :address1, :address2, :city, :country_code, :state_code, :postal_code] }.merge(options).merge({:f => form, :address => address, :method => method})

  case form.class.name
  when 'Effective::FormBuilder'
    render partial: 'effective/addresses/form_with', locals: opts
  when 'SimpleForm::FormBuilder'
    render partial: 'effective/addresses/simple_form', locals: opts
  when 'Formtastic::FormBuilder'
    render partial: 'effective/addresses/formtastic', locals: opts
  else
    raise 'Unsupported FormBuilder.  You must use formtastic, simpleform or effective_bootstrap. Sorry.'
  end
end

#effective_address_pre_select(address) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/effective_addresses_helper.rb', line 26

def effective_address_pre_select(address)
  if EffectiveAddresses.pre_selected_country.present?
    address.country = EffectiveAddresses.pre_selected_country
    address.state = EffectiveAddresses.pre_selected_state if EffectiveAddresses.pre_selected_state.present?
  elsif defined?(Geocoder) && request.location.present?
    location = request.location
    address.country = location.country_code
    address.state = location.state_code
    address.postal_code = location.postal_code
    address.city = location.city
  end
end

#effective_address_regions_collection(regions = nil, resource: nil) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/effective_addresses_helper.rb', line 39

def effective_address_regions_collection(regions = nil, resource: nil)
  if regions.present?
    countries = regions
  elsif EffectiveAddresses.country_codes == :all
    countries = Carmen::Country.all
  else
    countries = Carmen::Country.all.select { |c| (EffectiveAddresses.country_codes || []).include?(c.code) }
  end

  if regions.blank? && EffectiveAddresses.country_codes_priority.present?
    countries = countries.reject { |c| EffectiveAddresses.country_codes_priority.include?(c.code) }
  end

  countries = countries.map { |c| [c.name, c.code] }.sort! { |a, b| a.first <=> b.first }

  if regions.blank? && EffectiveAddresses.country_codes_priority.present?
    countries.unshift(*
      EffectiveAddresses.country_codes_priority.map do |code|
        country = Carmen::Country.coded(code)
        [country.name, country.code]
      end + [['-------------------', '-', disabled: :disabled]]
    )
  end

  # Special behaviour if the address has CAN, we treat it as CA
  if regions.blank? && resource.present? && resource.country_code == 'CAN'
    ca = countries.index { |_, code| code == 'CA' }

    if ca.present?
      countries[ca] = ['Canada', 'CAN']
    end
  end

  countries
end