Module: MerchantSidekick::Addressable::InstanceMethods
- Defined in:
- lib/merchant_sidekick/addressable/addressable.rb
Overview
This module contains instance methods
Instance Method Summary collapse
- #find_address(kind, options = {}) ⇒ Object
-
#find_addresses(selector, kind, options = {}) ⇒ Object
addressable.find_addresses(:first, :billing, conditions).
-
#find_default_address(kind = nil) ⇒ Object
returns the default address for either :has_one or :has_many address definitions Usage: find_default_address :billing.
-
#find_or_build_address(*args) ⇒ Object
Find address of kind ‘type’ or instantiate a new address to relationship.
-
#find_or_clone_address(to_type, from_address = nil, options = {}) ⇒ Object
Used for finding the billing address if none is present the billing address is cloned from business address and if that is not found then a new billing address is created Usage: find_or_clone_address :billing, an_address, { :company_name => “Bla Inc.” } or find_or_clone_address :billing, :shipping # finds billing or copies from shipping address.
Instance Method Details
#find_address(kind, options = {}) ⇒ Object
244 245 246 |
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 244 def find_address(kind, ={}) find_addresses(:first, kind, ) end |
#find_addresses(selector, kind, options = {}) ⇒ Object
addressable.find_addresses(:first, :billing, conditions)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 227 def find_addresses(selector, kind, = {}) defaults = {:order => "created_at DESC"} = defaults.merge().symbolize_keys if :has_one == [:association_type] conditions = [:conditions] || '' scoped = Address.scoped scoped = scoped.where("addressable_id = ? AND addressable_type = ? AND type LIKE ?", self.id, self.class.base_class.name, "#{kind.to_s.pluralize.classify}Address") scoped = scoped.where(conditions) unless conditions.blank? .merge!(:conditions => conditions) scoped.send(selector) elsif :has_many == [:association_type] self.send("#{kind}_addresses").find(selector, ) end end |
#find_default_address(kind = nil) ⇒ Object
returns the default address for either :has_one or :has_many address definitions Usage:
find_default_address :billing
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 253 def find_default_address(kind=nil) kind ||= auto_kind kind = kind.to_sym if :has_one == [:association_type] if [:attributes].empty? self.address else self.send("#{kind}_address") end elsif :has_many == [:association_type] if [:attributes].empty? self.addresses.find(:first, :order => "udpated_at DESC") else self.send("#{kind}_addresses").find(:first, :order => "udpated_at DESC") end end end |
#find_or_build_address(*args) ⇒ Object
Find address of kind ‘type’ or instantiate a new address to relationship. Optionally, submit attributes to instantiate the address with. Examples:
find_or_build_address :business
find_or_build_address :business, :street => "100 Infinity Loop"
find_or_build_address :street => "100 Infinity Loop"
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 277 def find_or_build_address(*args) = {} attributes = [] # if first argument, it determines the type => :kind args.each do |argument| case argument.class.name when /Symbol/, /String/ attributes << argument when /Hash/ .merge!( argument ) end end kind = attributes.first unless address = find_address(kind, :conditions => ) if :has_one == [:association_type] if [:attributes].empty? address = self.build_address() else address = self.send("build_#{kind}_address", ) end else if [:attributes].empty? address = self.addresses.build() else address = self.send("#{kind}_addresses").build() end end end address end |
#find_or_clone_address(to_type, from_address = nil, options = {}) ⇒ Object
Used for finding the billing address if none is present the billing address is cloned from business address and if that is not found then a new billing address is created Usage:
find_or_clone_address :billing, an_address, { :company_name => "Bla Inc." }
or
find_or_clone_address :billing, :shipping # finds billing or copies from shipping address
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 316 def find_or_clone_address(to_type, from_address=nil, ={}) unless to_address = find_default_address(to_type) if from_address.nil? from_address = "#{to_type}_address".camelize.constantize.new() elsif from_address.is_a? Symbol from_address = find_default_address(from_address) elsif from_address.is_a? Hash from_address = "#{to_type}_address".camelize.constantize.new(from_address) end if from_address if :has_one == [:association_type] to_address = self.send("build_#{to_type}_address", from_address.content_attributes) elsif :has_many == [:association_type] to_address = self.send("#{to_type}_addresses").build from_address.content_attributes end end end to_address end |