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

/api/app/controllers/spree/api/taxons_controller.rb

https://github.com/pageman/spree
Ruby | 78 lines | 64 code | 14 blank | 0 comment | 6 complexity | 3a945c9107675e503681396ee47214fe MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. class TaxonsController < Spree::Api::BaseController
  4. respond_to :json
  5. def index
  6. if taxonomy
  7. @taxons = taxonomy.root.children
  8. else
  9. if params[:ids]
  10. @taxons = Taxon.where(:id => params[:ids].split(","))
  11. else
  12. @taxons = Taxon.ransack(params[:q]).result
  13. end
  14. end
  15. respond_with(@taxons)
  16. end
  17. def show
  18. @taxon = taxon
  19. respond_with(@taxon)
  20. end
  21. def jstree
  22. show
  23. end
  24. def create
  25. authorize! :create, Taxon
  26. @taxon = Taxon.new(params[:taxon])
  27. @taxon.taxonomy_id = params[:taxonomy_id]
  28. taxonomy = Taxonomy.find_by_id(params[:taxonomy_id])
  29. if taxonomy.nil?
  30. @taxon.errors[:taxonomy_id] = "Invalid taxonomy_id."
  31. invalid_resource!(@taxon) and return
  32. end
  33. @taxon.parent_id = taxonomy.root.id
  34. if @taxon.save
  35. respond_with(@taxon, :status => 201, :default_template => :show)
  36. else
  37. invalid_resource!(@taxon)
  38. end
  39. end
  40. def update
  41. authorize! :update, Taxon
  42. if taxon.update_attributes(params[:taxon])
  43. respond_with(taxon, :status => 200, :default_template => :show)
  44. else
  45. invalid_resource!(taxon)
  46. end
  47. end
  48. def destroy
  49. authorize! :delete, Taxon
  50. taxon.destroy
  51. respond_with(taxon, :status => 204)
  52. end
  53. private
  54. def taxonomy
  55. if params[:taxonomy_id].present?
  56. @taxonomy ||= Taxonomy.find(params[:taxonomy_id])
  57. end
  58. end
  59. def taxon
  60. @taxon ||= taxonomy.taxons.find(params[:id])
  61. end
  62. end
  63. end
  64. end