PageRenderTime 1595ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/adamlwalker/spree
Ruby | 126 lines | 78 code | 15 blank | 33 comment | 6 complexity | 97d66cfdd48b371cb32c0cdba05f1b4a MD5 | raw file
  1. module Spree
  2. module Api
  3. module V1
  4. class ProductsController < Spree::Api::BaseController
  5. def index
  6. if params[:ids]
  7. @products = product_scope.where(id: params[:ids].split(",").flatten)
  8. else
  9. @products = product_scope.ransack(params[:q]).result
  10. end
  11. @products = @products.distinct.page(params[:page]).per(params[:per_page])
  12. expires_in 15.minutes, :public => true
  13. headers['Surrogate-Control'] = "max-age=#{15.minutes}"
  14. respond_with(@products)
  15. end
  16. def show
  17. @product = find_product(params[:id])
  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 create
  57. authorize! :create, Product
  58. params[:product][:available_on] ||= Time.now
  59. set_up_shipping_category
  60. options = { variants_attrs: variants_params, options_attrs: option_types_params }
  61. @product = Core::Importer::Product.new(nil, product_params, options).create
  62. if @product.persisted?
  63. respond_with(@product, :status => 201, :default_template => :show)
  64. else
  65. invalid_resource!(@product)
  66. end
  67. end
  68. def update
  69. @product = find_product(params[:id])
  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. @product = find_product(params[:id])
  81. authorize! :destroy, @product
  82. @product.destroy
  83. respond_with(@product, :status => 204)
  84. end
  85. private
  86. def product_params
  87. params.require(:product).permit(permitted_product_attributes)
  88. end
  89. def variants_params
  90. variants_key = if params[:product].has_key? :variants
  91. :variants
  92. else
  93. :variants_attributes
  94. end
  95. params.require(:product).permit(
  96. variants_key => [permitted_variant_attributes, :id],
  97. ).delete(variants_key) || []
  98. end
  99. def option_types_params
  100. params[:product].fetch(:option_types, [])
  101. end
  102. def set_up_shipping_category
  103. if shipping_category = params[:product].delete(:shipping_category)
  104. id = ShippingCategory.find_or_create_by(name: shipping_category).id
  105. params[:product][:shipping_category_id] = id
  106. end
  107. end
  108. end
  109. end
  110. end
  111. end