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

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

https://gitlab.com/adamlwalker/spree
Ruby | 73 lines | 63 code | 10 blank | 0 comment | 4 complexity | f9f924073ba9e24d514e06d8ebda49da MD5 | raw file
  1. module Spree
  2. module Api
  3. module V1
  4. class ProductPropertiesController < Spree::Api::BaseController
  5. before_action :find_product
  6. before_action :product_property, only: [:show, :update, :destroy]
  7. def index
  8. @product_properties = @product.product_properties.accessible_by(current_ability, :read).
  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
  17. end
  18. def create
  19. authorize! :create, ProductProperty
  20. @product_property = @product.product_properties.new(product_property_params)
  21. if @product_property.save
  22. respond_with(@product_property, status: 201, default_template: :show)
  23. else
  24. invalid_resource!(@product_property)
  25. end
  26. end
  27. def update
  28. if @product_property
  29. authorize! :update, @product_property
  30. @product_property.update_attributes(product_property_params)
  31. respond_with(@product_property, status: 200, default_template: :show)
  32. else
  33. invalid_resource!(@product_property)
  34. end
  35. end
  36. def destroy
  37. if @product_property
  38. authorize! :destroy, @product_property
  39. @product_property.destroy
  40. respond_with(@product_property, status: 204)
  41. else
  42. invalid_resource!(@product_property)
  43. end
  44. end
  45. private
  46. def find_product
  47. @product = super(params[:product_id])
  48. authorize! :read, @product
  49. end
  50. def product_property
  51. if @product
  52. @product_property ||= @product.product_properties.find_by(id: params[:id])
  53. @product_property ||= @product.product_properties.includes(:property).where(spree_properties: { name: params[:id] }).first
  54. authorize! :read, @product_property
  55. end
  56. end
  57. def product_property_params
  58. params.require(:product_property).permit(permitted_product_properties_attributes)
  59. end
  60. end
  61. end
  62. end
  63. end