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

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

https://gitlab.com/shinvdu/spree
Ruby | 63 lines | 55 code | 8 blank | 0 comment | 4 complexity | 75d678e735927572e32f128d75e141c9 MD5 | raw file
  1. module Spree
  2. module Api
  3. module V1
  4. class OptionValuesController < Spree::Api::BaseController
  5. def index
  6. if params[:ids]
  7. @option_values = scope.where(id: params[:ids])
  8. else
  9. @option_values = 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
  18. end
  19. def create
  20. authorize! :create, Spree::OptionValue
  21. @option_value = scope.new(option_value_params)
  22. if @option_value.save
  23. render :show, status: 201
  24. else
  25. invalid_resource!(@option_value)
  26. end
  27. end
  28. def update
  29. @option_value = scope.accessible_by(current_ability, :update).find(params[:id])
  30. if @option_value.update_attributes(option_value_params)
  31. render :show
  32. else
  33. invalid_resource!(@option_value)
  34. end
  35. end
  36. def destroy
  37. @option_value = scope.accessible_by(current_ability, :destroy).find(params[:id])
  38. @option_value.destroy
  39. render text: nil, status: 204
  40. end
  41. private
  42. def scope
  43. if params[:option_type_id]
  44. @scope ||= Spree::OptionType.find(params[:option_type_id]).option_values.accessible_by(current_ability, :read)
  45. else
  46. @scope ||= Spree::OptionValue.accessible_by(current_ability, :read).load
  47. end
  48. end
  49. def option_value_params
  50. params.require(:option_value).permit(permitted_option_value_attributes)
  51. end
  52. end
  53. end
  54. end
  55. end