Skip to content

Commit

Permalink
Added some js and actions to update a user status
Browse files Browse the repository at this point in the history
  • Loading branch information
gabceb committed Feb 22, 2013
1 parent 7e5c285 commit 834fef9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
19 changes: 19 additions & 0 deletions app/assets/javascripts/admin/admin.js.coffee
@@ -1,2 +1,21 @@
$(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)
return

act_on_user = (obj)->
user_id = $(this).closest("tr").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")

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

return
22 changes: 20 additions & 2 deletions app/controllers/admin/admin_controller.rb
Expand Up @@ -6,7 +6,11 @@ def index
@all_users = User.find(:all, :conditions => ["id != ?", current_user.id])

# Note that this reject! will remove users from all_users in order to show users in 2 different tables
@waiting_for_approval_users = @all_users.select{|user| user.status.waiting_approval? } || []
@waiting_for_approval_users = []
@approved_users = []

# Iterate over the array to get approved and non-approved users
@all_users.each{|user| user.status.waiting_approval? ? @waiting_for_approval_users.push(user) : @approved_users.push(user) }
end

def update
Expand All @@ -19,8 +23,22 @@ def update
redirect_to :admin_root
end

def update_users
def update_user
user_id = params[:user_id]
action = params[:action_taken].to_s

user = User.find(user_id)

case action
when "activate", "approve"
user.status = "active"
when "suspend"
user.status = "suspended"
end

user.save! if user.changed?

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

end
Expand Down
10 changes: 9 additions & 1 deletion app/helpers/admin/admin_helper.rb
Expand Up @@ -4,6 +4,14 @@ def user_status user
end

def user_action user

action = if user.status.waiting_approval?
"Approve"
elsif user.status.suspended?
"Activate"
else
"Suspend"
end

"<button class='action #{action.downcase}'>#{action}</button>".html_safe
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -7,7 +7,7 @@ class User < ActiveRecord::Base
has_many :activities
before_save :ensure_authentication_token
before_save :ensure_gravatar_hash
before_save :mark_status_depending_on_app_settings
before_create :mark_status_depending_on_app_settings

after_create :ensure_at_least_one_admin
after_destroy :ensure_at_least_one_admin
Expand Down
14 changes: 7 additions & 7 deletions app/views/admin/admin/index.html.erb
Expand Up @@ -3,7 +3,7 @@
<% end %>

<div class="admin-main-area">
<%= form_for @settings, :url => admin_update_path do |f| %>
<%= form_for @settings, :url => admin_update_path, :method => :post do |f| %>
<%= f.label :max_rooms, 'Max number of rooms' %>:
<%= f.text_field :max_rooms %><br />
<%= f.label :public_site, 'Is this a public site?' %>:
Expand All @@ -12,8 +12,8 @@
<% end %>

<div class="waiting-for-approval-users">
<div>Users waiting for approval</div>
<% if @waiting_for_approval_users.any? %>
<div>Users waiting for approval</div>
<table cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
Expand Down Expand Up @@ -44,7 +44,7 @@
<%= user_status(user) %>
</td>
<td>

<%= user_action(user) %>
</td>
</tr>
<% end %>
Expand All @@ -56,8 +56,8 @@
</div>

<div class="approved-users">
<div>All users</div>
<% if @all_users.any? %>
<% if @approved_users.any? %>
<div>All users</div>
<table cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
Expand All @@ -70,7 +70,7 @@
</tr>
</thead>
<tbody>
<% @all_users.each do |user| %>
<% @approved_users.each do |user| %>
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>">
<td>
<%= user.username %>
Expand All @@ -88,7 +88,7 @@
<%= user_status(user) %>
</td>
<td>

<%= user_action(user) %>
</td>
</tr>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Expand Up @@ -20,8 +20,8 @@

namespace :admin do
root :to => "admin#index"
put "/update", :to => "admin#update", :as => "update"
put "/update_users", :to => "admin#update_users", :as => "update_users"
post "/update", :to => "admin#update", :as => "update"
post "/update_user", :to => "admin#update_user", :as => "update_user"
end

# Pages Controller
Expand Down

0 comments on commit 834fef9

Please sign in to comment.