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

Instance Method Details

#createObject

Takes besides the products attributes either an array of variants or an array of option types.

By submitting an array of variants the option types will be created using the name key in options hash. e.g

product: {
  ...
  variants: {
    price: 19.99,
    sku: "hey_you",
    options: [
      { name: "size", value: "small" },
      { name: "color", value: "black" }
    ]
  }
}

Or just pass in the option types hash:

product: {
  ...
  option_types: ['size', 'color']
}

By passing the shipping category name you can fetch or create that shipping category on the fly. e.g.

product: {
  ...
  shipping_category: "Free Shipping Items"
}


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/spree/api/products_controller.rb', line 73

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

  options = { variants_attrs: variants_params, options_attrs: option_types_params }
  @product = Core::Importer::Product.new(nil, product_params, options).create

  if @product.persisted?
    respond_with(@product, status: 201, default_template: :show)
  else
    invalid_resource!(@product)
  end
end

#destroyObject



102
103
104
105
106
107
# File 'app/controllers/spree/api/products_controller.rb', line 102

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

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/spree/api/products_controller.rb', line 6

def index
  if params[:ids]
    ids = params[:ids].split(",").flatten
    @products = product_scope.where(id: ids)
  else
    products_includes = [
      :variants,
      :option_types,
      :product_properties,
      { classifications: :taxon }
    ]
    @products = product_scope
                  .ransack(params[:q])
                  .result
                  .includes(products_includes)
  end

  @products = paginate(@products.distinct)
  expires_in 15.minutes, public: true
  headers['Surrogate-Control'] = "max-age=#{15.minutes}"
  respond_with(@products)
end

#newObject



29
30
# File 'app/controllers/spree/api/products_controller.rb', line 29

def new
end

#showObject



32
33
34
35
36
37
38
# File 'app/controllers/spree/api/products_controller.rb', line 32

def show
  @product = find_product(params[:id])
  expires_in 15.minutes, public: true
  headers['Surrogate-Control'] = "max-age=#{15.minutes}"
  headers['Surrogate-Key'] = "product_id=1"
  respond_with(@product)
end

#updateObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/spree/api/products_controller.rb', line 88

def update
  @product = find_product(params[:id])
  authorize! :update, @product

  options = { variants_attrs: variants_params, options_attrs: option_types_params }
  @product = Core::Importer::Product.new(@product, product_params, options).update

  if @product.errors.empty?
    respond_with(@product.reload, status: 200, default_template: :show)
  else
    invalid_resource!(@product)
  end
end