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



59
60
61
62
63
64
65
# File 'app/models/concerns/spree/user_address_book.rb', line 59

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



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

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

#mark_default_bill_address(address) ⇒ Object



146
147
148
# File 'app/models/concerns/spree/user_address_book.rb', line 146

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



142
143
144
# File 'app/models/concerns/spree/user_address_book.rb', line 142

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



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

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



150
151
152
153
154
155
156
157
158
# File 'app/models/concerns/spree/user_address_book.rb', line 150

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



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
131
132
133
134
135
136
137
138
139
140
# File 'app/models/concerns/spree/user_address_book.rb', line 101

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: 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



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

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

#ship_address_attributes=(attributes) ⇒ Object



55
56
57
# File 'app/models/concerns/spree/user_address_book.rb', line 55

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