Class: Spree::Api::TaxonsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/api/taxons_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Methods inherited from BaseController

#map_nested_attributes_keys, #set_jsonp_format

Methods included from ControllerSetup

included

Instance Method Details

#createObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/spree/api/taxons_controller.rb', line 30

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

#destroyObject



59
60
61
62
63
# File 'app/controllers/spree/api/taxons_controller.rb', line 59

def destroy
  authorize! :delete, Taxon
  taxon.destroy
  respond_with(taxon, :status => 204)
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/spree/api/taxons_controller.rb', line 6

def index
  if taxonomy
    @taxons = taxonomy.root.children
  else
    if params[:ids]
      @taxons = Taxon.accessible_by(current_ability, :read).where(:id => params[:ids].split(","))
    else
      @taxons = Taxon.accessible_by(current_ability, :read).order(:taxonomy_id, :lft).ransack(params[:q]).result
    end
  end

  @taxons = @taxons.page(params[:page]).per(params[:per_page])
  respond_with(@taxons)
end

#jstreeObject



26
27
28
# File 'app/controllers/spree/api/taxons_controller.rb', line 26

def jstree
  show
end

#showObject



21
22
23
24
# File 'app/controllers/spree/api/taxons_controller.rb', line 21

def show
  @taxon = taxon
  respond_with(@taxon)
end

#updateObject



50
51
52
53
54
55
56
57
# File 'app/controllers/spree/api/taxons_controller.rb', line 50

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