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

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

http://github.com/spree/spree
Ruby | 73 lines | 60 code | 13 blank | 0 comment | 3 complexity | f141f265b407cb7c9514b933ea0a888e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. module V1
  4. class ProductPropertiesController < Spree::Api::BaseController
  5. before_action :find_product, :authorize_product!
  6. before_action :product_property, only: [:show, :update, :destroy]
  7. def index
  8. @product_properties = @product.product_properties.accessible_by(current_ability).
  9. ransack(params[:q]).result.
  10. page(params[:page]).per(params[:per_page])
  11. respond_with(@product_properties)
  12. end
  13. def show
  14. respond_with(@product_property)
  15. end
  16. def new; end
  17. def create
  18. authorize! :create, ProductProperty
  19. @product_property = @product.product_properties.new(product_property_params)
  20. if @product_property.save
  21. respond_with(@product_property, status: 201, default_template: :show)
  22. else
  23. invalid_resource!(@product_property)
  24. end
  25. end
  26. def update
  27. authorize! :update, @product_property
  28. if @product_property.update(product_property_params)
  29. respond_with(@product_property, status: 200, default_template: :show)
  30. else
  31. invalid_resource!(@product_property)
  32. end
  33. end
  34. def destroy
  35. authorize! :destroy, @product_property
  36. @product_property.destroy
  37. respond_with(@product_property, status: 204)
  38. end
  39. private
  40. def find_product
  41. super(params[:product_id])
  42. end
  43. def authorize_product!
  44. authorize! :show, @product
  45. end
  46. def product_property
  47. if @product
  48. @product_property ||= @product.product_properties.find_by(id: params[:id])
  49. @product_property ||= @product.product_properties.includes(:property).where(spree_properties: { name: params[:id] }).first
  50. raise ActiveRecord::RecordNotFound unless @product_property
  51. authorize! :show, @product_property
  52. end
  53. end
  54. def product_property_params
  55. params.require(:product_property).permit(permitted_product_properties_attributes)
  56. end
  57. end
  58. end
  59. end
  60. end