Skip to content

Commit

Permalink
Added ability to have add admins
Browse files Browse the repository at this point in the history
  • Loading branch information
gabceb committed Feb 22, 2013
1 parent 9ac14de commit e0ed0fb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
33 changes: 32 additions & 1 deletion app/assets/javascripts/admin/admin.js.coffee
Expand Up @@ -2,6 +2,7 @@ $(document).ready ->
$(document).on("click", ".waiting-for-approval-users .action.approve", {action : "approve" }, act_on_user)
$(document).on("click", ".approved-users .action.suspend", {action : "suspend" }, act_on_user)
$(document).on("click", ".approved-users .action.activate", {action : "activate" }, act_on_user)
$(document).on("click", ".admin input[type='checkbox']", {}, toggelAdminOnUser)
return

act_on_user = (obj)->
Expand Down Expand Up @@ -52,10 +53,40 @@ act_on_user = (obj)->
return

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

return

toggelAdminOnUser = ()->
$el = $(this)
$row = $el.closest("tr")

user_id = $row.data("user-id")

checked = $el.prop("checked");

full_name = $row.data("full-name")

# Generate the message based on the check of the user admin flag
message = if checked then "make #{full_name} an administrator?" else "remove #{full_name} from the administrators?"
message = "Are you sure " + message

if(confirm(message) != true)
$el.prop("checked", !checked);
return

request = $.post '/admin/toggle_admin',
user_id: user_id

request.success (data) ->
alert("#{full_name} is now an administrator")
return

request.error (data, textStatus, jqXHR) ->
alert("Something went wrong while trying to make #{full_name} an administrator")
$el.prop("checked", !checked);
return

# Toggles a table and the container that says there are no users if needed
toggleTableIfNeeded = ($container)->
$table = $container.find("table")
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/admin/admin_controller.rb
Expand Up @@ -39,6 +39,18 @@ def update_user

render :json => user, :status => 200
end

def toggle_admin
user_id = params[:user_id]

user = User.find(user_id)

user.is_admin = !user.is_admin?

user.save!

render :json => user, :status => 200
end

end
end
7 changes: 6 additions & 1 deletion app/views/admin/admin/_user_table.html.erb
@@ -1,3 +1,4 @@
<% show_admin_column = defined?(show_admin) && show_admin ? true : false%>
<table <%= inline_style_hidden(users.empty?)%> class="table table-striped table-bordered" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
Expand All @@ -7,11 +8,12 @@
<th class="email">Email</th>
<th class="statu">Status</th>
<th class="action">Action</th>
<th class="admin" <%= inline_style_hidden( !show_admin_column ) %> >Admin</th>
</tr>
</thead>
<tbody>
<% users.each do |user| %>
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>">
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>" data-full-name="<%= user.full_name %>">
<td class="username">
<%= user.username %>
</td>
Expand All @@ -30,6 +32,9 @@
<td class="action">
<%= user_action(user) %>
</td>
<td class="admin" <%= inline_style_hidden( !show_admin_column ) %>>
<input type="checkbox" <%= "checked" if user.is_admin? %> />
</td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/admin/index.html.erb
Expand Up @@ -33,7 +33,7 @@

<div class="approved-users">
<h3>All users</h3>
<%= render "user_table", :users => @approved_users %>
<%= render "user_table", :users => @approved_users, :show_admin => true %>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -27,6 +27,7 @@
root :to => "admin#index"
post "/update", :to => "admin#update", :as => "update"
post "/update_user", :to => "admin#update_user", :as => "update_user"
post "/toggle_admin", :to => "admin#toggle_admin"
end

# Pages Controller
Expand Down

0 comments on commit e0ed0fb

Please sign in to comment.