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

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

https://gitlab.com/adamlwalker/spree
Ruby | 72 lines | 60 code | 12 blank | 0 comment | 4 complexity | d4bf97a7f6d88a81702db6813a2e569d MD5 | raw file
  1. module Spree
  2. module Api
  3. module V1
  4. class PropertiesController < Spree::Api::BaseController
  5. before_action :find_property, only: [:show, :update, :destroy]
  6. def index
  7. @properties = Spree::Property.accessible_by(current_ability, :read)
  8. if params[:ids]
  9. @properties = @properties.where(id: params[:ids].split(",").flatten)
  10. else
  11. @properties = @properties.ransack(params[:q]).result
  12. end
  13. @properties = @properties.page(params[:page]).per(params[:per_page])
  14. respond_with(@properties)
  15. end
  16. def show
  17. respond_with(@property)
  18. end
  19. def new
  20. end
  21. def create
  22. authorize! :create, Property
  23. @property = Spree::Property.new(property_params)
  24. if @property.save
  25. respond_with(@property, status: 201, default_template: :show)
  26. else
  27. invalid_resource!(@property)
  28. end
  29. end
  30. def update
  31. if @property
  32. authorize! :update, @property
  33. @property.update_attributes(property_params)
  34. respond_with(@property, status: 200, default_template: :show)
  35. else
  36. invalid_resource!(@property)
  37. end
  38. end
  39. def destroy
  40. if @property
  41. authorize! :destroy, @property
  42. @property.destroy
  43. respond_with(@property, status: 204)
  44. else
  45. invalid_resource!(@property)
  46. end
  47. end
  48. private
  49. def find_property
  50. @property = Spree::Property.accessible_by(current_ability, :read).find(params[:id])
  51. rescue ActiveRecord::RecordNotFound
  52. @property = Spree::Property.accessible_by(current_ability, :read).find_by!(name: params[:id])
  53. end
  54. def property_params
  55. params.require(:property).permit(permitted_property_attributes)
  56. end
  57. end
  58. end
  59. end
  60. end