Module: Spree::AddressesHelper

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

Instance Method Summary collapse

Instance Method Details

#address_default_countryObject



81
82
83
# File 'app/helpers/spree/addresses_helper.rb', line 81

def address_default_country
  @address_default_country ||= current_store.default_country
end

#address_field(form, method, address_id = 'b', &handler) ⇒ Object



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

def address_field(form, method, address_id = 'b', &handler)
   :div, id: [address_id, method].join, class: 'form-group checkout-content-inner-field has-float-label' do
    if handler
      yield
    else
      is_required = Spree::Address.required_fields.include?(method)
      method_name = I18n.t("activerecord.attributes.spree/address.#{method}")
      required = Spree.t(:required)
      form.text_field(method,
                      class: ['form-control'].compact,
                      required: is_required,
                      placeholder: is_required ? "#{method_name} #{required}" : method_name,
                      aria: { label: method_name }) +
        form.label(method_name,
                   is_required ? "#{method_name} #{required}" : method_name)
    end
  end
end

#address_state(form, country, address_id = 'b') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/spree/addresses_helper.rb', line 38

def address_state(form, country, address_id = 'b')
  country ||= address_default_country
  have_states = country.states.any?
  state_elements = [
    form.collection_select(:state_id, checkout_zone_applicable_states_for(country).sort_by(&:name),
                          :id, :name,
                           { prompt: Spree.t(:state) },
                           class: ['spree-flat-select'].compact,
                           aria: { label: Spree.t(:state) },
                           disabled: !have_states) +
      form.text_field(:state_name,
                      class: ['form-control'].compact,
                      aria: { label: Spree.t(:state) },
                      disabled: have_states,
                      placeholder: Spree.t(:state) + " #{Spree.t(:required)}") +
      form.label(Spree.t(:state).downcase,
                 raw(Spree.t(:state) + (:abbr, " #{Spree.t(:required)}")),
                 class: [have_states ? 'state-select-label' : nil, ' '].compact,
                 id: address_id + '_state_label')
  ].join.tr('"', "'").delete("\n")

   :span, class: 'd-block position-relative' do
    state_elements.html_safe
  end
end

#address_zipcode(form, country, address_id = 'b') ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/spree/addresses_helper.rb', line 23

def address_zipcode(form, country, address_id = 'b')
  country ||= address_default_country
  is_required = country.zipcode_required?
  method_name = Spree.t(:zipcode)
  required = Spree.t(:required)
  form.text_field(:zipcode,
                  class: ['form-control'].compact,
                  required: is_required,
                  placeholder: is_required ? "#{method_name} #{required}" : method_name,
                  aria: { label: Spree.t(:zipcode) }) +
    form.label(:zipcode,
               is_required ? "#{method_name} #{required}" : method_name,
               id: address_id + '_zipcode_label')
end

#checkout_zone_applicable_states_for(country) ⇒ Object



77
78
79
# File 'app/helpers/spree/addresses_helper.rb', line 77

def checkout_zone_applicable_states_for(country)
  current_store.states_available_for_checkout(country)
end

#user_available_addressesObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/spree/addresses_helper.rb', line 64

def user_available_addresses
  @user_available_addresses ||= begin
    return [] unless try_spree_current_user

    states = current_store.countries_available_for_checkout.each_with_object([]) do |country, memo|
      memo << current_store.states_available_for_checkout(country)
    end.flatten

    try_spree_current_user.addresses.
      where(country_id: states.pluck(:country_id).uniq)
  end
end