Module: MerchantSidekick::Buyer::InstanceMethods

Defined in:
lib/merchant_sidekick/buyer.rb

Instance Method Summary collapse

Instance Method Details

#purchase(*arguments) ⇒ Object

purchase creates a purchase order based on the given sellables, e.g. product, or basically anything that has a price attribute.

E.g.

buyer.purchase(product, :seller => seller)

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/merchant_sidekick/buyer.rb', line 53

def purchase(*arguments)
  sellables = []
  options = default_purchase_options

  # distinguish between options and attributes
  arguments = arguments.flatten
  arguments.each do |argument|
    case argument.class.name
    when 'Hash'
      options.merge! argument
    else
      sellables << (argument.is_a?(MerchantSidekick::ShoppingCart::Cart) ? argument.line_items : argument)
    end
  end
  sellables.flatten!
  sellables.reject! {|s| s.blank?}

  raise ArgumentError.new("No sellable (e.g. product) model provided") if sellables.empty?
  raise ArgumentError.new("Sellable models must have a :price") unless sellables.all? {|sellable| sellable.respond_to? :price}

  self.purchase_orders.build do |po|
    po.buyer = self
    po.seller = options[:from]
    po.build_addresses
    sellables.each do |sellable|
      if sellable && sellable.respond_to?(:before_add_to_order)
        sellable.send(:before_add_to_order, self)
        sellable.reload unless sellable.new_record?
      end
      li = LineItem.new(:sellable => sellable, :order => po)
      po.line_items.push(li)
      sellable.send(:after_add_to_order, self) if sellable && sellable.respond_to?(:after_add_to_order)
    end
    self
  end
end

#purchase_from(seller, *arguments) ⇒ Object

like purchase but forces the seller parameter, instead of taking it as a :seller option



41
42
43
# File 'lib/merchant_sidekick/buyer.rb', line 41

def purchase_from(seller, *arguments)
  purchase(arguments, :from => seller)
end