Permalink
Browse files

Prettified User URLS

  • Loading branch information...
kirbydoss committed Mar 21, 2012
1 parent d969ff1 commit 808cf015b13882f6deda7e41a8a13c5e04feb38b
Showing with 20 additions and 1 deletion.
  1. +1 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +8 −0 app/models/user.rb
  4. +6 −0 db/migrate/20120320233543_add_slug_to_users.rb
  5. +3 −1 db/schema.rb
View
@@ -6,6 +6,7 @@ gem 'bcrypt-ruby', '3.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.5'
gem 'possessive', '1.0.0'
+gem 'friendly_id', '4.0.3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
View
@@ -74,6 +74,7 @@ GEM
faker (1.0.1)
i18n (~> 0.4)
ffi (1.0.11)
+ friendly_id (4.0.3)
gherkin (2.9.1)
json (>= 1.4.6)
growl (1.0.3)
@@ -186,6 +187,7 @@ DEPENDENCIES
database_cleaner (= 0.7.0)
factory_girl_rails (= 1.4.0)
faker (= 1.0.1)
+ friendly_id (= 4.0.3)
growl (= 1.0.3)
guard-rspec (= 0.5.5)
guard-spork (= 0.3.2)
View
@@ -10,9 +10,13 @@
#
class User < ActiveRecord::Base
+
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
+ extend FriendlyId
+ friendly_id :name, use: :slugged
+
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
@@ -34,6 +38,10 @@ class User < ActiveRecord::Base
validates :password, length: {minimum: 6}
validates :password_confirmation, presence: true
+ def should_generate_new_friendly_id?
+ new_record?
+ end
+
def feed
Micropost.from_users_followed_by(self)
end
@@ -0,0 +1,6 @@
+class AddSlugToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :slug, :string
+ add_index :users, :slug
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120320022958) do
+ActiveRecord::Schema.define(:version => 20120320233543) do
create_table "microposts", :force => true do |t|
t.text "content"
@@ -41,9 +41,11 @@
t.string "password_digest"
t.string "remember_token"
t.boolean "admin", :default => false
+ t.string "slug"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
+ add_index "users", ["slug"], :name => "index_users_on_slug"
end

0 comments on commit 808cf01

Please sign in to comment.