Skip to content

Commit 17977cd

Browse files
committed
Adding uniqueness validation for user name
Fixes #116
1 parent c7d261b commit 17977cd

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class User
1717
:message => "Make your username from letters, numbers, underscores('_'), and dots('.')."
1818
validates_length_of :username, :in => (1..32),
1919
:message => "Your username needs at least 1 character but no more than 32."
20+
validates_uniqueness_of :username,
21+
:message => "User name has been taken already. Please try another"
2022

2123
def to_param
2224
self.username

config/initializers/simple_form.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Components used by the form builder to generate a complete input. You can remove
44
# any of them, change the order, or even add your own components to the stack.
55
# config.components = [ :placeholder, :label_input, :hint, :error ]
6-
config.components = [ :placeholder, :label_input, :hint ]
6+
config.components = [ :placeholder, :label_input, :hint, :error ]
77

88
# Default tag used on hints.
99
# config.hint_tag = :span

features/signup.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ Feature: Sign up for an account
44

55
Scenario: Create an account via the signup form
66
When I register a new account
7-
Then I should be logged in with my new account
7+
Then I should be logged in with my new account
8+
9+
Scenario: Try to sign up using existing user name
10+
When I register a duplicate account
11+
Then I should see validation errors
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
When /^I register a new account$/ do
2-
@user_info = {:username => "username", :password => "password", :email => "test@example.com"}
3-
1+
def register_user(user)
42
visit new_user_registration_path
5-
6-
fill_in("Username", :with => @user_info[:username])
7-
fill_in("Email", :with => @user_info[:email])
8-
fill_in("Password", :with => @user_info[:password])
9-
fill_in("Password confirmation", :with => @user_info[:password])
3+
fill_in("Username", :with => user[:username])
4+
fill_in("Email", :with => user[:email])
5+
fill_in("Password", :with => user[:password])
6+
fill_in("Password confirmation", :with => user[:password])
107

118
click_button "Sign up"
129
end
1310

11+
When /^I register a new account$/ do
12+
@new_user = {:username => "username", :password => "password", :email => "test@example.com"}
13+
register_user @new_user
14+
end
15+
16+
When /^I register a duplicate account$/ do
17+
@existing_user = User.create!(username: "existing_user",
18+
email: "existing_user@example.com",
19+
password: "foobar",
20+
password_confirmation: "foobar")
21+
register_user @existing_user
22+
end
23+
1424
When /^I should be logged in with my new account$/ do
1525
page.should have_content("You have signed up successfully")
16-
page.should have_content(@user_info[:username])
17-
end
26+
page.should have_content(@new_user[:username])
27+
end
28+
29+
Then /^I should see validation errors$/ do
30+
page.should have_selector(".error_notification")
31+
end

0 commit comments

Comments
 (0)