PageRenderTime 1194ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/pageman/spree
Ruby | 56 lines | 48 code | 8 blank | 0 comment | 3 complexity | 4035d356180ba5284942ba1263cb3a07 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. class ProductsController < Spree::Api::BaseController
  4. respond_to :json
  5. def index
  6. if params[:ids]
  7. @products = product_scope.where(:id => params[:ids])
  8. else
  9. @products = product_scope.ransack(params[:q]).result
  10. end
  11. @products = @products.page(params[:page]).per(params[:per_page])
  12. respond_with(@products)
  13. end
  14. def show
  15. @product = find_product(params[:id])
  16. respond_with(@product)
  17. end
  18. def new
  19. end
  20. def create
  21. authorize! :create, Product
  22. params[:product][:available_on] ||= Time.now
  23. @product = Product.new(params[:product])
  24. if @product.save
  25. respond_with(@product, :status => 201, :default_template => :show)
  26. else
  27. invalid_resource!(@product)
  28. end
  29. end
  30. def update
  31. authorize! :update, Product
  32. @product = find_product(params[:id])
  33. if @product.update_attributes(params[:product])
  34. respond_with(@product, :status => 200, :default_template => :show)
  35. else
  36. invalid_resource!(@product)
  37. end
  38. end
  39. def destroy
  40. authorize! :delete, Product
  41. @product = find_product(params[:id])
  42. @product.update_attribute(:deleted_at, Time.now)
  43. @product.variants_including_master.update_all(:deleted_at => Time.now)
  44. respond_with(@product, :status => 204)
  45. end
  46. end
  47. end
  48. end