Guest User

Untitled

a guest
Apr 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. ## Trying to figure out how to delete a singlar item from the session-based cart
  2. ## Cart.rb
  3. class Cart
  4.  
  5. # Remember this cart has been hacked around and it is actually managing ProductOption, not Product models.
  6. attr_reader :items
  7.  
  8. def initialize
  9. @items = []
  10. end
  11.  
  12. def add_product(product)
  13. current_item = @items.find {|item| item.product == product}
  14. if current_item
  15. current_item.increment_quantity
  16. else
  17. current_item = CartItem.new(product)
  18. @items << current_item
  19. end
  20. current_item
  21. end
  22.  
  23. # Still trying to figure this out =\
  24. def remove_product(product)
  25. current_item = @items.find {|item| item.product == product}
  26. current_item.remove_item
  27. end
  28.  
  29. def total_items
  30. @items.sum { |item| item.quantity }
  31. end
  32.  
  33. def total_price
  34. @items.sum { |item| item.sub_total }
  35. end
  36. end
  37.  
  38. ## CartItem.rb
  39. class CartItem
  40.  
  41. attr_reader :product, :quantity, :piece_name, :item_id, :size, :price, :sub_total, :perma_link
  42.  
  43. def initialize(product)
  44. @product = product
  45. @quantity = 1
  46. end
  47.  
  48. def increment_quantity
  49. @quantity += 1
  50. end
  51.  
  52. def remove_item
  53. session[:cart][:item][:id] = nil
  54. end
  55.  
  56. def piece_name
  57. @product.piece_name
  58. end
  59.  
  60. def item_id
  61. @product.id
  62. end
  63.  
  64. def size
  65. @product.size
  66. end
  67.  
  68. def price
  69. @product.price
  70. end
  71.  
  72. def sub_total
  73. @product.price * @quantity
  74. end
  75.  
  76. def perma_link
  77. @product.perma_link
  78. end
  79.  
  80. end
Add Comment
Please, Sign In to add comment