cirandas.net

ref: master

plugins/shopping_cart/lib/shopping_cart_plugin/line_item.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ShoppingCartPlugin::LineItem

  attr_accessor :product_id, :quantity

  def initialize(product_id, name)
    @product_id = product_id
    @name = name
    @quantity = 0
  end

  def product
    @product ||= Product.find_by id: product_id
  end

  def name
    product && product.name || @name
  end

  def ==(other)
    self.product == other.product && self.name == other.name && self.quantity == other.quantity
  end

end