Module: MerchantSidekick::Addressable::InstanceMethods

Defined in:
lib/merchant_sidekick/addressable/addressable.rb

Overview

This module contains instance methods

Instance Method Summary collapse

Instance Method Details

#find_address(kind, options = {}) ⇒ Object



244
245
246
# File 'lib/merchant_sidekick/addressable/addressable.rb', line 244

def find_address(kind, options={})
  find_addresses(:first, kind, options)
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, options = {})
  defaults = {:order => "created_at DESC"}
  options = defaults.merge(options).symbolize_keys

  if :has_one == acts_as_addressable_options[:association_type]
    conditions = options[: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?
    options.merge!(:conditions => conditions)
    scoped.send(selector)
  elsif :has_many == acts_as_addressable_options[:association_type]
    self.send("#{kind}_addresses").find(selector, options)
  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 == acts_as_addressable_options[:association_type]
    if acts_as_addressable_options[:attributes].empty?
      self.address
    else
      self.send("#{kind}_address")
    end
  elsif :has_many == acts_as_addressable_options[:association_type]
    if acts_as_addressable_options[: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)
  options = {}
  attributes = []
  # if first argument, it determines the type => :kind
  args.each do |argument|
    case argument.class.name
    when /Symbol/, /String/
      attributes << argument
    when /Hash/
      options.merge!( argument )
    end
  end
  kind = attributes.first
  unless address = find_address(kind, :conditions => options)
    if :has_one == acts_as_addressable_options[:association_type]
      if acts_as_addressable_options[:attributes].empty?
        address = self.build_address(options)
      else
        address = self.send("build_#{kind}_address", options)
      end
    else
      if acts_as_addressable_options[:attributes].empty?
        address = self.addresses.build(options)
      else
        address = self.send("#{kind}_addresses").build(options)
      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, options={})
  unless to_address = find_default_address(to_type)
    if from_address.nil?
      from_address = "#{to_type}_address".camelize.constantize.new(options)
    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 == acts_as_addressable_options[:association_type]
        to_address = self.send("build_#{to_type}_address", from_address.content_attributes)
      elsif :has_many == acts_as_addressable_options[:association_type]
        to_address = self.send("#{to_type}_addresses").build from_address.content_attributes
      end
    end
  end
  to_address
end