Finished chapter 11
[depot.git] / app / models / cart.rb
1 class Cart
2 attr_reader :items
3
4 def initialize
5 @items = []
6 end
7
8 def add_product(product)
9 current_item = @items.find {|item| item.product == product}
10 if current_item
11 current_item.increment_quantity
12 else
13 current_item = CartItem.new(product)
14 @items << current_item
15 end
16 current_item
17 end
18
19 def total_price
20 @items.sum {|item| item.price}
21 end
22
23 def total_items
24 @items.sum {|item| item.quantity}
25 end
26
27 end