Class: Admin::RolesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/admin/roles_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_userObject



70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/admin/roles_controller.rb', line 70

def add_user
  role = Role.find(params[:role_id])
  user = User.find(params[:id])
  
  if role.users << user
    render :json => {:status => "Ok", :role_id => role.id, :user_id => user.id }.to_json
  else
    render :json => {:status => "Error", :user_id => user.id }.to_json
  end
end

#createObject



31
32
33
34
35
36
37
38
# File 'app/controllers/admin/roles_controller.rb', line 31

def create
  @role = Role.new(params[:role])
  @role.save!
  redirect_to admin_roles_path
rescue ActiveRecord::RecordInvalid => invalid
  flash[:error] = invalid.record.errors.full_messages
  render :action => :index
end

#destroyObject



40
41
42
43
44
45
46
47
# File 'app/controllers/admin/roles_controller.rb', line 40

def destroy
  @role = Role.find(params[:id])
  @role.destroy unless @role.standard?
  redirect_to admin_roles_path()
rescue ActiveRecord::RecordNotFound
  flash[:error] = 'The specified Role could not be found.'
  redirect_to :action => :index
end

#editObject



16
17
18
# File 'app/controllers/admin/roles_controller.rb', line 16

def edit
  
end

#indexObject



7
8
9
10
# File 'app/controllers/admin/roles_controller.rb', line 7

def index
  @roles = Role.find(:all)
  @role = Role.new
end

#newObject



19
20
21
# File 'app/controllers/admin/roles_controller.rb', line 19

def new
  
end

#remove_userObject



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

def remove_user 
  role = Role.find(params[:role_id])
  user = User.find(params[:id])
  
  if role.remove_user(user)
    render :json => {:status => "Ok", :role_id => role.id, :user_id => user.id }.to_json
  else
    render :json => {:status => "Error", :user_id => user.id }.to_json
  end
end

#showObject



12
13
14
# File 'app/controllers/admin/roles_controller.rb', line 12

def show
  @role = Role.find(params[:id])
end

#updateObject



22
23
24
25
26
27
28
29
# File 'app/controllers/admin/roles_controller.rb', line 22

def update
  @role = Role.find(params[:id])
  if @role.update_attributes(params[:role])
    redirect_to admin_role_path(@role)
  else
    render :action => 'show'
  end
end

#usersObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/admin/roles_controller.rb', line 49

def users
  role = Role.find(params[:role_id])
  
  available_users = User.find(:all, :conditions => ['id NOT IN (SELECT user_id FROM roles_users WHERE role_id = ?)', role.id])
  taken_users = role.users
  
  result = {:available => [], :taken => []}
  
  available_users.each do | usr |
    result[:available] << [usr.id, usr.name]
  end
  
  taken_users.each do | usr |
    result[:taken] << [usr.id, usr.name]
  end
  
  respond_to do |format|
    format.js { render :json => result.to_json }
  end
end