Add first_name, last_name, and display_name to the User and make first and last names editable via the preferences interface. The display_name attribute is used in the upper right-hand corner of the standard layout and falls back to use the login if neither first_name of last_name are set.

This is the beginning of Tony Shadwick's request http://www.rousette.org.uk/projects/forums/viewthread/70/



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@319 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-09-16 16:01:29 +00:00
parent ba3757f29e
commit 6fa8439c4a
9 changed files with 72 additions and 9 deletions

View file

@ -69,6 +69,8 @@ class UserController < ApplicationController
"admin_email" => "#{params['prefs']['admin_email']}",
"refresh" => "#{params['prefs']['refresh']}"
}
@user.first_name = params['user']['first_name']
@user.last_name = params['user']['last_name']
if @user.save
redirect_to :action => 'preferences'
else

View file

@ -18,7 +18,22 @@ class User < ActiveRecord::Base
def self.find_admin
find_first([ "is_admin = ?", true ])
end
def self.get_salt
SALT
end
def display_name
if first_name.blank? && last_name.blank?
return login
elsif first_name.blank?
return last_name
elsif last_name.blank?
return first_name
end
"#{first_name} #{last_name}"
end
def change_password(pass,pass_confirm)
self.password = pass
self.password_confirmation = pass_confirm
@ -27,10 +42,6 @@ class User < ActiveRecord::Base
def crypt_word
write_attribute("word", self.class.sha1(login + Time.now.to_i.to_s + rand.to_s))
end
def self.get_salt
SALT
end
protected

View file

@ -38,7 +38,7 @@
<% if @user.is_admin? -%>
<%= link_to "Add users", :controller => "login", :action => "signup" %>&nbsp;|&nbsp;
<% end -%>
<%= link_to "Logout (#{@user.login}) »", :controller => "login", :action=>"logout"%>
<%= link_to "Logout (#{@user.display_name}) »", :controller => "login", :action=>"logout"%>
</div>
<div id="navcontainer">

View file

@ -2,6 +2,7 @@
<h2>Help on preferences</h2>
<p>The preference settings should mostly be self-explanatory, but some hints are included below: </p>
<ul>
<li><strong>first name and last name:</strong> Used for display purposes if set</li>
<li><strong>staleness_starts:</strong> the number of days before items with no due date get marked as stale (with a yellow highlight)</li>
<li><strong>date_format:</strong> the format in which you'd like dates to be shown. For example, for the date 31st January 2006, %d/%m/%Y will show 31/01/2006, %b-%e-%y will show Jan-31-06. See the <a href="http://uk2.php.net/strftime" title="PHP strftime manual">strftime manual</a> for more formatting options for the date.</li>
<li><strong>no_completed:</strong> number of completed actions to show on the home page. If you set this to zero, the completed actions box will not be shown on the home page or on the individual context or project pages. You can still see all your completed items by clicking the 'Done' link in the navigation bar at the top of each page.</li>
@ -17,6 +18,14 @@
<div id="input_box" class="container context">
<%= start_form_tag :action => 'update_preferences' %>
<table>
<tr>
<td><label>first name:</label></td>
<td><%= text_field 'user', 'first_name' %></td>
</tr>
<tr>
<td><label>last name:</label></td>
<td><%= text_field 'user', 'last_name' %></td>
</tr>
<% @prefs.each do |k,v| %>
<% next if !@user.is_admin? and k == "admin_email" %>
<tr>

View file

@ -9,6 +9,8 @@
<% end %>
<ul id="prefs">
<li>First name: <span class="highlight"><%= @user.first_name %></span></li>
<li>Last name: <span class="highlight"><%= @user.last_name %></span></li>
<li>Date format: <span class="highlight"><%= @prefs["date_format"] %></span></li>
<li>Week starts on: <span class="highlight">
<% case @prefs["week_starts"]

View file

@ -0,0 +1,11 @@
class AddFirstAndLastNameToUser < ActiveRecord::Migration
def self.up
add_column :users, :first_name, :string
add_column :users, :last_name, :string
end
def self.down
remove_column :users, :first_name
remove_column :users, :last_name
end
end

View file

@ -2,7 +2,7 @@
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.
ActiveRecord::Schema.define(:version => 9) do
ActiveRecord::Schema.define(:version => 10) do
create_table "contexts", :force => true do |t|
t.column "name", :string, :default => "", :null => false
@ -55,6 +55,8 @@ ActiveRecord::Schema.define(:version => 9) do
t.column "word", :string
t.column "is_admin", :integer, :limit => 4, :default => 0, :null => false
t.column "preferences", :text
t.column "first_name", :string
t.column "last_name", :string
end
end

View file

@ -64,9 +64,11 @@ class UserControllerTest < Test::Unit::TestCase
#
def test_update_preferences
@request.session['user_id'] = users(:admin_user).id # log in the admin user
users(:admin_user).preferences = post :update_preferences, :prefs => { :date_format => "%m-%d-%Y", :week_starts => "0", :no_completed => "10", :staleness_starts => "14", :due_style => "1", :admin_email => "my.email@domain.com" }
@prefs = users(:admin_user).preferences
assert_not_nil @prefs
post :update_preferences, {:user => { :first_name => 'Jane', :last_name => 'Doe'}, :prefs => { :date_format => "%m-%d-%Y", :week_starts => "0", :no_completed => "10", :staleness_starts => "14", :due_style => "1", :admin_email => "my.email@domain.com" }}
updated_admin_user = User.find(users(:admin_user).id)
assert_not_nil updated_admin_user.preferences
assert_equal 'Jane', updated_admin_user.first_name
assert_equal 'Doe', updated_admin_user.last_name
assert_redirected_to :action => 'preferences'
end

View file

@ -110,5 +110,29 @@ class UserTest < Test::Unit::TestCase
assert_equal 2, @other_user.errors.count
assert_equal ["is too short (minimum is 3 characters)", "can't be blank"], @other_user.errors.on(:login)
end
def test_display_name_with_first_and_last_name_set
@other_user.first_name = "Jane"
@other_user.last_name = "Doe"
assert_equal "Jane Doe", @other_user.display_name
end
def test_display_name_with_first_name_set
@other_user.first_name = "Jane"
@other_user.last_name = nil
assert_equal "Jane", @other_user.display_name
end
def test_display_name_with_last_name_set
@other_user.first_name = nil
@other_user.last_name = "Doe"
assert_equal "Doe", @other_user.display_name
end
def test_display_name_with_neither_first_nor_last_name_set
@other_user.first_name = nil
@other_user.last_name = nil
assert_equal @other_user.login, @other_user.display_name
end
end