Class: Caboose::CartController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#addObject

POST /cart



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/caboose/cart_controller.rb', line 19

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

#indexObject

GET /cart



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

def index
end

#item_countObject

GET /cart/item-count



14
15
16
# File 'app/controllers/caboose/cart_controller.rb', line 14

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

#listObject

GET /cart/items



9
10
11
# File 'app/controllers/caboose/cart_controller.rb', line 9

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

#removeObject

DELETE /cart/:line_item_id



52
53
54
55
# File 'app/controllers/caboose/cart_controller.rb', line 52

def remove
  li = LineItem.find(params[:line_item_id]).destroy
  render :json => { :success => true, :item_count => @order.line_items.count }
end

#updateObject

PUT /cart/:line_item_id



42
43
44
45
46
47
48
49
# File 'app/controllers/caboose/cart_controller.rb', line 42

def update
  li = LineItem.find(params[:line_item_id])
  li.quantity = params[:quantity].to_i
  li.save
  li.destroy if li.quantity == 0
  @order.calculate_subtotal
  render :json => { :success => true }
end