8234cdcb244d053025bcc790324fc63b1b74352e
[depot.git] / app / controllers / store_controller.rb
1 class StoreController < ApplicationController
2 def index
3 @products = Product.find_products_for_sale
4 @cart = find_cart
5 end
6
7 def add_to_cart
8 product = Product.find(params[:id])
9 @cart = find_cart
10 @current_item = @cart.add_product(product)
11 respond_to do |format|
12 format.js
13 end
14 rescue ActiveRecord::RecordNotFound
15 logger.error("Attempt to access invalid product #{params[:id]}" )
16 redirect_to_index('Invalid product')
17 end
18
19 def empty_cart
20 session[:cart] = nil
21 redirect_to_index('Your cart has been emptied')
22 end
23
24 private
25
26 def find_cart
27 session[:cart] ||= Cart.new
28 end
29
30 def redirect_to_index(msg = nil)
31 flash[:notice] = msg if msg
32 redirect_to :action => 'index'
33 end
34
35 end