Sha256: 07f3fc3a02e3dd43631b6716195308d13c3d52c89b41fc2509c90a5227af25dd

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module RightnowOms
  class CartItemsController < ApplicationController
    before_filter :remove_null_params
    before_filter :load_or_create_cart, only: [:index, :create]
    before_filter :load_cart_item, only: [:update, :destroy]

    # TODO Unit test needed
    def index
      render_for_api :default, json: @cart.cart_items, root: :cart_items, status: :ok
    end

    def create
      params[:cart_item][:quantity] = 1 if params[:cart_item][:quantity].blank?
      cart_item = @cart.add_item(find_cartable, params[:cart_item])

      respond_to do |format|
        if cart_item.valid?
          format.json { render_for_api :default, json: cart_item, root: :cart_item, status: :created }
        else
          format.json { render json: cart_item.errors, status: :unprocessable_entity }
        end
      end
    end

    def update
      if params[:cart_item]
        params[:cart_item].delete(:original_price)
        params[:cart_item].delete(:base_quantity)
      end

      respond_to do |format|
        if @cart_item.update_attributes(params[:cart_item])
          format.json { render_for_api :default, json: @cart_item, root: :cart_item, status: :ok }
        else
          format.json { render json: @cart_item.errors, status: :unprocessable_entity }
        end
      end
    end

    def destroy
      @cart_item.destroy

      respond_to do |format|
        format.json { render json: nil, status: :ok }
      end
    end

    private
    def find_cartable
      cartable_id = params[:cart_item][:cartable_id]
      cartable_type = params[:cart_item][:cartable_type]

      cartable_type.constantize.find_by_id(cartable_id)
    end

    def load_cart_item
      @cart_item = RightnowOms::CartItem.find(params[:id])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rightnow_oms-0.1.6 app/controllers/rightnow_oms/cart_items_controller.rb