PageRenderTime 1366ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/spree/spree
Ruby | 62 lines | 54 code | 8 blank | 0 comment | 4 complexity | 647027e7a22745fb97ff237ae2f7f93b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. module V1
  4. class OptionValuesController < Spree::Api::BaseController
  5. def index
  6. @option_values = if params[:ids]
  7. scope.where(id: params[:ids])
  8. else
  9. scope.ransack(params[:q]).result.distinct
  10. end
  11. respond_with(@option_values)
  12. end
  13. def show
  14. @option_value = scope.find(params[:id])
  15. respond_with(@option_value)
  16. end
  17. def new; end
  18. def create
  19. authorize! :create, Spree::OptionValue
  20. @option_value = scope.new(option_value_params)
  21. if @option_value.save
  22. render :show, status: 201
  23. else
  24. invalid_resource!(@option_value)
  25. end
  26. end
  27. def update
  28. @option_value = scope.accessible_by(current_ability, :update).find(params[:id])
  29. if @option_value.update(option_value_params)
  30. render :show
  31. else
  32. invalid_resource!(@option_value)
  33. end
  34. end
  35. def destroy
  36. @option_value = scope.accessible_by(current_ability, :destroy).find(params[:id])
  37. @option_value.destroy
  38. render plain: nil, status: 204
  39. end
  40. private
  41. def scope
  42. @scope ||= if params[:option_type_id]
  43. Spree::OptionType.find(params[:option_type_id]).option_values.accessible_by(current_ability, :show)
  44. else
  45. Spree::OptionValue.accessible_by(current_ability, :show).load
  46. end
  47. end
  48. def option_value_params
  49. params.require(:option_value).permit(permitted_option_value_attributes)
  50. end
  51. end
  52. end
  53. end
  54. end