PageRenderTime 1329ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

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