validates_as_email_address 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = 'validates_as_email_address'
|
8
|
-
s.version = '0.2.
|
8
|
+
s.version = '0.2.1'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.summary = 'Adds support for validating the format/length of email addresses'
|
11
11
|
|
@@ -14,25 +14,35 @@ module ValidatesAsEmailAddress
|
|
14
14
|
# limits, i.e. between 3 and 320 characters in length.
|
15
15
|
#
|
16
16
|
# Configuration options for length:
|
17
|
-
# *
|
18
|
-
# *
|
19
|
-
# *
|
20
|
-
# *
|
21
|
-
#
|
22
|
-
# *
|
23
|
-
# *
|
24
|
-
#
|
17
|
+
# * <tt>:minimum</tt> - The minimum size of the attribute
|
18
|
+
# * <tt>:maximum</tt> - The maximum size of the attribute
|
19
|
+
# * <tt>:is</tt> - The exact size of the attribute
|
20
|
+
# * <tt>:within</tt> - A range specifying the minimum and maximum size of the
|
21
|
+
# attribute
|
22
|
+
# * <tt>:in</tt> - A synonym (or alias) for <tt>:within</tt>
|
23
|
+
# * <tt>:too_long</tt> - The error message if the attribute goes over the
|
24
|
+
# maximum (default is: "is too long (maximum is %d characters)")
|
25
|
+
# * <tt>:too_short</tt> - The error message if the attribute goes under the
|
26
|
+
# minimum (default is: "is too short (minimum is %d characters)")
|
27
|
+
# * <tt>:wrong_length</tt> - The error message if using the <tt>:is</tt>
|
28
|
+
# method and the attribute is the wrong size (default is:
|
29
|
+
# "is the wrong length (should be %d characters)")
|
25
30
|
#
|
26
31
|
# Configuration options for format:
|
27
|
-
# *
|
32
|
+
# * <tt>:wrong_format</tt> - A custom error message (default is:
|
33
|
+
# "is an invalid email address")
|
28
34
|
#
|
29
35
|
# Miscellaneous configuration options:
|
30
|
-
# *
|
31
|
-
# *
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
+
# * <tt>:allow_nil</tt> - Attribute may be nil; skip validation.
|
37
|
+
# * <tt>:on</tt> - Specifies when this validation is active (default is
|
38
|
+
# :save, other options :create, :update)
|
39
|
+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
|
40
|
+
# the validation should occur (e.g. :if => :allow_validation, or
|
41
|
+
# :if => lambda { |user| user.signup_step > 2 }). The method, proc or
|
42
|
+
# string should return or evaluate to a true or false value.
|
43
|
+
# * <tt>:strict</tt> - Specifies if the domain part of the email should be
|
44
|
+
# compliant to RFC 1035 (default is true). If set to false domains such as
|
45
|
+
# '-online.com', '[127.0.0.1]' become valid.
|
36
46
|
def validates_as_email_address(*attr_names)
|
37
47
|
configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
|
38
48
|
configuration.reverse_merge!(
|
@@ -4,8 +4,8 @@ module ValidatesAsEmailAddress
|
|
4
4
|
# Matches domain according to the RFC 1035 standard, with the exception
|
5
5
|
# that domains can start with a letter *or* digit
|
6
6
|
Domain = begin
|
7
|
-
digit =
|
8
|
-
letter =
|
7
|
+
digit = '[\\d]'
|
8
|
+
letter = '[\\x61-\\x7a\\x41-\\x5a]'
|
9
9
|
let_dig = "(?:#{letter}|#{digit})"
|
10
10
|
let_dig_hyp = "(?:#{let_dig}|[\\x2d])"
|
11
11
|
label = "#{let_dig}(?:#{let_dig_hyp}*#{let_dig})?"
|
@@ -20,6 +20,7 @@ module ValidatesAsEmailAddress
|
|
20
20
|
local_part = RFC822::LocalPart.source
|
21
21
|
domain = Domain.source
|
22
22
|
addr_spec = "(#{local_part})\\x40(#{domain})"
|
23
|
+
addr_spec.force_encoding('binary') if addr_spec.respond_to?(:force_encoding)
|
23
24
|
|
24
25
|
/\A#{addr_spec}\z/
|
25
26
|
end
|
@@ -14,11 +14,13 @@ module ValidatesAsEmailAddress
|
|
14
14
|
# Local part
|
15
15
|
word = "(?:#{atom}|#{quoted_string})"
|
16
16
|
local_part = "#{word}(?:\\x2e#{word})*"
|
17
|
+
local_part.force_encoding('binary') if local_part.respond_to?(:force_encoding)
|
17
18
|
|
18
19
|
# Domain
|
19
20
|
domain_ref = atom
|
20
21
|
sub_domain = "(?:#{domain_ref}|#{domain_literal})"
|
21
22
|
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
|
23
|
+
domain.force_encoding('binary') if domain.respond_to?(:force_encoding)
|
22
24
|
|
23
25
|
[/#{local_part}/, /#{domain}/]
|
24
26
|
end
|
@@ -28,6 +30,7 @@ module ValidatesAsEmailAddress
|
|
28
30
|
local_part = LocalPart.source
|
29
31
|
domain = Domain.source
|
30
32
|
addr_spec = "(#{local_part})\\x40(#{domain})"
|
33
|
+
addr_spec.force_encoding('binary') if addr_spec.respond_to?(:force_encoding)
|
31
34
|
|
32
35
|
/\A#{addr_spec}\z/
|
33
36
|
end
|
@@ -186,28 +186,28 @@ class ValidatesAsEmailAddressTest < Test::Unit::TestCase
|
|
186
186
|
end
|
187
187
|
|
188
188
|
def test_should_validate_if_if_is_true
|
189
|
-
User.validates_as_email_address :email, :if => lambda {true}
|
189
|
+
User.validates_as_email_address :email, :if => lambda {|user| true}
|
190
190
|
|
191
191
|
user = new_user(:email => 'a')
|
192
192
|
assert !user.valid?
|
193
193
|
end
|
194
194
|
|
195
195
|
def test_should_not_validate_if_if_is_false
|
196
|
-
User.validates_as_email_address :email, :if => lambda {false}
|
196
|
+
User.validates_as_email_address :email, :if => lambda {|user| false}
|
197
197
|
|
198
198
|
user = new_user(:email => 'a')
|
199
199
|
assert user.valid?
|
200
200
|
end
|
201
201
|
|
202
202
|
def test_should_validate_if_unless_is_false
|
203
|
-
User.validates_as_email_address :email, :unless => lambda {false}
|
203
|
+
User.validates_as_email_address :email, :unless => lambda {|user| false}
|
204
204
|
|
205
205
|
user = new_user(:email => 'a')
|
206
206
|
assert !user.valid?
|
207
207
|
end
|
208
208
|
|
209
209
|
def test_should_not_validate_if_unless_is_true
|
210
|
-
User.validates_as_email_address :email, :unless => lambda {true}
|
210
|
+
User.validates_as_email_address :email, :unless => lambda {|user| true}
|
211
211
|
|
212
212
|
user = new_user(:email => 'a')
|
213
213
|
assert user.valid?
|
@@ -237,4 +237,12 @@ class ValidatesAsEmailAddressUnrestrictedTest < Test::Unit::TestCase
|
|
237
237
|
assert user.valid?, "#{address} should be legal."
|
238
238
|
end
|
239
239
|
end
|
240
|
+
|
241
|
+
def teardown
|
242
|
+
User.class_eval do
|
243
|
+
@validate_callbacks = ActiveSupport::Callbacks::CallbackChain.new
|
244
|
+
@validate_on_create_callbacks = ActiveSupport::Callbacks::CallbackChain.new
|
245
|
+
@validate_on_update_callbacks = ActiveSupport::Callbacks::CallbackChain.new
|
246
|
+
end
|
247
|
+
end
|
240
248
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_as_email_address
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-01-11 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|