Sha256: bd91b9dec8050adc5ac95a2404cabe72fb600291ac24d8c5bb81a89834a4b228

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

module Spree
  module Api
    class TaxonsController < Spree::Api::BaseController
      respond_to :json

      def index
        @taxons = taxonomy.root.children
        respond_with(@taxons)
      end

      def show
        @taxon = taxon
        respond_with(@taxon)
      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) and 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
        @taxonomy ||= Taxonomy.find(params[:taxonomy_id])
      end

      def taxon
        @taxon ||= taxonomy.taxons.find(params[:id])
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_api-1.3.5 app/controllers/spree/api/taxons_controller.rb
spree_api-1.3.4 app/controllers/spree/api/taxons_controller.rb
spree_api-1.3.3 app/controllers/spree/api/taxons_controller.rb