class CartItem < ActiveRecord::Base belongs_to :cart belongs_to :product delegate :name, :to => :product def increment_quantity(increment) self.quantity += increment end def quantity=(quantity) if quantity < 1 self.remove_from_cart! else write_attribute(:quantity, quantity) end end def shipping_weight self.product.shipping_weight * self.quantity end def remove_from_cart! self.destroy() end # Price per unit def unit_price ; self.product.price ; end def unit_pricef ; Cart.format_price(self.unit_price()) ; end # Total price def price ; self.product.price * self.quantity ; end def pricef ; Cart.format_price(self.price()) ; end # needed for rails magic form_for stuff, so DO NOT change this to some debug/display value def to_s self.product_id end end