bit dojo labs : file explorer

Inspecting: home > misc > sample > sample_rails.rb (download)
#
# Ruby on Rails
# Sample code of a Controller
#
# From the Meta Cart project
# http://bitdojo.net/meta_cart
#
# Original filename: store_controller.rb
#

class StoreController < ApplicationController
  before_filter :find_cart, :except => :empty_cart
  
  def index
    @products = Product.find_products_for_sale
    #@cart = find_cart
    @current_time = Time.now.strftime("%Y/%m/%d %H:%M")
    @visits = visit_counter
    session[:at_page] = "index"
    
    render :layout => false if request.xhr?
  end

  def how_to_pay
    session[:at_page] = "how_to_pay"
    render :layout => false if request.xhr?
  end

  def contact_us
    session[:at_page] = "contact_us"
    render :layout => false if request.xhr?
  end

  def visit_counter
    session[:counter] ||= 0
    session[:counter] += 1
  end
  
  def add_to_cart
  
    # reset the visit count when an item is added
    session[:counter] = nil

    product = find_product_by_id(params[:id])
    #@cart = find_cart
    @current_item = @cart.add_product(product)
    redirect_to_index("Item added to cart") unless request.xhr?
    
  end
  
  def remove_from_cart
  
    product = find_product_by_id(params[:id])
    #@cart = find_cart
    @current_item = @cart.remove_product(product)
           
    #if !request.xhr?
    #  redirect_to_index("Removed one item from cart")
    #elsif (session[:at_page] == "checkout") && (@cart.total_items == 0)
    #  redirect_to_index
    #end
    
    redirect_to_index("Removed one item from cart") unless request.xhr?
    
  end
  
  def find_product_by_id(id)
  
    begin
      product = Product.find(id)
    rescue  ActiveRecord::RecordNotFound
      logger.error("Attempt to find invalid product #{params[:id]}")
      redirect_to_index("Invalid product") 
    end
    
    return product
    
  end
  
  def empty_cart
    session[:cart] = nil
    redirect_to_index("Your cart is now empty") unless request.xhr? 
  end
  
  def checkout
    session[:at_page] = "checkout"
    #@cart = find_cart
    if @cart.items.empty?
      redirect_to_index("Your cart is empty")
    else
      @order = Order.new
    end
  end  

  def save_order
    #@cart = find_cart
    @order = Order.new(params[:order])
    @order.status = Order::ORDER_STATUSES[0][1]
    @order.add_line_items_from_cart(@cart)
    if @order.save                       
      session[:cart] = nil
      #redirect_to_index("Thank you for your order")
      StoreMailer.deliver_confirm(@order)
      flash[:checkedout] = true
      redirect_to :action => :thank_you
    else
      render :action => :checkout
    end
  end  

  def thank_you
    if flash[:checkedout].nil?
      redirect_to_index
    else
      flash[:checkedout] = nil
    end
  end
  
  def redirect_to_index(msg = nil)
    flash[:notice] = msg if msg
    redirect_to :action => :index    
  end
  
  # private methods below
  private
  
  def find_cart
    @cart = (session[:cart] ||= Cart.new)
  end
  
end