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

https://github.com/joshnuss/spree-1 · Ruby · 58 lines · 48 code · 9 blank · 1 comment · 3 complexity · 99274b80a92665584e4fd7c2f36a0c7a MD5 · raw file

  1. module Spree
  2. module Api
  3. class TaxonomiesController < Spree::Api::BaseController
  4. def index
  5. @taxonomies = Taxonomy.accessible_by(current_ability, :read).order('name').includes(:root => :children).
  6. ransack(params[:q]).result.
  7. page(params[:page]).per(params[:per_page])
  8. last_taxonomy = Spree::Taxonomy.order("updated_at ASC").last
  9. if stale?(:etag => last_taxonomy, :last_modified => last_taxonomy.updated_at)
  10. respond_with(@taxonomies)
  11. end
  12. end
  13. def show
  14. @taxonomy = Taxonomy.accessible_by(current_ability, :read).find(params[:id])
  15. respond_with(@taxonomy)
  16. end
  17. # Because JSTree wants parameters in a *slightly* different format
  18. def jstree
  19. show
  20. end
  21. def create
  22. authorize! :create, Taxonomy
  23. @taxonomy = Taxonomy.new(params[:taxonomy])
  24. if @taxonomy.save
  25. respond_with(@taxonomy, :status => 201, :default_template => :show)
  26. else
  27. invalid_resource!(@taxonomy)
  28. end
  29. end
  30. def update
  31. authorize! :update, taxonomy
  32. if taxonomy.update_attributes(params[:taxonomy])
  33. respond_with(taxonomy, :status => 200, :default_template => :show)
  34. else
  35. invalid_resource!(taxonomy)
  36. end
  37. end
  38. def destroy
  39. authorize! :destroy, taxonomy
  40. taxonomy.destroy
  41. respond_with(taxonomy, :status => 204)
  42. end
  43. private
  44. def taxonomy
  45. @taxonomy ||= Taxonomy.accessible_by(current_ability, :read).find(params[:id])
  46. end
  47. end
  48. end
  49. end