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



83
84
85
# File 'app/models/concerns/acts_as_addressable.rb', line 83

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

#effective_addresses(category) ⇒ Object



78
79
80
81
# File 'app/models/concerns/acts_as_addressable.rb', line 78

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

#set_effective_address(category, obj) ⇒ Object



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

def set_effective_address(category, obj)
  return if obj.blank?

  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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/concerns/acts_as_addressable.rb', line 116

def set_singular_effective_address(category, obj)
  return if obj.blank?

  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