Commit f487494
Changed files (5)
app
controllers
models
spec
models
app/controllers/registrations_controller.rb
@@ -4,13 +4,13 @@ class RegistrationsController < ApplicationController
end
def create
- user = User.create!(secure_params)
- if params[:user][:username].blank?
- flash[:error] = 'blah'
- render :new
- else
+ user = User.new(secure_params)
+ if user.save
log_in(user)
redirect_to dashboard_path
+ else
+ flash[:error] = user.errors.full_messages
+ render :new
end
end
app/models/user.rb
@@ -1,3 +1,7 @@
class User < ActiveRecord::Base
attr_accessor :terms_and_conditions, :password
+ USERNAME_REGEX=/\A[-a-z0-9_.]*\z/i
+
+ validates :username, presence: true, format: { with: USERNAME_REGEX }, uniqueness: true
+ validates :email, presence: true, email: true, uniqueness: true
end
spec/models/user_spec.rb
@@ -11,4 +11,65 @@ describe User do
expect(saved_user.password).to be_nil
end
end
+
+ describe "validations" do
+ context "username" do
+
+ it 'is invalid when the username is missing' do
+ user = User.new(username: nil)
+ expect(user).to_not be_valid
+ expect(user.errors[:username]).to_not be_empty
+ end
+
+ it 'is invalid if invalid characters are in the username' do
+ user = User.new(username: '$()')
+ expect(user).to_not be_valid
+ expect(user.errors[:username]).to_not be_empty
+ end
+
+ it 'is invalid if the username is already taken' do
+ User.create(username: 'blah', email: 'blah@example.com')
+ second_user = User.create(username: 'blah', email: 'blahblah@example.com')
+
+ expect(second_user.errors[:username]).to_not be_empty
+ end
+ end
+
+ describe "#email" do
+ it 'is invalid when the email is missing' do
+ user = User.new(email: nil)
+ expect(user).to_not be_valid
+ expect(user.errors[:email]).to_not be_empty
+ end
+
+ it 'is invalid when the email is not in the correct format' do
+ user = User.new(email: 'blah')
+ expect(user).to_not be_valid
+ expect(user.errors[:email]).to_not be_empty
+ end
+
+ it 'is invalid if the email address is already registered' do
+ User.create(username: 'blahblah', email: 'blah@example.com')
+ second_user = User.create(username: 'blah', email: 'blah@example.com')
+
+ expect(second_user.errors[:email]).to_not be_empty
+ end
+ end
+ end
+
+ describe "USERNAME_REGEX" do
+ it 'does not match' do
+ expect(User::USERNAME_REGEX).to_not match("$username")
+ expect(User::USERNAME_REGEX).to_not match("!username")
+ expect(User::USERNAME_REGEX).to_not match("@username")
+ end
+
+ it "matches" do
+ expect(User::USERNAME_REGEX).to match("username")
+ expect(User::USERNAME_REGEX).to match("user.name")
+ expect(User::USERNAME_REGEX).to match("user_name")
+ expect(User::USERNAME_REGEX).to match("user-name")
+ expect(User::USERNAME_REGEX).to match("username1")
+ end
+ end
end
Gemfile
@@ -23,6 +23,7 @@ gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'foundation-rails'
+gem 'email_validator'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
Gemfile.lock
@@ -56,6 +56,8 @@ GEM
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
dotenv (1.0.2)
+ email_validator (1.5.0)
+ activemodel
erubis (2.7.0)
execjs (2.2.2)
foreman (0.76.0)
@@ -176,6 +178,7 @@ PLATFORMS
DEPENDENCIES
byebug
coffee-rails (~> 4.1.0)
+ email_validator
foreman
foundation-rails
jbuilder (~> 2.0)