Class: CabooseStore::CartController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose_store/cart_controller.rb

Instance Method Summary collapse

Instance Method Details

#addObject

POST /cart/add



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/caboose_store/cart_controller.rb', line 25

def add
  if @order.line_items.exists?(:variant_id => params[:variant_id])
    @line_item = @order.line_items.find_by_variant_id(params[:variant_id])
    @line_item.quantity += params[:quantity] ? params[:quantity].to_i : 1
  else
    @line_item = LineItem.new
    @line_item.variant_id = params[:variant_id]
    @line_item.order_id = @order.id
    @line_item.status = 'pending'
    @line_item.quantity = params[:quantity] ? params[:quantity].to_i : 1
  end
  
  render :json => { :success => @line_item.save, :errors => @line_item.errors.full_messages, :item_count => @order.line_items.count }
end

#get_line_itemObject



5
6
7
# File 'app/controllers/caboose_store/cart_controller.rb', line 5

def get_line_item
  @line_item = @order.line_items.find(params[:id])
end

#indexObject

GET /cart



10
11
12
# File 'app/controllers/caboose_store/cart_controller.rb', line 10

def index
  
end

#item_countObject

GET /cart/item-count



20
21
22
# File 'app/controllers/caboose_store/cart_controller.rb', line 20

def item_count
  render :json => { :item_count => @order.line_items.count }
end

#listObject

GET /cart/items



15
16
17
# File 'app/controllers/caboose_store/cart_controller.rb', line 15

def list
  render :json => { :order => @order }
end

#removeObject

DELETE cart/delete



47
48
49
# File 'app/controllers/caboose_store/cart_controller.rb', line 47

def remove
  render :json => { :success => !!@order.line_items.delete(@line_item), :item_count => @order.line_items.count }
end

#updateObject

PUT cart/update



41
42
43
44
# File 'app/controllers/caboose_store/cart_controller.rb', line 41

def update
  @line_item.quantity = params[:quantity].to_i
  render :json => { :success => @line_item.save, :errors => @line_item.errors.full_messages, :line_item => @line_item, :order_subtotal => @order.calculate_subtotal }
end