module Stall::Addressable

Public Instance Methods

billing_address(force_actual_address: false) click to toggle source

Allow billing address to fall back to shipping address when not filled

# File lib/stall/addressable.rb, line 25
def billing_address(force_actual_address: false)
  if (billing_address = association(:billing_address).load_target)
    billing_address
  elsif !@_force_actual_address_association && !force_actual_address
    association(:shipping_address).load_target
  end
end
billing_address?() click to toggle source
# File lib/stall/addressable.rb, line 33
def billing_address?
  billing_address.try(:persisted?) && billing_address.billing?
end
billing_address_attributes=(attributes) click to toggle source
# File lib/stall/addressable.rb, line 37
def billing_address_attributes=(attributes)
  with_actual_address_associations do
    assign_nested_attributes_for_one_to_one_association(:billing_address, attributes)
  end
end
shipping_address(force_actual_address: false) click to toggle source

Allow shipping address to fall back to billing address when not filled

# File lib/stall/addressable.rb, line 44
def shipping_address(force_actual_address: false)
  if (shipping_address = association(:shipping_address).load_target)
    shipping_address
  elsif !@_force_actual_address_association && !force_actual_address
    association(:billing_address).load_target
  end
end
shipping_address?() click to toggle source
# File lib/stall/addressable.rb, line 52
def shipping_address?
  shipping_address.try(:persisted?) && shipping_address.shipping?
end
shipping_address_attributes=(attributes) click to toggle source
# File lib/stall/addressable.rb, line 56
def shipping_address_attributes=(attributes)
  with_actual_address_associations do
    assign_nested_attributes_for_one_to_one_association(:shipping_address, attributes)
  end
end
with_actual_address_associations() { || ... } click to toggle source

Allow forcing actual address associations to be retrieved, thus disabling addresses fallback for the duration of the block.

This is used for nested attributes assignation which calls the associations reader directly, which happen to merge addresses when both billing and shipping addresses are assigned at the same time.

# File lib/stall/addressable.rb, line 69
def with_actual_address_associations
  @_force_actual_address_association = true
  yield
  @_force_actual_address_association = false
end