PageRenderTime 1361ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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