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

/api/app/controllers/spree/api/taxonomies_controller.rb

https://github.com/tomz/spree
Ruby | 64 lines | 53 code | 10 blank | 1 comment | 4 complexity | 85033fb37d0dd7bd603f885bfd784422 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. class TaxonomiesController < Spree::Api::BaseController
  4. def index
  5. respond_with(taxonomies)
  6. end
  7. def show
  8. respond_with(taxonomy)
  9. end
  10. # Because JSTree wants parameters in a *slightly* different format
  11. def jstree
  12. show
  13. end
  14. def create
  15. authorize! :create, Taxonomy
  16. @taxonomy = Taxonomy.new(taxonomy_params)
  17. if @taxonomy.save
  18. respond_with(@taxonomy, :status => 201, :default_template => :show)
  19. else
  20. invalid_resource!(@taxonomy)
  21. end
  22. end
  23. def update
  24. authorize! :update, taxonomy
  25. if taxonomy.update_attributes(taxonomy_params)
  26. respond_with(taxonomy, :status => 200, :default_template => :show)
  27. else
  28. invalid_resource!(taxonomy)
  29. end
  30. end
  31. def destroy
  32. authorize! :destroy, taxonomy
  33. taxonomy.destroy
  34. respond_with(taxonomy, :status => 204)
  35. end
  36. private
  37. def taxonomies
  38. @taxonomies = Taxonomy.accessible_by(current_ability, :read).order('name').includes(:root => :children).
  39. ransack(params[:q]).result.
  40. page(params[:page]).per(params[:per_page])
  41. end
  42. def taxonomy
  43. @taxonomy ||= Taxonomy.accessible_by(current_ability, :read).find(params[:id])
  44. end
  45. def taxonomy_params
  46. if params[:taxonomy] && !params[:taxonomy].empty?
  47. params.require(:taxonomy).permit(permitted_taxonomy_attributes)
  48. else
  49. {}
  50. end
  51. end
  52. end
  53. end
  54. end