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

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

https://github.com/tomz/spree
Ruby | 72 lines | 61 code | 11 blank | 0 comment | 4 complexity | dba4e33c05b05ff8a58c624ffea93156 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. class ProductPropertiesController < Spree::Api::BaseController
  4. before_filter :find_product
  5. before_filter :product_property, only: [:show, :update, :destroy]
  6. def index
  7. @product_properties = @product.product_properties.accessible_by(current_ability, :read).
  8. ransack(params[:q]).result.
  9. page(params[:page]).per(params[:per_page])
  10. respond_with(@product_properties)
  11. end
  12. def show
  13. respond_with(@product_property)
  14. end
  15. def new
  16. 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. if @product_property
  28. authorize! :update, @product_property
  29. @product_property.update_attributes(product_property_params)
  30. respond_with(@product_property, status: 200, default_template: :show)
  31. else
  32. invalid_resource!(@product_property)
  33. end
  34. end
  35. def destroy
  36. if @product_property
  37. authorize! :destroy, @product_property
  38. @product_property.destroy
  39. respond_with(@product_property, status: 204)
  40. else
  41. invalid_resource!(@product_property)
  42. end
  43. end
  44. private
  45. def find_product
  46. @product = super(params[:product_id])
  47. authorize! :read, @product
  48. end
  49. def product_property
  50. if @product
  51. @product_property ||= @product.product_properties.find_by(id: params[:id])
  52. @product_property ||= @product.product_properties.includes(:property).where(spree_properties: { name: params[:id] }).first
  53. authorize! :read, @product_property
  54. end
  55. end
  56. def product_property_params
  57. params.require(:product_property).permit(permitted_product_properties_attributes)
  58. end
  59. end
  60. end
  61. end