diff --git a/app/controllers/api/taxonomies_controller.rb b/app/controllers/api/taxonomies_controller.rb new file mode 100644 index 0000000000..356d84d676 --- /dev/null +++ b/app/controllers/api/taxonomies_controller.rb @@ -0,0 +1,12 @@ +module Api + class TaxonomiesController < Api::BaseController + respond_to :json + + skip_authorization_check only: :jstree + + def jstree + @taxonomy = Spree::Taxonomy.find(params[:id]) + render json: @taxonomy.root, serializer: Api::TaxonJstreeSerializer + end + end +end diff --git a/app/controllers/api/taxons_controller.rb b/app/controllers/api/taxons_controller.rb new file mode 100644 index 0000000000..13a079f568 --- /dev/null +++ b/app/controllers/api/taxons_controller.rb @@ -0,0 +1,71 @@ +module Api + class TaxonsController < Api::BaseController + respond_to :json + + skip_authorization_check only: [:index, :show, :jstree] + + def index + if taxonomy + @taxons = taxonomy.root.children + else + if params[:ids] + @taxons = Spree::Taxon.where(id: params[:ids].split(",")) + else + @taxons = Spree::Taxon.ransack(params[:q]).result + end + end + render json: @taxons, each_serializer: Api::TaxonSerializer + end + + def jstree + @taxon = taxon + render json: @taxon.children, each_serializer: Api::TaxonJstreeSerializer + end + + def create + authorize! :create, Spree::Taxon + @taxon = Spree::Taxon.new(params[:taxon]) + @taxon.taxonomy_id = params[:taxonomy_id] + taxonomy = Spree::Taxonomy.find_by_id(params[:taxonomy_id]) + + if taxonomy.nil? + @taxon.errors[:taxonomy_id] = I18n.t(:invalid_taxonomy_id, scope: 'spree.api') + invalid_resource!(@taxon) && return + end + + @taxon.parent_id = taxonomy.root.id unless params[:taxon][:parent_id] + + if @taxon.save + render json: @taxon, serializer: Api::TaxonSerializer, status: :created + else + invalid_resource!(@taxon) + end + end + + def update + authorize! :update, Spree::Taxon + if taxon.update_attributes(params[:taxon]) + render json: taxon, serializer: Api::TaxonSerializer, status: :ok + else + invalid_resource!(taxon) + end + end + + def destroy + authorize! :delete, Spree::Taxon + taxon.destroy + render json: taxon, serializer: Api::TaxonSerializer, status: :no_content + end + + private + + def taxonomy + return if params[:taxonomy_id].blank? + @taxonomy ||= Spree::Taxonomy.find(params[:taxonomy_id]) + end + + def taxon + @taxon ||= taxonomy.taxons.find(params[:id]) + end + end +end diff --git a/app/controllers/spree/api/taxons_controller.rb b/app/controllers/spree/api/taxons_controller.rb deleted file mode 100644 index a6fe754184..0000000000 --- a/app/controllers/spree/api/taxons_controller.rb +++ /dev/null @@ -1,75 +0,0 @@ -module Spree - module Api - class TaxonsController < Spree::Api::BaseController - respond_to :json - - def index - if taxonomy - @taxons = taxonomy.root.children - else - if params[:ids] - @taxons = Taxon.where(id: params[:ids].split(",")) - else - @taxons = Taxon.ransack(params[:q]).result - end - end - respond_with(@taxons) - end - - def show - @taxon = taxon - respond_with(@taxon) - end - - def jstree - show - end - - def create - authorize! :create, Taxon - @taxon = Taxon.new(params[:taxon]) - @taxon.taxonomy_id = params[:taxonomy_id] - taxonomy = Taxonomy.find_by_id(params[:taxonomy_id]) - - if taxonomy.nil? - @taxon.errors[:taxonomy_id] = I18n.t(:invalid_taxonomy_id, scope: 'spree.api') - invalid_resource!(@taxon) && return - end - - @taxon.parent_id = taxonomy.root.id unless params[:taxon][:parent_id] - - if @taxon.save - respond_with(@taxon, status: 201, default_template: :show) - else - invalid_resource!(@taxon) - end - end - - def update - authorize! :update, Taxon - if taxon.update_attributes(params[:taxon]) - respond_with(taxon, status: 200, default_template: :show) - else - invalid_resource!(taxon) - end - end - - def destroy - authorize! :delete, Taxon - taxon.destroy - respond_with(taxon, status: 204) - end - - private - - def taxonomy - return if params[:taxonomy_id].blank? - @taxonomy ||= Taxonomy.find(params[:taxonomy_id]) - end - - def taxon - @taxon ||= taxonomy.taxons.find(params[:id]) - end - end - end -end diff --git a/app/serializers/api/taxon_jstree_attribute_serializer.rb b/app/serializers/api/taxon_jstree_attribute_serializer.rb new file mode 100644 index 0000000000..abf10d406d --- /dev/null +++ b/app/serializers/api/taxon_jstree_attribute_serializer.rb @@ -0,0 +1,5 @@ +module Api + class TaxonJstreeAttributeSerializer < ActiveModel::Serializer + attributes :id, :name + end +end diff --git a/app/serializers/api/taxon_jstree_serializer.rb b/app/serializers/api/taxon_jstree_serializer.rb new file mode 100644 index 0000000000..cd457c644b --- /dev/null +++ b/app/serializers/api/taxon_jstree_serializer.rb @@ -0,0 +1,18 @@ +module Api + class TaxonJstreeSerializer < ActiveModel::Serializer + attributes :data, :state + has_one :attr, serializer: TaxonJstreeAttributeSerializer + + def data + object.name + end + + def attr + object + end + + def state + "closed" + end + end +end diff --git a/app/serializers/api/taxon_serializer.rb b/app/serializers/api/taxon_serializer.rb index 352a158e95..775c069a41 100644 --- a/app/serializers/api/taxon_serializer.rb +++ b/app/serializers/api/taxon_serializer.rb @@ -2,7 +2,7 @@ class Api::TaxonSerializer < ActiveModel::Serializer cached delegate :cache_key, to: :object - attributes :id, :name, :permalink, :icon + attributes :id, :name, :permalink, :icon, :pretty_name, :position, :parent_id, :taxonomy_id def icon object.icon(:original) diff --git a/app/views/spree/admin/shared/_routes.html.erb b/app/views/spree/admin/shared/_routes.html.erb index 056a49cf0c..b84c691ea9 100644 --- a/app/views/spree/admin/shared/_routes.html.erb +++ b/app/views/spree/admin/shared/_routes.html.erb @@ -1,7 +1,7 @@