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



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

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



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

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

#mark_default_bill_address(address) ⇒ Object



164
165
166
# File 'app/models/concerns/spree/user_address_book.rb', line 164

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

#mark_default_ship_address(address) ⇒ Object



160
161
162
# File 'app/models/concerns/spree/user_address_book.rb', line 160

def mark_default_ship_address(address)
  user_addresses.mark_default(user_addresses.find_by(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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/concerns/spree/user_address_book.rb', line 93

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



168
169
170
171
172
173
174
175
176
# File 'app/models/concerns/spree/user_address_book.rb', line 168

def remove_from_address_book(address_id)
  user_address = user_addresses.find_by(address_id:)
  if user_address
    remove_user_address_reference(address_id)
    user_address.destroy!
  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



119
120
121
122
123
124
125
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 119

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?
  user_address = prepare_user_address(new_address)

  if address_attributes[:id].present? && new_address.id != address_attributes[:id]
    if ship_address&.id == address_attributes[:id].to_i
      user_addresses.mark_default(user_address, address_type: :shipping)
    end

    if bill_address&.id == address_attributes[:id].to_i
      user_addresses.mark_default(user_address, address_type: :billing)
    end
    remove_from_address_book(address_attributes[:id])
  end

  user_addresses.mark_default(user_address, 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



66
67
68
69
70
71
# File 'app/models/concerns/spree/user_address_book.rb', line 66

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

#ship_address_attributes=(attributes) ⇒ Object



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

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