Module: CartHelper

Defined in:
lib/generators/templates/cart_helper.rb

Instance Method Summary collapse

Instance Method Details

#cartObject

Returns the cart



31
32
33
# File 'lib/generators/templates/cart_helper.rb', line 31

def cart
  find_cart
end

#empty_cartObject

Cleans up the cart



9
10
11
12
13
# File 'lib/generators/templates/cart_helper.rb', line 9

def empty_cart
  session[:cart] = nil
  flash[:notice] = "Your cart is empty"
  redirect_to :back
end

#find_cartObject

Find the cart



4
5
6
# File 'lib/generators/templates/cart_helper.rb', line 4

def find_cart
  session[:cart] ||= Cart.new
end

Link to add to cart



21
22
23
# File 'lib/generators/templates/cart_helper.rb', line 21

def link_to_add_to_cart(product)
  link_to "Add to cart", add_to_cart_path(product.id)
end

Link to empty the cart



16
17
18
# File 'lib/generators/templates/cart_helper.rb', line 16

def link_to_empty_cart
  link_to "Empty the car", empty_cart_path
end

Link to remove from cart



26
27
28
# File 'lib/generators/templates/cart_helper.rb', line 26

def link_to_remove_from_cart(product)
  link_to "Remove from cart", remove_from_cart_path(product.id) if find_cart.find_item(product)
end