Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created April 8, 2011 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/909719 to your computer and use it in GitHub Desktop.
Save krisleech/909719 to your computer and use it in GitHub Desktop.
<div id="left">
<%=render(:partial => "user_widget")%>
</div>
<div id="right">
<div class="box">
<div class="title"><h5>Users</h5></div>
<%
fields = [
{:label => 'Name', :method => :last_name, :display => lambda { |u| u.name } },
{:label => 'Registered', :method => :created_at, :display => lambda { |u| u.created_at.to_s(:short) } },
{:label => 'Type', :method => :type },
{:label => 'Approved', :method => :approved, :display => lambda { |u| u.approved? ? 'Yes' : 'No' } },
{:label => 'Active', :method => :active, :display => lambda { |u| u.active? ? 'Yes' : 'No' } },
]
%>
<!--
<% %w(asc desc).each do | dir | %>
<p>
<% fields.each do | field | %>
<% next if field[:sortable] == false %>
<% sort_key = "#{field[:method]} #{dir}" %>
<%= link_to "#{field[:label]}", admin_users_path(:page => 1, :sort => sort_key), :class => "link-as-button#{sort_key == params[:sort] ? ' current' : ''}" %>
<% end %>
<%= dir.upcase %>
</p>
<% end %>
-->
<div class="sorter">
<form action="<%= admin_users_path(:page=>1) %>" method='get'>
Sort: <select name='sort' onchange="this.form.submit();">
<% fields.each do | field | %>
<% %w(asc desc).each do | dir | %>
<% sort_key = "#{field[:method]} #{dir}" %>
<option value="<%= sort_key %>" <%= 'selected="selected"' if sort_key == params[:sort] %>><%= field[:label] %> (<%= dir.capitalize %>)</option>
<% end %>
<% end %>
</select>
</form>
</div>
<div class="table">
<table>
<tr>
<% fields.each do | field | %>
<th><%= field[:label] %></th>
<% end %>
<th class="last">Actions</th>
</tr>
<% @users.each do |user| %>
<tr>
<% fields.each do | field | %>
<td><%= field[:display] ? field[:display].call(user) : user.send(field[:method]) %></td>
<% end %>
<td class="last">
<%= link_to 'Details', admin_user_path(user),:class => "link-as-button" %>
<%= link_to 'Edit', edit_admin_user_path(user),:class => "link-as-button" %>
<%= link_to 'Resend Activation', send_activation_admin_user_path(user), :method => :post,:class => "link-as-button" if !user.active? && user.approved? %>
<%= link_to 'Approve', approve_admin_user_path(user), :method => :post,:class => "link-as-button" unless user.approved? %>
<%= link_to 'Delete', admin_user_path(user), :confirm => 'Are you sure?', :method => :delete,:class => "link-as-button" %></td>
</tr>
<% end %>
</table>
</div>
<%= will_paginate @users %>
</div>
</div>
<style>
.current { color:red; }
.sorter { padding: 10px; margin-left: 20px; }
</style>
# admin/users_controller.rb
def index
params[:sort] ||= 'approved asc, last_name asc'
@users = current_university.users.order_by(params[:sort]).paginate(:page => params[:page])
end
# User.rb
named_scope :order_by, lambda { |sort| { :order => sort } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment