Class: Admin::CategoriesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/admin/categories_controller.rb

Overview

This Controller Manage Categories

Instance Method Summary collapse

Instance Method Details

#add_elementObject



90
91
92
93
# File 'app/controllers/admin/categories_controller.rb', line 90

def add_element
  @category.update_attribute(:element_ids, @category.element_ids << params[:element_id].to_i)
  render :text => @category.elements.count
end

#createObject

Create a Category

Params

  • category = Hash of Category’s attributes

The Category can be a child of another Category.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/admin/categories_controller.rb', line 35

def create
  if @category.save
    flash[:notice] = t('category.create.success').capitalize
    respond_to do |format|
      format.html { redirect_to [forgeos_core, :edit, :admin, @category] }
      format.json { render :text => @category.id }
    end
  else
    flash[:error] = t('category.create.failed').capitalize
    render :action => 'new'
  end
end

#destroyObject

Destroy a Category

Params

  • id = Category’s id

Output

if destroy succed, return the Categories list


81
82
83
84
85
86
87
88
# File 'app/controllers/admin/categories_controller.rb', line 81

def destroy
  if @category.destroy
    flash[:notice] = t('category.destroy.success').capitalize
  else
    flash[:error] = t('category.destroy.failed').capitalize
  end
  render :text => true
end

#editObject

Edit a Category

Params

  • id = Category’s id to edit

  • category = Hash of Category’s attributes

The Category can be a child of another Category.



54
55
# File 'app/controllers/admin/categories_controller.rb', line 54

def edit
end

#indexObject

List Categories



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/admin/categories_controller.rb', line 8

def index
  respond_to do |format|
    format.html{ redirect_to([forgeos_core, :root]) }
    format.json do
      unless params[:type]
        # list page and product categories
        sort
        render :layout => false
      else
        # list categories like a tree
        klass = params[:type].camelize.constantize
        @categories = klass.roots

        render :json => @categories.collect(&:to_jstree).to_json
      end
    end
  end
end

#newObject



27
28
# File 'app/controllers/admin/categories_controller.rb', line 27

def new
end

#updateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/admin/categories_controller.rb', line 57

def update
  if categories = params[:categories_hash]
    paramz = ActiveSupport::JSON.decode(categories)
    paramz.each_with_index do | param, position |
      parent_id = nil
      update_category_from_params(param, position, parent_id)
    end
  elsif @category.update_attributes(params[:category])
    flash[:notice] = t('category.update.success').capitalize
  else
    flash[:error] = t('category.update.failed').capitalize
  end

  respond_to do |format|
    format.html { render :action => 'edit' }
    format.json { render :text => @category.total_elements_count }
  end
end