Module: Stall::Addressable

Extended by:
ActiveSupport::Concern
Defined in:
lib/stall/addressable.rb

Instance Method Summary collapse

Instance Method Details

#billing_address(force_actual_address: false) ⇒ Object

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



25
26
27
28
29
30
31
# 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?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/stall/addressable.rb', line 33

def billing_address?
  billing_address.try(:persisted?) && billing_address.billing?
end

#billing_address_attributes=(attributes) ⇒ Object



37
38
39
40
41
# 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) ⇒ Object

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



44
45
46
47
48
49
50
# 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?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/stall/addressable.rb', line 52

def shipping_address?
  shipping_address.try(:persisted?) && shipping_address.shipping?
end

#shipping_address_attributes=(attributes) ⇒ Object



56
57
58
59
60
# 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_associationsObject

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.



69
70
71
72
73
# File 'lib/stall/addressable.rb', line 69

def with_actual_address_associations
  @_force_actual_address_association = true
  yield
  @_force_actual_address_association = false
end