Skip to content

Commit

Permalink
Replace email regex with EmailValidator gem
Browse files Browse the repository at this point in the history
* Use Rails 3 `validates` method.
* Use Ruby 1.9 hash syntax.
  • Loading branch information
Dan Croak committed Feb 27, 2013
1 parent 68d77d1 commit 5d5d5c0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
clearance (1.0.0.rc4)
bcrypt-ruby
email_validator
rails (>= 3.0)

GEM
Expand Down Expand Up @@ -69,6 +70,8 @@ GEM
nokogiri (>= 1.5.0)
database_cleaner (0.8.0)
diff-lcs (1.1.3)
email_validator (1.3.0)
activemodel
erubis (2.7.0)
factory_girl (3.5.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -155,7 +158,7 @@ GEM
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.34)
tzinfo (0.3.35)
xpath (0.1.4)
nokogiri (~> 1.3)

Expand Down
1 change: 1 addition & 0 deletions clearance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = Gem::Requirement.new('>= 1.9.2')

s.add_dependency 'bcrypt-ruby'
s.add_dependency 'email_validator'
s.add_dependency 'rails', '>= 3.0'
s.add_development_dependency 'appraisal', '0.4.1'
s.add_development_dependency 'aruba', '0.4.11'
Expand Down
3 changes: 3 additions & 0 deletions gemfiles/3.0.20.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
clearance (1.0.0.rc4)
bcrypt-ruby
email_validator
rails (>= 3.0)

GEM
Expand Down Expand Up @@ -68,6 +69,8 @@ GEM
nokogiri (>= 1.5.0)
database_cleaner (0.8.0)
diff-lcs (1.1.3)
email_validator (1.3.0)
activemodel
erubis (2.6.6)
abstract (>= 1.0.0)
factory_girl (3.5.0)
Expand Down
3 changes: 3 additions & 0 deletions gemfiles/3.1.11.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
clearance (1.0.0.rc4)
bcrypt-ruby
email_validator
rails (>= 3.0)

GEM
Expand Down Expand Up @@ -69,6 +70,8 @@ GEM
nokogiri (>= 1.5.0)
database_cleaner (0.8.0)
diff-lcs (1.1.3)
email_validator (1.3.0)
activemodel
erubis (2.7.0)
factory_girl (3.5.0)
activesupport (>= 3.0.0)
Expand Down
3 changes: 3 additions & 0 deletions gemfiles/3.2.12.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
clearance (1.0.0.rc4)
bcrypt-ruby
email_validator
rails (>= 3.0)

GEM
Expand Down Expand Up @@ -68,6 +69,8 @@ GEM
nokogiri (>= 1.5.0)
database_cleaner (0.8.0)
diff-lcs (1.1.3)
email_validator (1.3.0)
activemodel
erubis (2.7.0)
factory_girl (3.5.0)
activesupport (>= 3.0.0)
Expand Down
11 changes: 7 additions & 4 deletions lib/clearance/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'digest/sha1'
require 'email_validator'

module Clearance
module User
Expand Down Expand Up @@ -36,11 +37,13 @@ module Validations
extend ActiveSupport::Concern

included do
validates_presence_of :email, :unless => :email_optional?
validates_uniqueness_of :email, :allow_blank => true
validates_format_of :email, :with => %r{\A[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\Z}i, :allow_blank => true
validates :email,
email: true,
presence: true,
uniqueness: { allow_blank: true },
unless: :email_optional?

validates_presence_of :password, :unless => :password_optional?
validates :password, presence: true, unless: :password_optional?
end
end

Expand Down
12 changes: 9 additions & 3 deletions spec/models/password_strategies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

describe Clearance::User do
subject do
class UniquenessValidator < ActiveModel::Validator
def validate(record)
end
end

Class.new do
def self.validates_presence_of(*args); end
def self.validates_uniqueness_of(*args); end
def self.validates_format_of(*args); end
include ActiveModel::Validations

validates_with UniquenessValidator

def self.before_validation(*args); end
def self.before_create(*args); end

Expand Down

0 comments on commit 5d5d5c0

Please sign in to comment.