Module: ActiveCart::CartStorage
- Defined in:
- lib/active_cart/cart_storage.rb
Instance Method Summary collapse
- #add_to_cart(item, quantity = 1) ⇒ Object
- #invoice_id ⇒ Object
- #quantity ⇒ Object
- #remove_from_cart(item, quantity = 1) ⇒ Object
- #sub_total ⇒ Object
Instance Method Details
#add_to_cart(item, quantity = 1) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/active_cart/cart_storage.rb', line 15 def add_to_cart(item, quantity = 1) if self.include?(item) index = self.index(item) self.at(index).quantity += quantity else item.quantity += quantity self << item end end |
#invoice_id ⇒ Object
3 4 5 |
# File 'lib/active_cart/cart_storage.rb', line 3 def invoice_id raise NotImplementedError end |
#quantity ⇒ Object
11 12 13 |
# File 'lib/active_cart/cart_storage.rb', line 11 def quantity inject(0) { |t, item| t + item.quantity } end |
#remove_from_cart(item, quantity = 1) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_cart/cart_storage.rb', line 25 def remove_from_cart(item, quantity = 1) if self.include?(item) index = self.index(item) if (existing = self.at(index)).quantity - quantity > 0 existing.quantity = existing.quantity - quantity else self.delete_at(index) end end end |
#sub_total ⇒ Object
7 8 9 |
# File 'lib/active_cart/cart_storage.rb', line 7 def sub_total inject(0) { |t, item| t + (item.quantity * item.price) } end |