From 8f3e967c6a691fc9cd6c4b8bc18aa4a3ad1d1d05 Mon Sep 17 00:00:00 2001 From: Dan Croak Date: Sat, 23 Feb 2013 19:57:47 -0800 Subject: [PATCH] Replace email regex with EmailValidator gem * Use Rails 3 `validates` method. * Use Ruby 1.9 hash syntax. --- Gemfile.lock | 5 ++++- clearance.gemspec | 1 + gemfiles/3.0.20.gemfile.lock | 3 +++ gemfiles/3.1.11.gemfile.lock | 3 +++ gemfiles/3.2.12.gemfile.lock | 3 +++ lib/clearance/user.rb | 11 +++++++---- spec/models/password_strategies_spec.rb | 12 +++++++++--- 7 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index e06893e1c..f70e6d5ba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: clearance (1.0.0.rc4) bcrypt-ruby + email_validator rails (>= 3.0) GEM @@ -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) @@ -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) diff --git a/clearance.gemspec b/clearance.gemspec index 27cb62913..a133897ce 100644 --- a/clearance.gemspec +++ b/clearance.gemspec @@ -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' diff --git a/gemfiles/3.0.20.gemfile.lock b/gemfiles/3.0.20.gemfile.lock index b3130c5e4..2c90e7e86 100644 --- a/gemfiles/3.0.20.gemfile.lock +++ b/gemfiles/3.0.20.gemfile.lock @@ -3,6 +3,7 @@ PATH specs: clearance (1.0.0.rc4) bcrypt-ruby + email_validator rails (>= 3.0) GEM @@ -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) diff --git a/gemfiles/3.1.11.gemfile.lock b/gemfiles/3.1.11.gemfile.lock index ae0cd154b..0e22ddc5e 100644 --- a/gemfiles/3.1.11.gemfile.lock +++ b/gemfiles/3.1.11.gemfile.lock @@ -3,6 +3,7 @@ PATH specs: clearance (1.0.0.rc4) bcrypt-ruby + email_validator rails (>= 3.0) GEM @@ -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) diff --git a/gemfiles/3.2.12.gemfile.lock b/gemfiles/3.2.12.gemfile.lock index ed8af9e88..13c5a2de4 100644 --- a/gemfiles/3.2.12.gemfile.lock +++ b/gemfiles/3.2.12.gemfile.lock @@ -3,6 +3,7 @@ PATH specs: clearance (1.0.0.rc4) bcrypt-ruby + email_validator rails (>= 3.0) GEM @@ -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) diff --git a/lib/clearance/user.rb b/lib/clearance/user.rb index 40a743c00..c98b77c70 100644 --- a/lib/clearance/user.rb +++ b/lib/clearance/user.rb @@ -1,4 +1,5 @@ require 'digest/sha1' +require 'email_validator' module Clearance module User @@ -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 diff --git a/spec/models/password_strategies_spec.rb b/spec/models/password_strategies_spec.rb index 144bb0235..ad95fa615 100644 --- a/spec/models/password_strategies_spec.rb +++ b/spec/models/password_strategies_spec.rb @@ -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