Module: ActsAsAddressable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/acts_as_addressable.rb

Overview

ActsAsAddressable

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#effective_address(category) ⇒ Object


81
82
83
# File 'app/models/concerns/acts_as_addressable.rb', line 81

def effective_address(category)
  effective_addresses(category).last
end

#effective_addresses(category) ⇒ Object


76
77
78
79
# File 'app/models/concerns/acts_as_addressable.rb', line 76

def effective_addresses(category)
  category = category.to_s
  addresses.select { |address| address.category == category }
end

#set_effective_address(category, obj) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/concerns/acts_as_addressable.rb', line 85

def set_effective_address(category, obj)
  raise "#{category}_address= expected an Effective::Address or Hash" unless obj.kind_of?(Effective::Address) || obj.kind_of?(Hash)

  address = (obj.kind_of?(Effective::Address) ? obj : Effective::Address.new(obj))

  if category == 'shipping' && address.shipping_address_same_as_billing? && respond_to?(:billing_address)
    address = effective_address('billing')
  end

  # Prevents duplicates from being created
  return effective_address(category) if address == effective_address(category)

  (self.addresses.build).tap do |existing|
    existing.addressable  = self
    existing.category     = category.to_s
    existing.full_name    = address.full_name
    existing.address1     = address.address1
    existing.address2     = address.address2
    existing.address3     = address.address3 if address.respond_to?(:address3)
    existing.city         = address.city
    existing.state_code   = address.state_code
    existing.country_code = address.country_code
    existing.postal_code  = address.postal_code
    existing.shipping_address_same_as_billing = address.shipping_address_same_as_billing?
  end
end

#set_singular_effective_address(category, obj) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/concerns/acts_as_addressable.rb', line 112

def set_singular_effective_address(category, obj)
  raise "#{category}_address= expected an Effective::Address or Hash" unless obj.kind_of?(Effective::Address) || obj.kind_of?(Hash)

  address = (obj.kind_of?(Effective::Address) ? obj : Effective::Address.new(obj))

  if category == 'shipping' && address.shipping_address_same_as_billing? && respond_to?(:billing_address)
    address = effective_address('billing')
  end

  # This wouldn't create duplicates anyway
  return effective_address(category) if address == effective_address(category)

  (effective_address(category) || self.addresses.build).tap do |existing|
    existing.addressable  = self
    existing.category     = category.to_s
    existing.full_name    = address.full_name
    existing.address1     = address.address1
    existing.address2     = address.address2
    existing.address3     = address.address3 if address.respond_to?(:address3)
    existing.city         = address.city
    existing.state_code   = address.state_code
    existing.country_code = address.country_code
    existing.postal_code  = address.postal_code
    existing.shipping_address_same_as_billing = address.shipping_address_same_as_billing?
  end
end