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


56
57
58
59
60
61
62
# File 'app/models/concerns/spree/user_address_book.rb', line 56

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


64
65
66
# File 'app/models/concerns/spree/user_address_book.rb', line 64

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

#mark_default_bill_address(address) ⇒ Object


136
137
138
# File 'app/models/concerns/spree/user_address_book.rb', line 136

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


132
133
134
# File 'app/models/concerns/spree/user_address_book.rb', line 132

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


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/concerns/spree/user_address_book.rb', line 72

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


140
141
142
143
144
145
146
147
148
# File 'app/models/concerns/spree/user_address_book.rb', line 140

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


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/concerns/spree/user_address_book.rb', line 98

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


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

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

#ship_address_attributes=(attributes) ⇒ Object


52
53
54
# File 'app/models/concerns/spree/user_address_book.rb', line 52

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