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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/spree/api/products_controller.rb', line 26

def create
  authorize! :create, Product
  params[:product][:available_on] ||= Time.now

  variants_attributes = params[:product].delete(:variants_attributes) || []
  option_type_attributes = params[:product].delete(:option_types) || []
  set_up_shipping_category

  @product = Product.new(params[:product])
  begin
    if @product.save
      variants_attributes.each do |variant_attribute|
        variant = @product.variants.new
        variant.update_attributes(variant_attribute)
      end

      option_type_attributes.each do |name|
        option_type = OptionType.where(name: name).first_or_initialize do |option_type|
          option_type.presentation = name
          option_type.save!
        end

        @product.option_types << option_type unless @product.option_types.include?(option_type)
      end

      respond_with(@product, :status => 201, :default_template => :show)
    else
      invalid_resource!(@product)
    end
  rescue ActiveRecord::RecordNotUnique
    @product.permalink = nil
    retry
  end
end

#destroyObject



95
96
97
98
99
100
# File 'app/controllers/spree/api/products_controller.rb', line 95

def destroy
  authorize! :delete, Product
  @product = find_product(params[:id])
  @product.destroy
  respond_with(@product, :status => 204)
end

#indexObject



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

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

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

  respond_with(@products)
end

#newObject



23
24
# File 'app/controllers/spree/api/products_controller.rb', line 23

def new
end

#showObject



18
19
20
21
# File 'app/controllers/spree/api/products_controller.rb', line 18

def show
  @product = find_product(params[:id])
  respond_with(@product)
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/spree/api/products_controller.rb', line 61

def update
  authorize! :update, Product

  variants_attributes = params[:product].delete(:variants_attributes) || []
  option_type_attributes = params[:product].delete(:option_types) || []
  set_up_shipping_category

  @product = find_product(params[:id])
  if @product.update_attributes(params[:product])
    variants_attributes.each do |variant_attribute|
      # update the variant if the id is present in the payload
      if variant_attribute['id'].present?
        @product.variants.find(variant_attribute['id'].to_i).update_attributes(variant_attribute)
      else
        variant = @product.variants.new
        variant.update_attributes(variant_attribute)
      end
    end

    option_type_attributes.each do |name|
      option_type = OptionType.where(name: name).first_or_initialize do |option_type|
        option_type.presentation = name
        option_type.save!
      end

      @product.option_types << option_type unless @product.option_types.include?(option_type)
    end

    respond_with(@product, :status => 200, :default_template => :show)
  else
    invalid_resource!(@product)
  end
end