Skip to content

Commit

Permalink
Javascript to handle users finished. Added check to make sure approve…
Browse files Browse the repository at this point in the history
…d users are not stuck on the approval page. Same for suspended
  • Loading branch information
gabceb committed Feb 22, 2013
1 parent 7b11928 commit 53339a7
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 89 deletions.
64 changes: 57 additions & 7 deletions app/assets/javascripts/admin/admin.js.coffee
@@ -1,21 +1,71 @@
$(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", ".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)
return

act_on_user = (obj)->
user_id = $(this).closest("tr").data("user-id")
$el = $(this)
$row = $el.closest("tr")

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

request = $.post '/admin/update_user',
user_id: user_id
action_taken: action_taken

request.success (data) ->
alert("done")
# We will set the new and old css actions depending on the action that was taken because this method
# is used by 3 different buttons
if action_taken == "suspend"
old_btn_class = "suspend"
old_css_class = "btn-danger"
new_btn_class = "btn-success"
new_css_class = "activate"
else
# Approve and activate will be almost the same except for the original css class
if action_taken == "activate"
old_css_class = "activate"
else
old_css_class = "approve"

old_btn_class = "btn-success"
new_btn_class = "btn-danger"
new_css_class = "suspend"

btn_text = _.str.titleize(new_css_class)

$row.find("td.status").text(_.str.titleize(data.status))

# Change the look of the buttons by removing and adding classes
$el.text(btn_text).removeClass("#{old_btn_class} #{old_css_class}").addClass("#{new_btn_class} #{new_css_class}")

# Check if the user was an approval. If so, move the user out of the waiting for approval table and add it to the approved table
if action_taken == "approve"
$row.remove()
$(".approved-users table").show().append($row)

toggleTableIfNeeded $(".waiting-for-approval-users")
toggleTableIfNeeded $(".approved-users")

return

request.error (data, textStatus, jqXHR) ->
alert('error')

return
return

# Toggles a table and the container that says there are no users if needed
toggleTableIfNeeded = ($container)->
$table = $container.find("table")
$no_user_el = $container.find(".js-no-users")

if $table.find("tbody tr").length == 0
$table.hide()
$no_user_el.show()
else
$table.show()
$no_user_el.hide()

return
2 changes: 1 addition & 1 deletion app/controllers/admin/admin_controller.rb
Expand Up @@ -38,7 +38,7 @@ def update_user

user.save! if user.changed?

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

end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/pages_controller.rb
Expand Up @@ -6,9 +6,11 @@ class PagesController < ApplicationController
skip_filter :redirect_suspended_account, :only => :suspended

def approval
redirect_to(root_path) && return unless current_user.status.waiting_approval?
end

def suspended
redirect_to(root_path) && return unless current_user.status.suspended?
end

def about
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/admin/admin_helper.rb
Expand Up @@ -14,4 +14,8 @@ def user_action user

"<button class='action #{action.downcase} btn #{css}'>#{action}</button>".html_safe
end

def inline_style_hidden(hidden)
"style=display:none;" if hidden
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -21,7 +21,7 @@ class User < ActiveRecord::Base
devise devise *Kandan.devise_modules

# Setup accessible (or protected) attributes for your model
attr_accessible :id, :username, :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :locale, :gravatar_hash
attr_accessible :id, :username, :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :locale, :gravatar_hash, :status

def full_name
"#{self.first_name.to_s} #{self.last_name.to_s}".titlecase
Expand Down
37 changes: 37 additions & 0 deletions app/views/admin/admin/_user_table.html.erb
@@ -0,0 +1,37 @@
<table <%= inline_style_hidden(users.empty?)%> class="table table-striped table-bordered" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th class="username">Username</th>
<th class="first_name">First Name</th>
<th class="last_name">Last Name</th>
<th class="email">Email</th>
<th class="statu">Status</th>
<th class="action">Action</th>
</tr>
</thead>
<tbody>
<% users.each do |user| %>
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>">
<td class="username">
<%= user.username %>
</td>
<td class="first_name">
<%= user.first_name %>
</td>
<td class="last_name">
<%= user.last_name %>
</td>
<td class="email">
<%= user.email %>
</td>
<td class="status">
<%= user_status(user) %>
</td>
<td class="action">
<%= user_action(user) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="js-no-users" <%= inline_style_hidden(users.any?)%> >There are no users. Invite others to join Kandan!</div>
82 changes: 2 additions & 80 deletions app/views/admin/admin/index.html.erb
Expand Up @@ -28,90 +28,12 @@

<div class="waiting-for-approval-users">
<h3>Users waiting for approval</h3>
<% if @waiting_for_approval_users.any? %>
<table class="table table-striped table-bordered" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th class="">Username</th>
<th class="">First Name</th>
<th class="">Last Name</th>
<th class="">Email</th>
<th class="">Status</th>
<th class="">Action</th>
</tr>
</thead>
<tbody>
<% @waiting_for_approval_users.each do |user| %>
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>">
<td>
<%= user.username %>
</td>
<td>
<%= user.first_name %>
</td>
<td>
<%= user.last_name %>
</td>
<td>
<%= user.email %>
</td>
<td>
<%= user_status(user) %>
</td>
<td>
<%= user_action(user) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div>There are no users. Invite others to join Kandan!</div>
<% end %>
<%= render "user_table", :users => @waiting_for_approval_users %>
</div>

<div class="approved-users">
<h3>All users</h3>
<% if @approved_users.any? %>
<table class="table table-striped table-bordered" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th class="">Username</th>
<th class="">First Name</th>
<th class="">Last Name</th>
<th class="">Email</th>
<th class="">Status</th>
<th class="">Action</th>
</tr>
</thead>
<tbody>
<% @approved_users.each do |user| %>
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>">
<td>
<%= user.username %>
</td>
<td>
<%= user.first_name %>
</td>
<td>
<%= user.last_name %>
</td>
<td>
<%= user.email %>
</td>
<td>
<%= user_status(user) %>
</td>
<td>
<%= user_action(user) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div>There are no users. Invite others to join Kandan!</div>
<% end %>
<%= render "user_table", :users => @approved_users %>
</div>
</div>
</div>

0 comments on commit 53339a7

Please sign in to comment.