Class: Spree::Api::ProductsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/api/products_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Methods inherited from BaseController

#map_nested_attributes_keys, #set_jsonp_format

Methods included from ControllerSetup

included

Instance Method Details

#createObject



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

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

#destroyObject



50
51
52
53
54
55
56
# File 'app/controllers/spree/api/products_controller.rb', line 50

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

#indexObject



5
6
7
8
9
10
11
12
13
# File 'app/controllers/spree/api/products_controller.rb', line 5

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

#newObject



21
22
# File 'app/controllers/spree/api/products_controller.rb', line 21

def new
end

#showObject



15
16
17
18
19
# File 'app/controllers/spree/api/products_controller.rb', line 15

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

#updateObject



40
41
42
43
44
45
46
47
48
# File 'app/controllers/spree/api/products_controller.rb', line 40

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