Skip to content

Commit e0ed0fb

Browse files
committed
Added ability to have add admins
1 parent 9ac14de commit e0ed0fb

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

app/assets/javascripts/admin/admin.js.coffee

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ $(document).ready ->
22
$(document).on("click", ".waiting-for-approval-users .action.approve", {action : "approve" }, act_on_user)
33
$(document).on("click", ".approved-users .action.suspend", {action : "suspend" }, act_on_user)
44
$(document).on("click", ".approved-users .action.activate", {action : "activate" }, act_on_user)
5+
$(document).on("click", ".admin input[type='checkbox']", {}, toggelAdminOnUser)
56
return
67

78
act_on_user = (obj)->
@@ -52,10 +53,40 @@ act_on_user = (obj)->
5253
return
5354

5455
request.error (data, textStatus, jqXHR) ->
55-
alert('error')
56+
alert('Something went wrong while trying to get change user status')
5657

5758
return
5859

60+
toggelAdminOnUser = ()->
61+
$el = $(this)
62+
$row = $el.closest("tr")
63+
64+
user_id = $row.data("user-id")
65+
66+
checked = $el.prop("checked");
67+
68+
full_name = $row.data("full-name")
69+
70+
# Generate the message based on the check of the user admin flag
71+
message = if checked then "make #{full_name} an administrator?" else "remove #{full_name} from the administrators?"
72+
message = "Are you sure " + message
73+
74+
if(confirm(message) != true)
75+
$el.prop("checked", !checked);
76+
return
77+
78+
request = $.post '/admin/toggle_admin',
79+
user_id: user_id
80+
81+
request.success (data) ->
82+
alert("#{full_name} is now an administrator")
83+
return
84+
85+
request.error (data, textStatus, jqXHR) ->
86+
alert("Something went wrong while trying to make #{full_name} an administrator")
87+
$el.prop("checked", !checked);
88+
return
89+
5990
# Toggles a table and the container that says there are no users if needed
6091
toggleTableIfNeeded = ($container)->
6192
$table = $container.find("table")

app/controllers/admin/admin_controller.rb

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ def update_user
3939

4040
render :json => user, :status => 200
4141
end
42+
43+
def toggle_admin
44+
user_id = params[:user_id]
45+
46+
user = User.find(user_id)
47+
48+
user.is_admin = !user.is_admin?
49+
50+
user.save!
51+
52+
render :json => user, :status => 200
53+
end
4254

4355
end
4456
end

app/views/admin/admin/_user_table.html.erb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<% show_admin_column = defined?(show_admin) && show_admin ? true : false%>
12
<table <%= inline_style_hidden(users.empty?)%> class="table table-striped table-bordered" cellspacing="0" cellpadding="0" border="0">
23
<thead>
34
<tr>
@@ -7,11 +8,12 @@
78
<th class="email">Email</th>
89
<th class="statu">Status</th>
910
<th class="action">Action</th>
11+
<th class="admin" <%= inline_style_hidden( !show_admin_column ) %> >Admin</th>
1012
</tr>
1113
</thead>
1214
<tbody>
1315
<% users.each do |user| %>
14-
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>">
16+
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>" data-full-name="<%= user.full_name %>">
1517
<td class="username">
1618
<%= user.username %>
1719
</td>
@@ -30,6 +32,9 @@
3032
<td class="action">
3133
<%= user_action(user) %>
3234
</td>
35+
<td class="admin" <%= inline_style_hidden( !show_admin_column ) %>>
36+
<input type="checkbox" <%= "checked" if user.is_admin? %> />
37+
</td>
3338
</tr>
3439
<% end %>
3540
</tbody>

app/views/admin/admin/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<div class="approved-users">
3535
<h3>All users</h3>
36-
<%= render "user_table", :users => @approved_users %>
36+
<%= render "user_table", :users => @approved_users, :show_admin => true %>
3737
</div>
3838
</div>
3939
</div>

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
root :to => "admin#index"
2828
post "/update", :to => "admin#update", :as => "update"
2929
post "/update_user", :to => "admin#update_user", :as => "update_user"
30+
post "/toggle_admin", :to => "admin#toggle_admin"
3031
end
3132

3233
# Pages Controller

0 commit comments

Comments
 (0)