Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Trying to figure out how to delete a singlar item from the session-based cart
- ## Cart.rb
- class Cart
- # Remember this cart has been hacked around and it is actually managing ProductOption, not Product models.
- attr_reader :items
- def initialize
- @items = []
- end
- def add_product(product)
- current_item = @items.find {|item| item.product == product}
- if current_item
- current_item.increment_quantity
- else
- current_item = CartItem.new(product)
- @items << current_item
- end
- current_item
- end
- # Still trying to figure this out =\
- def remove_product(product)
- current_item = @items.find {|item| item.product == product}
- current_item.remove_item
- end
- def total_items
- @items.sum { |item| item.quantity }
- end
- def total_price
- @items.sum { |item| item.sub_total }
- end
- end
- ## CartItem.rb
- class CartItem
- attr_reader :product, :quantity, :piece_name, :item_id, :size, :price, :sub_total, :perma_link
- def initialize(product)
- @product = product
- @quantity = 1
- end
- def increment_quantity
- @quantity += 1
- end
- def remove_item
- session[:cart][:item][:id] = nil
- end
- def piece_name
- @product.piece_name
- end
- def item_id
- @product.id
- end
- def size
- @product.size
- end
- def price
- @product.price
- end
- def sub_total
- @product.price * @quantity
- end
- def perma_link
- @product.perma_link
- end
- end
Add Comment
Please, Sign In to add comment