Class: Caboose::CartController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Caboose::CartController
- Defined in:
- app/controllers/caboose/cart_controller.rb
Instance Method Summary collapse
-
#add ⇒ Object
POST /cart.
-
#index ⇒ Object
GET /cart.
-
#item_count ⇒ Object
GET /cart/item-count.
-
#list ⇒ Object
GET /cart/items.
-
#remove ⇒ Object
DELETE /cart/:line_item_id.
-
#update ⇒ Object
PUT /cart/:line_item_id.
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
#add ⇒ Object
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., :item_count => @order.line_items.count } end |
#index ⇒ Object
GET /cart
5 6 |
# File 'app/controllers/caboose/cart_controller.rb', line 5 def index end |
#item_count ⇒ Object
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 |
#list ⇒ Object
GET /cart/items
9 10 11 |
# File 'app/controllers/caboose/cart_controller.rb', line 9 def list render :json => { :order => @order } end |
#remove ⇒ Object
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 |
#update ⇒ Object
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 |