Module: MerchantSidekick::Seller::InstanceMethods

Defined in:
lib/merchant_sidekick/seller.rb

Instance Method Summary collapse

Instance Method Details

#sell(*arguments) ⇒ Object

Sell sellables (line_items) and add them to a sales order The seller will be this person.

e.g.

seller.sell(@product, :buyer => @buyer)

Raises:

  • (ArgumentError)


47
48
49
50
51
52
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
# File 'lib/merchant_sidekick/seller.rb', line 47

def sell(*arguments)
  sellables = []
  options = default_sell_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.sales_orders.build do |so|
    so.buyer = options[:to]
    so.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 => so)
      so.line_items.push(li)
      sellable.send(:after_add_to_order, self) if sellable && sellable.respond_to?(:after_add_to_order)
    end
    self
  end
end

#sell_to(buyer, *arguments) ⇒ Object



36
37
38
# File 'lib/merchant_sidekick/seller.rb', line 36

def sell_to(buyer, *arguments)
  sell(arguments, :to => buyer)
end