Skip to content

Commit

Permalink
WIP: archived 'users' now work correctly; now working on other user '…
Browse files Browse the repository at this point in the history
…types'
  • Loading branch information
rnhurt committed Nov 2, 2010
1 parent d64513f commit 1e297b5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
14 changes: 11 additions & 3 deletions app/controllers/users_controller.rb
@@ -1,16 +1,25 @@
class UsersController < ApplicationController class UsersController < ApplicationController
before_filter :require_user before_filter :require_user
before_filter :decode_user_type, :except => "archive" before_filter :decode_user_type, :except => ["archive"]


append_before_filter :authorized? append_before_filter :authorized?
include SortHelper include SortHelper


# Show 'active' users
def index def index
sort_init 'last_name' sort_init 'last_name'
sort_update sort_update
params[:sort_clause] = sort_clause params[:sort_clause] = sort_clause
@users = @user_type.active.search(params) @users = @user_type.active.search(params)
end end
# Show 'archived' users
def archived
sort_init 'last_name'
sort_update
params[:sort_clause] = sort_clause
@users = @user_type.archived.search(params)
render :index
end




# Show the group of users # Show the group of users
Expand All @@ -23,7 +32,6 @@ def show
render :index render :index
end end



def new def new
@user = @user_type.new @user = @user_type.new
render :action => 'edit' render :action => 'edit'
Expand Down Expand Up @@ -85,7 +93,7 @@ def archive
# The list of users is sent to this method using the "on" value. Therefor we grab all the # The list of users is sent to this method using the "on" value. Therefor we grab all the
# elements in the params array that has a value of "on" and process them. # elements in the params array that has a value of "on" and process them.
params.reject{|k,v| v !="on" }.keys.each do |id| params.reject{|k,v| v !="on" }.keys.each do |id|
User.find(id).archive User.find(id).toggle_archive
end end


redirect_to users_url redirect_to users_url
Expand Down
12 changes: 6 additions & 6 deletions app/models/user.rb
Expand Up @@ -18,13 +18,13 @@ class User < ActiveRecord::Base
validates_existence_of :site validates_existence_of :site


named_scope :active, :conditions => { :active => true } named_scope :active, :conditions => { :active => true }
scope :archived, :conditions => { :active => false } named_scope :archived, :conditions => { :active => false }


# Search for a user using the 'will_paginate' plugin # Search for a user using the 'will_paginate' plugin
def self.search(params) def self.search(params)
search = params[:search] search = params[:search]
search.downcase! if search # Make sure we don't have any case sensitivity problems search.downcase! if search # Make sure we don't have any case sensitivity problems
paginate :per_page => 15, :page => params[:page], paginate :per_page => 20, :page => params[:page],
:conditions => ['LOWER(first_name) like ? or LOWER(last_name) like ?', "%#{search}%", "%#{search}%"], :conditions => ['LOWER(first_name) like ? or LOWER(last_name) like ?', "%#{search}%", "%#{search}%"],
:order => params[:sort_clause], :order => params[:sort_clause],
:include => :site :include => :site
Expand All @@ -35,7 +35,7 @@ def self.user_types
types = [] types = []
types << {'All (active)' => nil} << {'Students' => Student} types << {'All (active)' => nil} << {'Students' => Student}
types << {'Teachers' => Teacher} << {'Teacher Assistants' => TeacherAssistant} types << {'Teachers' => Teacher} << {'Teacher Assistants' => TeacherAssistant}
types << {'Archived' => nil} types << {:name => "archived", "Archived" => nil }
end end


# Display the user's full name. # Display the user's full name.
Expand All @@ -50,9 +50,9 @@ def full_name=(name)
self.last_name = split.last self.last_name = split.last
end end


# Archive a user by setting its "active" bit to false # Change a users Archive status by toggling it's "active" bit
def archive def toggle_archive
self.active = false self.active = !self.active
self.save! self.save!
end end
end end
8 changes: 4 additions & 4 deletions app/views/users/_user_list.html.erb
@@ -1,3 +1,5 @@
<% archived = current_page?(:action => :archived) %>

<table id="users" class="master"> <table id="users" class="master">
<thead> <thead>
<tr> <tr>
Expand All @@ -16,7 +18,7 @@


<form method="post" action="/users/0/archive"> <form method="post" action="/users/0/archive">
<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>" /> <input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>" />
<th><%= submit_tag "Archive" %></th> <th><%= submit_tag archived ? "Restore" : "Archive" %></th>


</tr> </tr>
</thead> </thead>
Expand Down Expand Up @@ -52,6 +54,4 @@


<span class="paginate"> <span class="paginate">
<%= will_paginate users %> <%= will_paginate users %>
</span> </span>

<%#= javascript_tag "$$('input:checked')" %>
14 changes: 12 additions & 2 deletions app/views/users/_user_types.html.erb
Expand Up @@ -8,8 +8,18 @@
<tbody> <tbody>
<% User.user_types.each do |type| %> <% User.user_types.each do |type| %>
<% key,value = type.shift %> <% key,value = type.shift %>
<% url = %W{/users #{value.try(:name).try(:tableize)}}.join('/') %> <% if Class == value.class %>
<tr class="<%= "#{cycle('odd', 'even')} #{value.nil? || !current_page?(:controller => url) ? '' : ' current'}" %>" <% url = %W{/users #{value.try(:name).try(:tableize)}}.join('/') %>
<% current = current_page?(:controller => url) %>
<% elsif !type[:name].nil? %>
<% url = "/users/#{type[:name]}" %>
<% current = true if current_page?(:action => type[:name]) %>
<% else %>
<% url = "/users" %>
<% current = current_page?(:controller => url) %>
<% end %>

<tr class="<%= "#{cycle('odd', 'even')} #{current ? ' current' : ''}" %>"
onclick="location.href='<%= url -%>'"> onclick="location.href='<%= url -%>'">
<%= content_tag :td, key %> <%= content_tag :td, key %>
<%= content_tag :td, value.nil? ? '' : button_to( <%= content_tag :td, value.nil? ? '' : button_to(
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -6,7 +6,7 @@


# Since we subclass the Users into different types, # Since we subclass the Users into different types,
# we need to build routes for them. # we need to build routes for them.
map.resources :users, :member => { :archive => :post } map.resources :users, :member => { :archive => :post }, :collection => { :archived => :get }
map.namespace :users do |u| map.namespace :users do |u|
u.resources :students, :name_prefix => nil u.resources :students, :name_prefix => nil
u.resources :teachers, :name_prefix => nil u.resources :teachers, :name_prefix => nil
Expand Down

0 comments on commit 1e297b5

Please sign in to comment.