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

Instance Method Details

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/spree/api/taxons_controller.rb', line 37

def create
  authorize! :create, Taxon
  @taxon = Spree::Taxon.new(taxon_params)
  @taxon.taxonomy_id = params[:taxonomy_id]
  taxonomy = Spree::Taxonomy.find_by(id: params[:taxonomy_id])

  if taxonomy.nil?
    @taxon.errors.add(:taxonomy_id, I18n.t('spree.api.invalid_taxonomy_id'))
    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

#destroyObject



66
67
68
69
70
# File 'app/controllers/spree/api/taxons_controller.rb', line 66

def destroy
  authorize! :destroy, taxon
  taxon.destroy
  respond_with(taxon, status: 204)
end

#indexObject



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

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

  unless params[:without_children]
    @taxons = @taxons.includes(:children)
  end

  @taxons = paginate(@taxons)
  preload_taxon_parents(@taxons)
  respond_with(@taxons)
end

#jstreeObject



32
33
34
35
# File 'app/controllers/spree/api/taxons_controller.rb', line 32

def jstree
  Spree::Deprecation.warn("Please don't use `/api/taxonomies/:taxonomy_id/taxons/:taxon_id/jstree` endpoint. It is deprecated and will be removed in the next future.", caller)
  show
end

#newObject



24
25
# File 'app/controllers/spree/api/taxons_controller.rb', line 24

def new
end

#productsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/spree/api/taxons_controller.rb', line 72

def products
  # Returns the products sorted by their position with the classification
  # Products#index does not do the sorting.
  taxon = Spree::Taxon.find(params[:id])
  @products = paginate(taxon.products.ransack(params[:q]).result)
  @products = @products.includes(master: :default_price)

  if params[:simple]
    @exclude_data = {
      variants: true,
      option_types: true,
      product_properties: true,
      classifications: true
    }
    @product_attributes = i(id name display_price)
  end
  render "spree/api/products/index"
end

#showObject



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

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

#updateObject



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

def update
  authorize! :update, taxon
  if taxon.update_attributes(taxon_params)
    respond_with(taxon, status: 200, default_template: :show)
  else
    invalid_resource!(taxon)
  end
end