This patch requires that you do a migration.
Adds a new column to the users table for the user's last date and
time of login. Displays this value in the user list.
Signed-off-by: Darryl L. Pierce <mcpierce(a)gmail.com>
---
app/controllers/home_controller.rb | 7 ++++-
app/helpers/application_helper.rb | 10 ++++---
app/views/users/_list.html.erb | 3 ++
db/migrate/023_add_last_logged_in_date_to_users.rb | 26 ++++++++++++++++++++
db/schema.rb | 4 ++-
test/fixtures/users.yml | 7 +++++
test/functional/home_controller_test.rb | 22 ++++++++--------
7 files changed, 61 insertions(+), 18 deletions(-)
create mode 100644 db/migrate/023_add_last_logged_in_date_to_users.rb
diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb
index 15d708c..2f9085d 100644
--- a/app/controllers/home_controller.rb
+++ b/app/controllers/home_controller.rb
@@ -40,11 +40,9 @@ class HomeController < ApplicationController
# dashboard.
#
# On failure, the user is reshown the login page.
- #
def login
session[:user_id] = nil
- # if the user was doing a login, then process the values submitted
if request.post?
user = User.authenticate(params[:email], params[:password])
@@ -52,6 +50,11 @@ class HomeController < ApplicationController
session[:user_id] = user.id
uri = session[:original_uri]
session[:original_uri] = nil
+ user.last_logged_in = DateTime.now
+
+ User.transaction do
+ user.save
+ end
redirect_to(uri || dashboard_url)
else
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 9e81b48..47e76bb 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -37,10 +37,12 @@ module ApplicationHelper
# Displays a date or date and time.
def show_date(datetime, include_time = false)
- if include_time
- datetime.strftime("%m/%d/%Y %I:%M:%S %P")
- else
- datetime.strftime("%m/%d/%Y")
+ if datetime
+ if include_time
+ datetime.strftime("%m/%d/%Y %I:%M:%S %P")
+ else
+ datetime.strftime("%m/%d/%Y")
+ end
end
end
diff --git a/app/views/users/_list.html.erb b/app/views/users/_list.html.erb
index d4bb89f..c36e77e 100644
--- a/app/views/users/_list.html.erb
+++ b/app/views/users/_list.html.erb
@@ -7,6 +7,7 @@
<col class="number" />
<col class="number" />
<col class="date" />
+ <col class="date" />
<col class="actions" />
</colgroup>
@@ -18,6 +19,7 @@
<th>Email</th>
<th>Backlog</th>
<th>Roles</th>
+ <th>Last On</th>
<th>Since</th>
<th>Actions</th>
</tr>
@@ -34,6 +36,7 @@
<td><%= mail_to user.email, user.display_name %></td>
<td><%= link_to "#{user.backlog.size} items",
backlog_user_path(user) %>
<td><%= link_to "#{user.product_roles.size} roles",
roles_user_path(user) %></td>
+ <td><%= show_date(user.last_logged_in) %></td>
<td><%= show_date(user.created_at) %></td>
<td>
<%= link_to(image_tag("icons/view.png", :alt => "View
user..."), user_path(user)) %>
diff --git a/db/migrate/023_add_last_logged_in_date_to_users.rb
b/db/migrate/023_add_last_logged_in_date_to_users.rb
new file mode 100644
index 0000000..824f7f5
--- /dev/null
+++ b/db/migrate/023_add_last_logged_in_date_to_users.rb
@@ -0,0 +1,26 @@
+# 023_add_last_logged_in_date_to_users.rb
+# Copyright (C) 2008, Darryl L. Pierce <mcpierce(a)gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program. If not, see <
http://www.gnu.org/licenses/>.
+#
+
+class AddLastLoggedInDateToUsers < ActiveRecord::Migration
+ def self.up
+ add_column :users, :last_logged_in, :datetime, :null => false
+ end
+
+ def self.down
+ remove_column :users, :last_logged_in
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 6c86037..024b1cd 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 21) do
+ActiveRecord::Schema.define(:version => 23) do
create_table "backlog_items", :force => true do |t|
t.integer "sprint_id",
:null => false
@@ -31,6 +31,7 @@ ActiveRecord::Schema.define(:version => 21) do
t.boolean "daily_updates"
t.datetime "created_at"
t.datetime "updated_at"
+ t.boolean "updates_to_product_list", :default => false
end
create_table "product_roles", :force => true do |t|
@@ -173,6 +174,7 @@ ActiveRecord::Schema.define(:version => 21) do
t.text "introduction"
t.datetime "created_at"
t.datetime "updated_at"
+ t.datetime "last_logged_in", :null => false
end
add_index "users", ["email"], :name =>
"index_users_on_email", :unique => true
diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml
index 6a204c8..dfefb66 100644
--- a/test/fixtures/users.yml
+++ b/test/fixtures/users.yml
@@ -6,6 +6,7 @@ admin:
introduction: Can't you see I'm busy?
salt: <%= SALT %>
hashed_password: <%= User.encrypted_password('admin', SALT) %>
+ last_logged_in: <%= DateTime.now - 1 %>
projxp_owner:
email: admin(a)projxp.org
@@ -13,6 +14,7 @@ projxp_owner:
introduction: Wassup?
salt: <%= SALT %>
hashed_password: <%= User.encrypted_password('bonk', SALT) %>
+ last_logged_in: <%= DateTime.now - 1 %>
teatime_owner:
email: admin(a)teatime.org
@@ -20,6 +22,7 @@ teatime_owner:
introduction: I'd like a nice cuppa.
salt: <%= SALT %>
hashed_password: <%= User.encrypted_password('earlgrey', SALT) %>
+ last_logged_in: <%= DateTime.now - 1 %>
mcpierce:
email: mcpierce(a)gmail.com
@@ -27,6 +30,7 @@ mcpierce:
introduction: How are you?
salt: <%= SALT %>
hashed_password: <%= User.encrypted_password('farkle', SALT) %>
+ last_logged_in: <%= DateTime.now - 1 %>
jdonuts:
email: jdonuts(a)gmail.com
@@ -34,6 +38,7 @@ jdonuts:
introduction: How's it going, eh?
salt: <%= SALT %>
hashed_password: <%= User.encrypted_password('jelly', SALT) %>
+ last_logged_in: <%= DateTime.now - 1 %>
celliot:
email: celliot(a)gmail.com
@@ -41,6 +46,7 @@ celliot:
introduction: Get a life.
salt: <%= SALT %>
hashed_password: <%= User.encrypted_password('loser', SALT) %>
+ last_logged_in: <%= DateTime.now - 1 %>
unverified_user:
email: unverified(a)newuser.com
@@ -48,3 +54,4 @@ unverified_user:
introduction: Am I who I say I am?
salt: <%= SALT %>
hashed_password: <%= User.encrypted_password('unverified', SALT) %>
+ last_logged_in: <%= DateTime.now - 1 %>
diff --git a/test/functional/home_controller_test.rb
b/test/functional/home_controller_test.rb
index 94574b5..6edd68e 100644
--- a/test/functional/home_controller_test.rb
+++ b/test/functional/home_controller_test.rb
@@ -33,7 +33,6 @@ class HomeControllerTest < ActionController::TestCase
# Ensures that anonymous users are sent to the login page when they try to
# view the dashboard.
- #
def test_dashboard_for_anonymous_user
get :dashboard
@@ -42,7 +41,6 @@ class HomeControllerTest < ActionController::TestCase
end
# Ensures that authenticated users are shown their dashboard.
- #
def test_dashboard_for_authenticated_user
get :dashboard, nil, {:user_id => @authenticated_user.id}
@@ -52,30 +50,33 @@ class HomeControllerTest < ActionController::TestCase
end
# Ensures that a failed login redirects back to the login action.
- #
def test_login_failure
get :dashboard
- assert_redirected_to :action => :login
+ assert_redirected_to login_path
post :login, {:email => 'mcpierce(a)gmail.com',
:password => 'klefar' }
- assert_redirected_to :action => :login
+ assert_response :success
+ assert !session[:user_id], "User ID should not be in the session."
assert session[:original_uri], 'Lost the redirect URL'
end
# Ensures that a user who logs in is sent to their dashboard.
- #
- def test_login_failure
- post :login, {:email => 'mcpierce(a)gmail.com',
- :password => 'farkle'}
+ def test_login
+ old_last_logged_in = User.find_by_email('mcpierce(a)gmail.com').last_logged_in
+
+ post :login,
+ {:email => 'mcpierce@gmail.com',:password => 'farkle'}
+
+ puts "ERROR: #{flash[:error]}"
assert_redirected_to dashboard_url
+ assert User.find_by_email('mcpierce(a)gmail.com').last_logged_in >
old_last_logged_in
end
# Ensures that the user's session is destroyed on logout.
- #
def test_logout
get :logout, nil, {:user_id => @authenticated_user.id}
@@ -84,7 +85,6 @@ class HomeControllerTest < ActionController::TestCase
end
# Ensures that the main page doesn't require the user be logged.
- #
def test_index
get :index
--
1.6.0.2