Module: SpreeCmCommissioner::Order::AddressBookDecorator

Defined in:
app/models/spree_cm_commissioner/order/address_book_decorator.rb

Instance Method Summary collapse

Instance Method Details

#assign_address_attributes(attributes = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/spree_cm_commissioner/order/address_book_decorator.rb', line 26

def assign_address_attributes(attributes = {})
  return if attributes.blank?

  attributes.transform_values! { |v| v == '' ? nil : v }
  attributes = attributes.to_h.symbolize_keys

  existing_address = attributes[:id].present? ? find_existing_address(attributes[:id]) : nil

  if existing_address&.editable?
    existing_address.assign_attributes(attributes)
    existing_address
  else
    ::Spree::Address.new(attributes.except(:id, :updated_at, :created_at))
  end
end

#bill_address_attributes=(attributes) ⇒ Object

override



9
10
11
12
13
14
15
# File 'app/models/spree_cm_commissioner/order/address_book_decorator.rb', line 9

def bill_address_attributes=(attributes)
  attributes[:id] = bill_address_id if attributes[:id].blank?
  attributes[:phone] = intel_phone_number if intel_phone_number.present?

  self.bill_address = assign_address_attributes(attributes)
  user.bill_address = bill_address if user && user.bill_address.nil?
end

#find_existing_address(id) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'app/models/spree_cm_commissioner/order/address_book_decorator.rb', line 42

def find_existing_address(id)
  if user.present?
    ::Spree::Address.where('(user_id = ? OR user_id IS NULL) AND deleted_at IS NULL', user.id)
                    .find_by(id: id)
  else
    ::Spree::Address.where(user_id: nil, deleted_at: nil)
                    .find_by(id: id)
  end
end

#ship_address_attributes=(attributes) ⇒ Object

override



18
19
20
21
22
23
24
# File 'app/models/spree_cm_commissioner/order/address_book_decorator.rb', line 18

def ship_address_attributes=(attributes)
  attributes[:id] = ship_address_id if attributes[:id].blank?
  attributes[:phone] = intel_phone_number if intel_phone_number.present?

  self.ship_address = assign_address_attributes(attributes)
  user.ship_address = ship_address if user && user.ship_address.nil?
end