Module: ActsAsAddressable

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

Overview

ActsAsAddressable

Defined Under Namespace

Modules: ActiveRecord, ClassMethods

Instance Method Summary collapse

Instance Method Details

#effective_address(category) ⇒ Object



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

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

#effective_addresses(category) ⇒ Object



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

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

#set_effective_address(category, atts) ⇒ Object



84
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
111
# File 'app/models/concerns/acts_as_addressable.rb', line 84

def set_effective_address(category, atts)
  unless (atts.kind_of?(Effective::Address) || atts.kind_of?(Hash) || atts == nil)
    raise ArgumentError.new("Effective::Address #{category}_address= expecting an Effective::Address or Hash of attributes")
  end

  atts = HashWithIndifferentAccess.new(atts.kind_of?(Effective::Address) ? atts.attributes : atts)

  return if atts[:address1].blank?

  add = Effective::Address.new(
    :category     => category.to_s,
    :full_name    => atts[:full_name],
    :address1     => atts[:address1],
    :address2     => atts[:address2],
    :city         => atts[:city],
    :state_code   => atts[:state_code],
    :country_code => atts[:country_code],
    :postal_code  => atts[:postal_code],
    :shipping_address_same_as_billing => atts[:shipping_address_same_as_billing]
  )

  if category == 'shipping' && add.shipping_address_same_as_billing?
    @_effective_addresses_shipping_address_same_as_billing = true
    return
  end

  self.addresses.build(add.attributes) unless (add == effective_address(category))
end

#set_singular_effective_address(category, atts) ⇒ Object

Raises:

  • (ArgumentError)


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

def set_singular_effective_address(category, atts)
  raise ArgumentError.new("Effective::Address #{category}_address= expecting an Effective::Address or Hash of attributes") unless (atts.kind_of?(Effective::Address) || atts.kind_of?(Hash) || atts == nil)

  atts = HashWithIndifferentAccess.new(atts.kind_of?(Effective::Address) ? atts.attributes : atts)

  return if atts[:address1].blank?

  add = (effective_address(category) || self.addresses.build()).tap do |existing|
    existing.category     = category.to_s
    existing.full_name    = atts[:full_name]
    existing.address1     = atts[:address1]
    existing.address2     = atts[:address2]
    existing.city         = atts[:city]
    existing.state_code   = atts[:state_code]
    existing.country_code = atts[:country_code]
    existing.postal_code  = atts[:postal_code]
    existing.shipping_address_same_as_billing = atts[:shipping_address_same_as_billing]
  end

  if category == 'shipping' && add.shipping_address_same_as_billing?
    @_effective_addresses_shipping_address_same_as_billing = true
  end

end