Permalink
Browse files

Add Unique Username

  • Loading branch information...
1 parent 307d6a9 commit a3d1885897ada8dbc7dce1560a81259672211971 @shannoncancello shannoncancello committed Mar 29, 2013
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -139,6 +139,13 @@ aside {
margin-bottom: 3px;
margin-top: 0px;
}
+ p1 {
+ font-size: 1.2em;
+ text-align: left;
+ letter-spacing: -1px;
+ margin-bottom: 2px;
+ margin-top: 1px;
+ }
}
}
View
@@ -10,7 +10,7 @@
#
class User < ActiveRecord::Base
- attr_accessible :name, :email, :password, :password_confirmation
+ attr_accessible :name, :username, :email, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
@@ -24,6 +24,9 @@ class User < ActiveRecord::Base
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
+ validates :username, presence: true, length: { maximum: 30 },
+ uniqueness: true
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
@@ -1,6 +1,6 @@
<li>
<%= gravatar_for user, size: 52 %>
- <%= link_to user.name, user %>
+ <%= link_to user.username, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
@@ -9,6 +9,9 @@
<%= f.label :name %>
<%= f.text_field :name %>
+ <%= f.label :username %>
+ <%= f.text_field :username %>
+
<%= f.label :email %>
<%= f.text_field :email %>
@@ -9,6 +9,9 @@
<%= f.label :name %>
<%= f.text_field :name %>
+ <%= f.label :username %>
+ <%= f.text_field :username %>
+
<%= f.label :email %>
<%= f.text_field :email %>
@@ -4,8 +4,11 @@
<section>
<h1>
<%= gravatar_for @user %>
- <%= @user.name %>
+ <%= @user.username %>
</h1>
+ <p1>
+ <%= @user.name %>
+ </p1>
</section>
<section>
<%= render 'shared/stats' %>
@@ -0,0 +1,5 @@
+class AddUsernameToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :username, :string
+ end
+end
@@ -0,0 +1,5 @@
+class AddIndexToUsersUsername < ActiveRecord::Migration
+ def change
+ add_index :users, :username, 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 => 20130324202615) do
+ActiveRecord::Schema.define(:version => 20130329011721) do
create_table "microposts", :force => true do |t|
t.string "content"
@@ -41,9 +41,11 @@
t.string "password_digest"
t.string "remember_token"
t.boolean "admin", :default => false
+ t.string "username"
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", ["username"], :name => "index_users_on_username", :unique => true
end
@@ -9,15 +9,18 @@ end
def make_users
admin = User.create!(name: "Example User",
+ username: "Admin",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
+ username = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(name: name,
+ username: username,
email: email,
password: password,
password_confirmation: password)
View
@@ -1,7 +1,8 @@
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
- sequence(:email) { |n| "person_#{n}@example.com"}
+ sequence(:email) { |n| "person_#{n}@example.com"}
+ sequence(:username) { |n| "#{n}" }
password "foobar"
password_confirmation "foobar"
@@ -14,13 +14,14 @@
describe User do
before do
- @user = User.new(name: "Example User", email: "user@example.com",
+ @user = User.new(name: "Example User", username: "Coolio", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
+ it { should respond_to(:username) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
@@ -66,6 +67,25 @@
it { should_not be_valid }
end
+ describe "when user name is too long" do
+ before { @user.username = "a" * 31 }
+ it { should_not be_valid }
+ end
+
+ describe "when user name is not present" do
+ before { @user.username = " " }
+ it { should_not be_valid }
+ end
+
+ describe "when user name is already taken" do
+ before do
+ user_with_same_username = @user.dup
+ user_with_same_username.save
+ end
+
+ it { should_not be_valid }
+ end
+
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
@@ -23,7 +23,7 @@
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
- it { should have_selector('title', text: user.name) }
+ it { should have_selector('title', text: user.username) }
it { should have_link('Users', href: users_path) }
it { should have_link('Profile', href: user_path(user)) }
@@ -25,7 +25,7 @@
it "should list each user" do
User.paginate(page: 1).each do |user|
- page.should have_selector('li', text: user.name)
+ page.should have_selector('li', text: user.username)
end
end
end
@@ -57,8 +57,8 @@
before { visit user_path(user) }
- it { should have_selector('h1', text: user.name) }
- it { should have_selector('title', text: user.name) }
+ it { should have_selector('h1', text: user.username) }
+ it { should have_selector('title', text: user.username) }
describe "microposts" do
it { should have_content(m1.content) }
@@ -145,6 +145,7 @@
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
+ fill_in "Username", with: "Coolio"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
@@ -186,9 +187,11 @@
describe "with valid information" do
let(:new_name) { "New Name" }
+ let(:new_username) { "NewCoo" }
let(:new_email) { "new@example.com" }
before do
fill_in "Name", with: new_name
+ fill_in "Username", with: new_username
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
@@ -199,6 +202,7 @@
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { user.reload.name.should == new_name }
+ specify { user.reload.username.should == new_username }
specify { user.reload.email.should == new_email }
end
end
@@ -216,7 +220,7 @@
it { should have_selector('title', text: full_title('Following')) }
it { should have_selector('h3', text: 'Following') }
- it { should have_link(other_user.name, href: user_path(other_user)) }
+ it { should have_link(other_user.username, href: user_path(other_user)) }
end
describe "followers" do
@@ -227,7 +231,7 @@
it { should have_selector('title', text: full_title('Followers')) }
it { should have_selector('h3', text: 'Followers') }
- it { should have_link(user.name, href: user_path(user)) }
+ it { should have_link(user.username, href: user_path(user)) }
end
end
end

0 comments on commit a3d1885

Please sign in to comment.