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, #permitted_line_item_attributes, #set_jsonp_format

Methods included from ControllerSetup

included

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"
}


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/spree/api/products_controller.rb', line 54

def create
  authorize! :create, Product
  params[:product][:available_on] ||= Time.now
  set_up_shipping_category
  
  begin
    @product = Product.new(product_params)
    if @product.save
      variants_params.each do |variant_attribute|
        # make sure the product is assigned before the options=
        @product.variants.create({ product: @product }.merge(variant_attribute))
      end

      option_types_params.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



97
98
99
100
101
102
103
# File 'app/controllers/spree/api/products_controller.rb', line 97

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].split(","))
  else
    @products = product_scope.ransack(params[:q]).result
  end

  @products = @products.page(params[:page]).per(params[:per_page])
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



86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/spree/api/products_controller.rb', line 86

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