Class: Spree::Api::V1::ProductsController
- Inherits:
-
BaseController
- Object
- ActionController::Base
- BaseController
- Spree::Api::V1::ProductsController
- Defined in:
- app/controllers/spree/api/v1/products_controller.rb
Instance Attribute Summary
Attributes inherited from BaseController
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
-
#new ⇒ Object
Takes besides the products attributes either an array of variants or an array of option types.
- #show ⇒ Object
- #update ⇒ Object
Methods inherited from BaseController
#content_type, #permitted_line_item_attributes
Methods included from ControllerSetup
Instance Method Details
#create ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/controllers/spree/api/v1/products_controller.rb', line 62 def create :create, Product params[:product][:available_on] ||= Time.current set_up_shipping_category = { variants_attrs: variants_params, options_attrs: option_types_params } @product = Core::Importer::Product.new(nil, product_params, ).create if @product.persisted? respond_with(@product, status: 201, default_template: :show) else invalid_resource!(@product) end end |
#destroy ⇒ Object
90 91 92 93 94 |
# File 'app/controllers/spree/api/v1/products_controller.rb', line 90 def destroy :destroy, @product @product.destroy respond_with(@product, status: 204) end |
#index ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'app/controllers/spree/api/v1/products_controller.rb', line 7 def index @products = if params[:ids] product_scope.where(id: params[:ids].split(',').flatten) else product_scope.ransack(params[:q]).result end @products = @products.distinct.page(params[:page]).per(params[:per_page]) expires_in 15.minutes, public: true headers['Surrogate-Control'] = "max-age=#{15.minutes}" respond_with(@products) end |
#new ⇒ Object
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"
}
60 |
# File 'app/controllers/spree/api/v1/products_controller.rb', line 60 def new; end |
#show ⇒ Object
20 21 22 23 24 25 |
# File 'app/controllers/spree/api/v1/products_controller.rb', line 20 def show expires_in 15.minutes, public: true headers['Surrogate-Control'] = "max-age=#{15.minutes}" headers['Surrogate-Key'] = 'product_id=1' respond_with(@product) end |
#update ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/spree/api/v1/products_controller.rb', line 77 def update :update, @product = { variants_attrs: variants_params, options_attrs: option_types_params } @product = Core::Importer::Product.new(@product, product_params, ).update if @product.errors.empty? respond_with(@product.reload, status: 200, default_template: :show) else invalid_resource!(@product) end end |