Sha256: 6f265c10b8f3d61cd11d275f9b4d466294cc423018752e55a9940d7721b333d1

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

module Spree
  module Api
    class ProductsController < Spree::Api::BaseController

      def index
        if params[:ids]
          @products = product_scope.where(:id => params[:ids])
        else
          @products = product_scope.ransack(params[:q]).result
        end

        @products = @products.page(params[:page]).per(params[:per_page])
      end

      def show
        @product = find_product(params[:id])
        expires_in 3.minutes
        respond_with(@product)
      end

      def new
      end

      def create
        authorize! :create, Product
        params[:product][:available_on] ||= Time.now
        begin
          @product = Product.new(product_params)
          if @product.save
            respond_with(@product, :status => 201, :default_template => :show)
          else
            invalid_resource!(@product)
          end
        rescue ActiveRecord::RecordNotUnique
          @product.permalink = nil
          retry
        end
      end 

      def update
        @product = find_product(params[:id])
        authorize! :update, @product
        if @product.update_attributes(product_params)
          respond_with(@product, :status => 200, :default_template => :show)
        else
          invalid_resource!(@product)
        end
      end

      def destroy
        @product = find_product(params[:id])
        authorize! :destroy, @product
        @product.update_attribute(:deleted_at, Time.now)
        @product.variants_including_master.update_all(:deleted_at => Time.now)
        respond_with(@product, :status => 204)
      end

      private
        def product_params
          params.require(:product).permit(permitted_product_attributes)
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_api-2.1.2 app/controllers/spree/api/products_controller.rb
spree_api-2.1.1 app/controllers/spree/api/products_controller.rb
spree_api-2.1.0 app/controllers/spree/api/products_controller.rb