/api/app/controllers/spree/api/products_controller.rb

https://github.com/tomz/spree · Ruby · 126 lines · 78 code · 15 blank · 33 comment · 7 complexity · 2f0c7ec1b8ec14e81c5a2e5edaef75db MD5 · raw file

  1. module Spree
  2. module Api
  3. class ProductsController < Spree::Api::BaseController
  4. def index
  5. if params[:ids]
  6. @products = product_scope.where(:id => params[:ids].split(","))
  7. else
  8. @products = product_scope.ransack(params[:q]).result
  9. end
  10. @products = @products.distinct.page(params[:page]).per(params[:per_page])
  11. expires_in 15.minutes, :public => true
  12. headers['Surrogate-Control'] = "max-age=#{15.minutes}"
  13. end
  14. def show
  15. @product = find_product(params[:id])
  16. expires_in 15.minutes, :public => true
  17. headers['Surrogate-Control'] = "max-age=#{15.minutes}"
  18. headers['Surrogate-Key'] = "product_id=1"
  19. end
  20. # Takes besides the products attributes either an array of variants or
  21. # an array of option types.
  22. #
  23. # By submitting an array of variants the option types will be created
  24. # using the *name* key in options hash. e.g
  25. #
  26. # product: {
  27. # ...
  28. # variants: {
  29. # price: 19.99,
  30. # sku: "hey_you",
  31. # options: [
  32. # { name: "size", value: "small" },
  33. # { name: "color", value: "black" }
  34. # ]
  35. # }
  36. # }
  37. #
  38. # Or just pass in the option types hash:
  39. #
  40. # product: {
  41. # ...
  42. # option_types: ['size', 'color']
  43. # }
  44. #
  45. # By passing the shipping category name you can fetch or create that
  46. # shipping category on the fly. e.g.
  47. #
  48. # product: {
  49. # ...
  50. # shipping_category: "Free Shipping Items"
  51. # }
  52. #
  53. def create
  54. authorize! :create, Product
  55. params[:product][:available_on] ||= Time.now
  56. set_up_shipping_category
  57. options = { variants_attrs: variants_params, options_attrs: option_types_params }
  58. @product = Core::Importer::Product.new(nil, product_params, options).create
  59. if @product.persisted?
  60. respond_with(@product, :status => 201, :default_template => :show)
  61. else
  62. invalid_resource!(@product)
  63. end
  64. end
  65. def update
  66. @product = find_product(params[:id])
  67. authorize! :update, @product
  68. options = { variants_attrs: variants_params, options_attrs: option_types_params }
  69. @product = Core::Importer::Product.new(@product, product_params, options).update
  70. if @product.errors.empty?
  71. respond_with(@product.reload, :status => 200, :default_template => :show)
  72. else
  73. invalid_resource!(@product)
  74. end
  75. end
  76. def destroy
  77. @product = find_product(params[:id])
  78. authorize! :destroy, @product
  79. @product.destroy
  80. respond_with(@product, :status => 204)
  81. end
  82. private
  83. def product_params
  84. product_params = params.require(:product).permit(permitted_product_attributes)
  85. if product_params[:taxon_ids].present?
  86. product_params[:taxon_ids] = product_params[:taxon_ids].split(',')
  87. end
  88. product_params
  89. end
  90. def variants_params
  91. variants_key = if params[:product].has_key? :variants
  92. :variants
  93. else
  94. :variants_attributes
  95. end
  96. params.require(:product).permit(
  97. variants_key => [permitted_variant_attributes, :id],
  98. ).delete(variants_key) || []
  99. end
  100. def option_types_params
  101. params[:product].fetch(:option_types, [])
  102. end
  103. def set_up_shipping_category
  104. if shipping_category = params[:product].delete(:shipping_category)
  105. id = ShippingCategory.find_or_create_by(name: shipping_category).id
  106. params[:product][:shipping_category_id] = id
  107. end
  108. end
  109. end
  110. end
  111. end