Module: Spree::UserAddressBook
- Extended by:
- ActiveSupport::Concern
- Included in:
- UserMethods
- Defined in:
- app/models/concerns/spree/user_address_book.rb
Instance Method Summary collapse
- #bill_address=(address) ⇒ Object
- #bill_address_attributes=(attributes) ⇒ Object
- #default_address=(address) ⇒ Object
- #default_address_attributes=(attributes) ⇒ Object (also: #ship_address_attributes=)
- #mark_default_address(address) ⇒ Object
-
#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.
- #remove_from_address_book(address_id) ⇒ Object
-
#save_in_address_book(address_attributes, default = false) ⇒ 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.
-
#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.
Instance Method Details
#bill_address=(address) ⇒ Object
35 36 37 38 39 |
# File 'app/models/concerns/spree/user_address_book.rb', line 35 def bill_address=(address) # stow a copy in our address book too address = save_in_address_book(address.attributes) if address super(address) end |
#bill_address_attributes=(attributes) ⇒ Object
41 42 43 |
# File 'app/models/concerns/spree/user_address_book.rb', line 41 def bill_address_attributes=(attributes) self.bill_address = Spree::Address.immutable_merge(bill_address, attributes) end |
#default_address=(address) ⇒ Object
45 46 47 |
# File 'app/models/concerns/spree/user_address_book.rb', line 45 def default_address=(address) save_in_address_book(address.attributes, true) if address end |
#default_address_attributes=(attributes) ⇒ Object Also known as: ship_address_attributes=
49 50 51 52 53 54 |
# File 'app/models/concerns/spree/user_address_book.rb', line 49 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 self.default_address = Spree::Address.immutable_merge(default_address, attributes) end |
#mark_default_address(address) ⇒ Object
126 127 128 |
# File 'app/models/concerns/spree/user_address_book.rb', line 126 def mark_default_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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/concerns/spree/user_address_book.rb', line 70 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 && address.persisted? end if order.bill_address address = save_in_address_book( order.bill_address.attributes, order.ship_address.nil? && Spree::Config.automatic_default_address ) self.bill_address_id = address.id if address && address.persisted? end save! # In case the ship_address_id or bill_address_id was set end |
#remove_from_address_book(address_id) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'app/models/concerns/spree/user_address_book.rb', line 130 def remove_from_address_book(address_id) user_address = user_addresses.find_by(address_id: address_id) if user_address user_address.update(archived: true, default: false) else false end end |
#save_in_address_book(address_attributes, default = false) ⇒ 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
95 96 97 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 |
# File 'app/models/concerns/spree/user_address_book.rb', line 95 def save_in_address_book(address_attributes, default = false) return nil unless address_attributes.present? 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) 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_address).reset association(:default_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
61 62 63 64 |
# File 'app/models/concerns/spree/user_address_book.rb', line 61 def ship_address=(address) be_default = Spree::Config.automatic_default_address save_in_address_book(address.attributes, be_default) if address end |