aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/application.rb4
-rw-r--r--app/controllers/user_controller.rb8
-rw-r--r--app/helpers/link_to_helper.rb4
-rw-r--r--app/models/public_body.rb8
-rw-r--r--app/models/user.rb14
-rw-r--r--config/routes.rb4
-rw-r--r--db/migrate/038_add_more_url_names.rb17
-rw-r--r--db/schema.rb5
-rw-r--r--spec/controllers/user_controller_spec.rb12
-rw-r--r--spec/fixtures/users.yml2
-rw-r--r--todo.txt4
11 files changed, 58 insertions, 24 deletions
diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index f518a89e9..af3f8de1b 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -6,7 +6,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: application.rb,v 1.29 2008-02-27 12:04:10 francis Exp $
+# $Id: application.rb,v 1.30 2008-02-27 12:18:28 francis Exp $
class ApplicationController < ActionController::Base
@@ -44,7 +44,7 @@ class ApplicationController < ActionController::Base
def authenticated_as_user?(user, reason_params)
reason_params[:user_name] = user.name
- reason_params[:user_url] = show_user_url(:simple_name => MySociety::Format.simplify_url_part(user.name))
+ reason_params[:user_url] = show_user_url(:url_name => user.url_name)
if session[:user_id]
if session[:user_id] == user.id
# They are logged in as the right user
diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb
index 8dc056167..2c10db6f8 100644
--- a/app/controllers/user_controller.rb
+++ b/app/controllers/user_controller.rb
@@ -4,16 +4,16 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: user_controller.rb,v 1.27 2008-02-27 12:04:10 francis Exp $
+# $Id: user_controller.rb,v 1.28 2008-02-27 12:18:28 francis Exp $
class UserController < ApplicationController
# XXX See controllers/application.rb simplify_url_part for reverse of expression in SQL below
def show
- if MySociety::Format.simplify_url_part(params[:simple_name]) != params[:simple_name]
- redirect_to :simple_name => MySociety::Format.simplify_url_part(params[:simple_name])
+ if MySociety::Format.simplify_url_part(params[:url_name]) != params[:url_name]
+ redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name])
end
- @display_users = User.find(:all, :conditions => [ "regexp_replace(replace(lower(name), ' ', '-'), '[^a-z0-9_-]', '', 'g') = ?", params[:simple_name] ], :order => "created_at desc")
+ @display_users = User.find(:all, :conditions => [ "url_name = ?", params[:url_name] ], :order => "created_at desc")
end
# Login form
diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb
index dbf569f20..8d4a1baca 100644
--- a/app/helpers/link_to_helper.rb
+++ b/app/helpers/link_to_helper.rb
@@ -5,7 +5,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: link_to_helper.rb,v 1.17 2008-02-27 12:09:03 francis Exp $
+# $Id: link_to_helper.rb,v 1.18 2008-02-27 12:18:28 francis Exp $
module LinkToHelper
@@ -47,7 +47,7 @@ module LinkToHelper
# Users
def user_url(user)
- return show_user_url(:simple_name => MySociety::Format.simplify_url_part(user.name), :only_path => true)
+ return show_user_url(:url_name => user.url_name, :only_path => true)
end
def user_link(user)
link_to h(user.name), user_url(user)
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 9c4956b68..546de2c40 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -21,7 +21,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: public_body.rb,v 1.22 2008-02-27 12:04:10 francis Exp $
+# $Id: public_body.rb,v 1.23 2008-02-27 12:18:28 francis Exp $
class PublicBody < ActiveRecord::Base
validates_presence_of :name
@@ -50,14 +50,14 @@ class PublicBody < ActiveRecord::Base
# When name or short name is changed, also change the url name
def short_name=(short_name)
write_attribute(:short_name, short_name)
- update_url_name
+ self.update_url_name
end
def name=(name)
write_attribute(:name, name)
- update_url_name
+ self.update_url_name
end
def update_url_name
- url_name = MySociety::Format.simplify_url_part(short_or_long_name)
+ url_name = MySociety::Format.simplify_url_part(self.short_or_long_name)
write_attribute(:url_name, url_name)
end
# Return the short name if present, or else long name
diff --git a/app/models/user.rb b/app/models/user.rb
index ab680dc1e..79f26464d 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -19,7 +19,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: user.rb,v 1.29 2008-02-26 15:13:51 francis Exp $
+# $Id: user.rb,v 1.30 2008-02-27 12:18:28 francis Exp $
require 'digest/sha1'
@@ -28,6 +28,8 @@ class User < ActiveRecord::Base
validates_uniqueness_of :email, :case_sensitive => false, :message => "^There is already an account with that email address. You can sign in to it on the left."
validates_presence_of :name, :message => "^Please enter your name"
+ validates_presence_of :url_name
+
validates_presence_of :hashed_password, :message => "^Please enter a password"
has_many :info_requests
@@ -73,6 +75,16 @@ class User < ActiveRecord::Base
return self.find(:first, :conditions => [ 'email ilike ?', email ] ) # using ilike for case insensitive
end
+ # When name is changed, also change the url name
+ def name=(name)
+ write_attribute(:name, name)
+ self.update_url_name
+ end
+ def update_url_name
+ url_name = MySociety::Format.simplify_url_part(self.name)
+ write_attribute(:url_name, url_name)
+ end
+
# Virtual password attribute, which stores the hashed password, rather than plain text.
def password
@password
diff --git a/config/routes.rb b/config/routes.rb
index d60100f9d..cc5032f09 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -4,7 +4,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: routes.rb,v 1.38 2008-02-27 12:09:03 francis Exp $
+# $Id: routes.rb,v 1.39 2008-02-27 12:18:29 francis Exp $
ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation: first created -> highest priority.
@@ -33,7 +33,7 @@ ActionController::Routing::Routes.draw do |map|
user.signout '/signout', :action => 'signout'
user.signchange '/signchange', :action => 'signchange'
user.confirm '/c/:email_token', :action => 'confirm'
- user.show_user '/user/:simple_name', :action => 'show'
+ user.show_user '/user/:url_name', :action => 'show'
end
map.with_options :controller => 'body' do |body|
diff --git a/db/migrate/038_add_more_url_names.rb b/db/migrate/038_add_more_url_names.rb
new file mode 100644
index 000000000..d3dc2e669
--- /dev/null
+++ b/db/migrate/038_add_more_url_names.rb
@@ -0,0 +1,17 @@
+class AddMoreUrlNames < ActiveRecord::Migration
+ def self.up
+ add_column :users, :url_name, :text
+
+ User.find(:all).each do |user|
+ user.update_url_name
+ user.save!
+ end
+ add_index :users, :url_name
+ change_column :users, :url_name, :text, :null => false
+ end
+
+ def self.down
+ remove_column :users, :url_name
+ end
+end
+
diff --git a/db/schema.rb b/db/schema.rb
index 3dcab3c2f..08ecdcce6 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 => 37) do
+ActiveRecord::Schema.define(:version => 38) do
create_table "incoming_messages", :force => true do |t|
t.integer "info_request_id", :null => false
@@ -127,6 +127,9 @@ ActiveRecord::Schema.define(:version => 37) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "email_confirmed", :default => false, :null => false
+ t.text "url_name", :null => false
end
+ add_index "users", ["url_name"], :name => "index_users_on_url_name"
+
end
diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb
index ad8b4a6d4..331428cf8 100644
--- a/spec/controllers/user_controller_spec.rb
+++ b/spec/controllers/user_controller_spec.rb
@@ -5,27 +5,27 @@ describe UserController, "when showing a user" do
fixtures :users, :outgoing_messages, :incoming_messages, :info_requests, :info_request_events
it "should be successful" do
- get :show, :simple_name => "bob-smith"
+ get :show, :url_name => "bob-smith"
response.should be_success
end
it "should redirect to lower case name if given one with capital letters" do
- get :show, :simple_name => "Bob-Smith"
- response.should redirect_to(:controller => 'user', :action => 'show', :simple_name => "bob-smith")
+ get :show, :url_name => "Bob-Smith"
+ response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob-smith")
end
it "should render with 'show' template" do
- get :show, :simple_name => "bob-smith"
+ get :show, :url_name => "bob-smith"
response.should render_template('show')
end
it "should assign the user" do
- get :show, :simple_name => "bob-smith"
+ get :show, :url_name => "bob-smith"
assigns[:display_users].should == [ users(:bob_smith_user) ]
end
it "should assign the user for a more complex name" do
- get :show, :simple_name => "silly-emnameem"
+ get :show, :url_name => "silly-emnameem"
assigns[:display_users].should == [ users(:silly_name_user) ]
end
diff --git a/spec/fixtures/users.yml b/spec/fixtures/users.yml
index 092d76e45..9daa34bd0 100644
--- a/spec/fixtures/users.yml
+++ b/spec/fixtures/users.yml
@@ -1,6 +1,7 @@
bob_smith_user:
id: "1"
name: Bob Smith
+ url_name: bob-smith
email: bob@localhost
salt: "-6116981980.392287733335677"
hashed_password: 6b7cd45a5f35fd83febc0452a799530398bfb6e8 # jonespassword
@@ -10,6 +11,7 @@ bob_smith_user:
silly_name_user:
id: "2"
name: "Silly <em>Name</em>"
+ url_name: silly-emnameem
email: silly@localhost
salt: "-6116981980.392287733335677"
hashed_password: 6b7cd45a5f35fd83febc0452a799530398bfb6e8 # jonespassword
diff --git a/todo.txt b/todo.txt
index e46e12f3e..21595827c 100644
--- a/todo.txt
+++ b/todo.txt
@@ -1,10 +1,10 @@
-simple_short_name => url_name
-
user should have url_name too
and request!
fix up comments in Format.simplify_url_part re. other places
+check all simplify_url_part again
+
FOI requests to use to test it
==============================