Permalink
Browse files

Ticket #523: Added Friendly URLs for Users

Continue changes on #523.
Now for Users. Performed changes:
 * Added a migration for "slug" column
 * Changed model User to support FruendlyId
  • Loading branch information...
1 parent 4de8856 commit bfdeb7a465bc2ffc21d8d1cf545afc91bd4018e8 @MrHant committed Mar 10, 2012
Showing with 14 additions and 6 deletions.
  1. +5 −5 app/models/user.rb
  2. +6 −0 db/migrate/20120310160046_add_slug_to_users.rb
  3. +3 −1 db/schema.rb
View
@@ -1,6 +1,11 @@
class User < ActiveRecord::Base
require 'digest/sha2'
+ if User.table_exists? && User.column_names.include?("slug")
+ extend FriendlyId
+ friendly_id :username, :use => :slugged
+ end
+
make_voter
mount_uploader :avatar, ::AvatarUploader
@@ -92,11 +97,6 @@ def self.search(search, page)
:order => 'username'
end
- def to_param # make custom parameter generator for seo urls, to use: pass actual object(not id) into id ie: :id => object
- # this is also backwards compatible with id lookups, since .to_i gets only contiguous numbers, ie: "4-some-string-here".to_i # => 4
- "#{id}-#{username.parameterize}"
- end
-
def to_s
self.username
end
@@ -0,0 +1,6 @@
+class AddSlugToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :slug, :string
+ add_index :users, :slug, :unique => true
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120310123336) do
+ActiveRecord::Schema.define(:version => 20120310160046) do
create_table "authentications", :force => true do |t|
t.integer "user_id"
@@ -443,11 +443,13 @@
t.string "current_login_ip"
t.string "salt"
t.string "avatar"
+ t.string "slug"
end
add_index "users", ["email"], :name => "index_users_on_email"
add_index "users", ["last_request_at"], :name => "index_users_on_last_request_at"
add_index "users", ["persistence_token"], :name => "index_users_on_persistence_token"
+ add_index "users", ["slug"], :name => "index_users_on_slug", :unique => true
add_index "users", ["username"], :name => "index_users_on_username"
create_table "votings", :force => true do |t|

0 comments on commit bfdeb7a

Please sign in to comment.