Module: Spree::Purchase::Addresses
- Extended by:
- ActiveSupport::Concern
- Included in:
- Cart, Order, OrderGroup
- Defined in:
- app/models/concerns/spree/purchase/addresses.rb
Overview
The full address surface shared by Spree::Cart and Spree::Order: the bill/ship associations and their public aliases, default filling from the customer's saved addresses, address-row deduplication on write, ownership-guarded address_id assignment and deletion, promotion of checkout addresses to the customer's defaults, and the ship→bill cloning predicate. The shipping address is canonical: use_shipping copies it onto the billing address; the reverse use_billing direction is a deprecated 6.3-removal bridge. Clone callbacks are wired per model.
Instance Method Summary collapse
-
#assign_default_addresses! ⇒ void
Fills any blank bill/ship address from the customer's valid saved defaults.
-
#bill_address_attributes=(attributes) ⇒ Object
Deduplicating writer: reuses or updates an existing address row when possible, then promotes the result to the customer's default bill address.
-
#bill_address_id=(id) ⇒ Object
Ownership-guarded writer: the id is applied only when the address is one this purchase may ship to — the buyer's own book, or the book of the company it is for.
-
#can_propose_deliveries? ⇒ Boolean
Whether delivery can be proposed yet: once there is a destination (a shipping address, or a pickup intent for a purchase that never gets one), or straight away when no item ships to an address.
-
#clone_billing_address ⇒ true
deprecated
Deprecated.
The shipping address is canonical (see #use_billing=); removed in 6.3 together with the use_billing bridge.
-
#clone_shipping_address ⇒ true
Copies the ship address onto the bill address and promotes it to the customer's default bill address.
-
#owns_address?(address) ⇒ Boolean
Whether an attached address row is this purchase's own to delete.
-
#selectable_address(id) ⇒ Spree::Address?
The address behind an id a client may select, or nil when the id names one this purchase has no business shipping to.
-
#selectable_addresses ⇒ ActiveRecord::Relation<Spree::Address>
Everything a checkout address picker may offer for this purchase: the buyer's own book plus the book of the company it names, that node's own entries and the ones it inherits, limited to the countries the purchase's market sells to.
-
#ship_address_attributes=(attributes) ⇒ Object
Deduplicating writer — see #bill_address_attributes=.
-
#ship_address_id=(id) ⇒ Object
Ownership-guarded writer — see #bill_address_id=.
-
#shipping_address_required? ⇒ Boolean
Whether checkout must collect a customer shipping address.
-
#shipping_eq_billing_address? ⇒ Boolean
Whether both addresses are present and match.
-
#use_billing=(value) ⇒ Object
deprecated
Deprecated.
The shipping address is canonical — use
use_shippingto copy it onto the billing address. Removed in 6.3. -
#use_billing? ⇒ Boolean
deprecated
Deprecated.
See #use_billing=; removed in 6.3.
- #use_shipping? ⇒ Boolean
Instance Method Details
#assign_default_addresses! ⇒ void
This method returns an undefined value.
Fills any blank bill/ship address from the customer's valid saved defaults. The ship address is skipped when no physical delivery is required, so shipping-address validations never fire on digital-only purchases.
176 177 178 179 180 181 182 183 184 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 176 def assign_default_addresses! return unless customer self.bill_address = customer.bill_address if !bill_address_id && customer.bill_address&.valid? # Skip the ship address only for all-digital records to avoid # triggering shipping-address validations (an empty record still # gets one — items usually arrive after the address). self.ship_address = customer.ship_address if !ship_address_id && customer.ship_address&.valid? && !digital? end |
#bill_address_attributes=(attributes) ⇒ Object
Deduplicating writer: reuses or updates an existing address row when possible, then promotes the result to the customer's default bill address.
226 227 228 229 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 226 def bill_address_attributes=(attributes) self.bill_address = update_or_create_address(attributes, guest_row: guest_editable_address(bill_address, ship_address_id)) customer.bill_address = bill_address if should_assign_user_default_address?(bill_address) end |
#bill_address_id=(id) ⇒ Object
Ownership-guarded writer: the id is applied only when the address is one this purchase may ship to — the buyer's own book, or the book of the company it is for. Anything else, including any id on a guest record, resolves to nil.
214 215 216 217 218 219 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 214 def bill_address_id=(id) return if bill_address_id == id self['bill_address_id'] = selectable_address(id)&.id reset_bill_address end |
#can_propose_deliveries? ⇒ Boolean
Whether delivery can be proposed yet: once there is a destination (a shipping address, or a pickup intent for a purchase that never gets one), or straight away when no item ships to an address. Quoted without a destination, only pickup qualifies.
83 84 85 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 83 def can_propose_deliveries? ship_address.present? || preferred_stock_location_id.present? || !items_require_shipping_address? end |
#clone_billing_address ⇒ true
The shipping address is canonical (see #use_billing=); removed in 6.3 together with the use_billing bridge.
Copies the bill address onto the ship address and promotes it to the customer's default ship address.
202 203 204 205 206 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 202 def clone_billing_address self.ship_address = bill_address if bill_address customer.ship_address = bill_address if should_assign_user_default_address?(bill_address) true end |
#clone_shipping_address ⇒ true
Copies the ship address onto the bill address and promotes it to the customer's default bill address.
190 191 192 193 194 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 190 def clone_shipping_address self.bill_address = ship_address if ship_address customer.bill_address = ship_address if should_assign_user_default_address?(ship_address) true end |
#owns_address?(address) ⇒ Boolean
Whether an attached address row is this purchase's own to delete.
Only the ownerless rows are: the one a guest typed in, and the snapshot completion copies onto the order. Everything else is an entry in somebody's address book — the buyer's, a company node's, a seller's — which the purchase is merely pointing at. A cart may name a company site every other member ships from, so taking an attached row down with the cart would let one member empty the shared book.
A business snapshot keeps its owner_type to stay addressed to a
company rather than to a person, so ownership is the id, not the type.
166 167 168 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 166 def owns_address?(address) address.present? && address.owner_id.blank? end |
#selectable_address(id) ⇒ Spree::Address?
The address behind an id a client may select, or nil when the id names one this purchase has no business shipping to. Takes a prefixed id or a raw primary key, so the services that resolve a client-supplied id and the writers below share one rule rather than each inventing their own.
Two books qualify. The buyer's own is the ordinary case. The company's is the B2B one: a purchase for a node ships to that node's sites, and to its ancestors' — the same self-and-ancestors chain the node's default address is prefilled from, so a division can pick the headquarters address it already inherits.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 121 def selectable_address(id) address = ::Spree::Address.not_deleted.find_by_param(id) || ::Spree::Address.not_deleted.find_by(id: id) return if address.nil? return address if customer_id.present? && address.customer_owned? && address.owner_id == customer_id company = readable_company return if company.nil? || address.owner_type != 'Spree::Company' company.self_and_ancestors.any? { |node| node.id == address.owner_id } ? address : nil end |
#selectable_addresses ⇒ ActiveRecord::Relation<Spree::Address>
Everything a checkout address picker may offer for this purchase: the buyer's own book plus the book of the company it names, that node's own entries and the ones it inherits, limited to the countries the purchase's market sells to. A subset of what #selectable_address accepts, so a picker built from it never offers an address the checkout cannot deliver to.
141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 141 def selectable_addresses books = [] books << ::Spree::Address.not_deleted.where(owner_type: ::Spree.customer_class.to_s, owner_id: customer_id) if customer_id.present? books << readable_company&.address_book addresses = books.compact.reduce { |left, right| left.or(right) } || ::Spree::Address.none return addresses if market.nil? addresses.where(country_code: market.market_countries.select(:country_code)) end |
#ship_address_attributes=(attributes) ⇒ Object
Deduplicating writer — see #bill_address_attributes=. Quick-checkout (wallet) addresses are never promoted to defaults, and an unpersisted quick-checkout address is discarded.
246 247 248 249 250 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 246 def ship_address_attributes=(attributes) self.ship_address = update_or_create_address(attributes, guest_row: guest_editable_address(ship_address, bill_address_id)) customer.ship_address = ship_address if should_assign_user_default_address?(ship_address) self.ship_address = nil if quick_checkout_address?(attributes[:quick_checkout]) && !ship_address.persisted? end |
#ship_address_id=(id) ⇒ Object
Ownership-guarded writer — see #bill_address_id=.
234 235 236 237 238 239 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 234 def ship_address_id=(id) return if ship_address_id == id self['ship_address_id'] = selectable_address(id)&.id reset_ship_address end |
#shipping_address_required? ⇒ Boolean
Whether checkout must collect a customer shipping address. Selected delivery methods are the source of truth — their fulfillment providers decide (digital and pickup deliveries need none). Before any selection, falls back to the record's merchant-pickup intent and the items' possible fulfillment types, matched against the store's configured address-requiring delivery methods.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 64 def shipping_address_required? candidates = fulfillments.to_a if candidates.many? && candidates.all?(ActiveRecord::Base) ActiveRecord::Associations::Preloader.new(records: candidates, associations: { selected_delivery_rate: :delivery_method }).call end selected_methods = candidates.filter_map(&:delivery_method) return selected_methods.any?(&:requires_address?) if selected_methods.any? return false if preferred_stock_location_id.present? items_require_shipping_address? end |
#shipping_eq_billing_address? ⇒ Boolean
Whether both addresses are present and match. With neither present there is nothing to call the same.
52 53 54 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 52 def shipping_eq_billing_address? ship_address.present? && bill_address == ship_address end |
#use_billing=(value) ⇒ Object
The shipping address is canonical — use use_shipping to
copy it onto the billing address. Removed in 6.3.
89 90 91 92 93 94 95 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 89 def use_billing=(value) Spree::Deprecation.warn( 'use_billing is deprecated and will be removed in Spree 6.3. ' \ 'The shipping address comes first — use use_shipping to copy it onto the billing address.' ) @use_billing = value end |
#use_billing? ⇒ Boolean
See #use_billing=; removed in 6.3.
99 100 101 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 99 def use_billing? use_billing.in?([true, 'true', '1']) end |
#use_shipping? ⇒ Boolean
104 105 106 |
# File 'app/models/concerns/spree/purchase/addresses.rb', line 104 def use_shipping? use_shipping.in?([true, 'true', '1']) end |