Module: Spree::UserAddressBook

Extended by:
ActiveSupport::Concern
Included in:
UserMethods
Defined in:
app/models/concerns/spree/user_address_book.rb

Instance Method Summary collapse

Instance Method Details

#bill_address=(address) ⇒ Object



84
85
86
87
88
89
90
# File 'app/models/concerns/spree/user_address_book.rb', line 84

def bill_address=(address)
  if address
    save_in_address_book(address.attributes,
                         Spree::Config.automatic_default_address,
                         :billing)
  end
end

#bill_address_attributes=(attributes) ⇒ Object



92
93
94
# File 'app/models/concerns/spree/user_address_book.rb', line 92

def bill_address_attributes=(attributes)
  self.bill_address = Spree::Address.immutable_merge(bill_address, attributes)
end

#default_addressObject



42
43
44
45
# File 'app/models/concerns/spree/user_address_book.rb', line 42

def default_address
  Spree::Deprecation.warn "#default_address is deprecated. Please start using #ship_address."
  ship_address
end

#default_address=(address) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/models/concerns/spree/user_address_book.rb', line 52

def default_address=(address)
  Spree::Deprecation.warn(
    "#default_address= does not take Spree::Config.automatic_default_address into account and is deprecated. " \
    "Please use #ship_address=."
  )

  self.ship_address = address if address
end

#default_address_attributes=(attributes) ⇒ Object



61
62
63
64
65
66
67
68
# File 'app/models/concerns/spree/user_address_book.rb', line 61

def default_address_attributes=(attributes)
  # see "Nested Attributes Examples" section of http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
  # this #{fieldname}_attributes= method works with fields_for in the views
  # even without declaring accepts_nested_attributes_for
  Spree::Deprecation.warn "#default_address_attributes= is deprecated. Please use #ship_address_attributes=."

  self.default_address = Spree::Address.immutable_merge(ship_address, attributes)
end

#default_user_addressObject



47
48
49
50
# File 'app/models/concerns/spree/user_address_book.rb', line 47

def default_user_address
  Spree::Deprecation.warn "#default_user_address is deprecated. Please start using #default_user_ship_address."
  default_user_ship_address
end

#mark_default_address(address) ⇒ Object



160
161
162
163
164
165
166
167
# File 'app/models/concerns/spree/user_address_book.rb', line 160

def mark_default_address(address)
  Spree::Deprecation.warn(
    "#mark_default_address is deprecated and it sets the ship_address only. " \
    "Please use #mark_default_ship_address."
  )

  mark_default_ship_address(address)
end

#mark_default_bill_address(address) ⇒ Object



173
174
175
# File 'app/models/concerns/spree/user_address_book.rb', line 173

def mark_default_bill_address(address)
  user_addresses.mark_default(user_addresses.find_by(address: address), address_type: :billing)
end

#mark_default_ship_address(address) ⇒ Object



169
170
171
# File 'app/models/concerns/spree/user_address_book.rb', line 169

def mark_default_ship_address(address)
  user_addresses.mark_default(user_addresses.find_by(address: address))
end

#persist_order_address(order) ⇒ Object

saves order.ship_address and order.bill_address in address book sets ship_address to the default if automatic_default_address is set to true sets bill_address to the default if automatic_default_address is set to true and there is no ship_address if one address is nil, does not save that address



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/concerns/spree/user_address_book.rb', line 100

def persist_order_address(order)
  if order.ship_address
    address = save_in_address_book(
      order.ship_address.attributes,
      Spree::Config.automatic_default_address
    )
    self.ship_address_id = address.id if address&.persisted?
  end

  if order.bill_address
    address = save_in_address_book(
      order.bill_address.attributes,
      Spree::Config.automatic_default_address,
      :billing
    )
    self.bill_address_id = address.id if address&.persisted?
  end

  save! # In case the ship_address_id or bill_address_id was set
end

#remove_from_address_book(address_id) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'app/models/concerns/spree/user_address_book.rb', line 177

def remove_from_address_book(address_id)
  user_address = user_addresses.find_by(address_id: address_id)
  if user_address
    remove_user_address_reference(address_id)
    user_address.update(archived: true, default: false)
  else
    false
  end
end

#save_in_address_book(address_attributes, default = false, address_type = :shipping) ⇒ Object

Add an address to the user’s list of saved addresses for future autofill treated as value equality to de-dup among existing Addresses #default_address or not

Parameters:

  • address_attributes

    HashWithIndifferentAccess of attributes that will be

  • default (defaults to: false)

    set whether or not this address will show up from



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/models/concerns/spree/user_address_book.rb', line 126

def save_in_address_book(address_attributes, default = false, address_type = :shipping)
  return nil if address_attributes.blank?

  address_attributes = address_attributes.to_h.with_indifferent_access

  new_address = Spree::Address.factory(address_attributes)
  return new_address unless new_address.valid?

  first_one = user_addresses.empty?

  if address_attributes[:id].present? && new_address.id != address_attributes[:id]
    remove_from_address_book(address_attributes[:id])
  end

  user_address = prepare_user_address(new_address)
  user_addresses.mark_default(user_address, address_type: address_type) if default || first_one

  if persisted?
    user_address.save!

    # If these associations have already been accessed, they will be
    # caching the existing values.
    # user_addresses need to be reset to get the new ordering based on any changes
    # {default_,}user_address needs to be reset as its result is likely to have changed.
    user_addresses.reset
    association(:default_user_ship_address).reset
    association(:ship_address).reset
    association(:default_user_bill_address).reset
    association(:bill_address).reset
  end

  user_address.address
end

#ship_address=(address) ⇒ Object

saves address in address book sets address to the default if automatic_default_address is set to true if address is nil, does nothing and returns nil



73
74
75
76
77
78
# File 'app/models/concerns/spree/user_address_book.rb', line 73

def ship_address=(address)
  if address
    save_in_address_book(address.attributes,
                         Spree::Config.automatic_default_address)
  end
end

#ship_address_attributes=(attributes) ⇒ Object



80
81
82
# File 'app/models/concerns/spree/user_address_book.rb', line 80

def ship_address_attributes=(attributes)
  self.ship_address = Spree::Address.immutable_merge(ship_address, attributes)
end