Class: Spree::Addresses::Create

Inherits:
Object
  • Object
show all
Includes:
Helper, ServiceModule::Base
Defined in:
app/services/spree/addresses/create.rb

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(address_params: {}, owner: nil, user: nil, **opts) ⇒ Object

Parameters:

  • owner (Object, nil) (defaults to: nil) —

    who keeps the address — a customer, a company node, anything with an address book. user: is the older name for the same thing, from when only customers had one.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/spree/addresses/create.rb', line 10

def call(address_params: {}, owner: nil, user: nil, **opts)
  owner ||= user
  order = opts[:order]
  # nil, not false — the same three-state contract Update keeps, so
  # silence means the same thing on both.
  default_billing = address_params.key?(:is_default_billing) ? address_params.delete(:is_default_billing) : opts.fetch(:default_billing, nil)
  default_shipping = address_params.key?(:is_default_shipping) ? address_params.delete(:is_default_shipping) : opts.fetch(:default_shipping, nil)

  address = Spree::Address.new(address_params)
  address.owner = owner if owner.present?

  ApplicationRecord.transaction do
    arguments = { order: order, default_billing: default_billing, default_shipping: default_shipping }
    next write(address, **arguments) if owner.nil?

    # One writer per book at a time. Looking for an entry and then filing
    # one when there is none is a read the write depends on, and no
    # unique index stands behind it, so two requests arriving together
    # would each find nothing and file an entry each — the very duplicate
    # the lookup exists to prevent.
    owner.with_lock { write(address, **arguments) }
  end
end