Sha256: 5e2b4d5e91ce1119694e68cbed96b035ffef20a68e88a85c77161a3230bd556f

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

module CabooseStore
  class CartController < ApplicationController  
    
    # GET /cart
    def index    
      @order = self.get_cart
      render :layout => 'layouts/caboose/modal'    
    end
    
    # GET /cart/add/:id
    def add
      v = Variant.find(params[:id])    
      @order = self.get_cart
      
      exists = false
      @order.line_items.each do |li|
        next if li.variant.id != v.id
        li.quantity = li.quantity + 1
        li.save
        exists = true
        break
      end    
      if !exists
        li = OrderLineItem.new
        li.variant = v
        li.quantity = 1
        li.unit_price = v.price
        li.variant_sku = v.sku
        @order.line_items << li      
      end
      @order.save    
      
      render 'caboose_store/cart/index', :layout => 'layouts/caboose/modal'
    end
    
    # PUT /cart/:id
    def update    
      resp = Caboose::StdClass.new
      
      order_id = session['cart_id']
      variant_id = params[:id].to_i
      qty = params[:quantity_in_stock].to_i
      
      if (qty == 0)
        OrderLineItem.where(:order_id => order_id, :variant_id => variant_id).delete_all
      else    
        li = OrderLineItem.where(:order_id => order_id, :variant_id => variant_id).first
        li.quantity = qty
        li.save
      end
      render :json => resp
    end
    
    def get_cart
      # Assumes the init_cart method is running in the parent application controller
      return Order.find(session['cart_id'])
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
caboose-store-0.0.10 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.9 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.8 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.7 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.6 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.5 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.4 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.3 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.2 app/controllers/caboose_store/cart_controller.rb