validates_as_email_address 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ == master
2
+
3
+ == 0.1.0 / 2008-09-07
4
+
5
+ * Allow all options to be passed to format/length validations so that future options can be defined without changing this plugin
6
+ * Add support for overriding length limits
7
+ * Add support for requiring that domains follow the RFC 1035 format (on by default) [Kacper Bielecki]
8
+
9
+ == 0.0.3 / 2008-06-22
10
+
11
+ * Remove log files from gems
12
+
13
+ == 0.0.2 / 2008-05-05
14
+
15
+ * Update documentation
16
+
17
+ == 0.0.1 / 2007-09-26
18
+
19
+ * Add documentation
@@ -5,21 +5,21 @@ email addresses.
5
5
 
6
6
  == Resources
7
7
 
8
- Wiki
9
-
10
- * http://wiki.pluginaweek.org/Validates_as_email_address
11
-
12
8
  API
13
9
 
14
10
  * http://api.pluginaweek.org/validates_as_email_address
15
11
 
12
+ Bugs
13
+
14
+ * http://pluginaweek.lighthouseapp.com/projects/13293-validates_as_email_address
15
+
16
16
  Development
17
17
 
18
- * http://dev.pluginaweek.org/browser/trunk/validates_as_email_address
18
+ * http://github.com/pluginaweek/validates_as_email_address
19
19
 
20
20
  Source
21
21
 
22
- * http://svn.pluginaweek.org/trunk/validates_as_email_address
22
+ * git://github.com/pluginaweek/validates_as_email_address.git
23
23
 
24
24
  == Description
25
25
 
@@ -40,7 +40,7 @@ the email address.
40
40
  == Testing
41
41
 
42
42
  Before you can run any tests, the following gem must be installed:
43
- * plugin_test_helper[http://wiki.pluginaweek.org/Plugin_test_helper]
43
+ * plugin_test_helper[http://github.com/pluginaweek/plugin_test_helper]
44
44
 
45
45
  To run against a specific version of Rails:
46
46
 
data/Rakefile CHANGED
@@ -3,46 +3,54 @@ require 'rake/rdoctask'
3
3
  require 'rake/gempackagetask'
4
4
  require 'rake/contrib/sshpublisher'
5
5
 
6
- PKG_NAME = 'validates_as_email_address'
7
- PKG_VERSION = '0.0.3'
8
- PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
9
- RUBY_FORGE_PROJECT = 'pluginaweek'
10
-
11
- desc 'Default: run unit tests.'
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = 'validates_as_email_address'
8
+ s.version = '0.1.0'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = 'Adds support for validating the format/length of email addresses'
11
+
12
+ s.files = FileList['{lib,test}/**/*'] + %w(CHANGELOG.rdoc init.rb LICENSE Rakefile README.rdoc) - FileList['test/app_root/{log,log/*,script,script/*}']
13
+ s.require_path = 'lib'
14
+ s.has_rdoc = true
15
+ s.test_files = Dir['test/**/*_test.rb']
16
+
17
+ s.author = 'Aaron Pfeifer'
18
+ s.email = 'aaron@pluginaweek.org'
19
+ s.homepage = 'http://www.pluginaweek.org'
20
+ s.rubyforge_project = 'pluginaweek'
21
+ end
22
+
23
+ desc 'Default: run all tests.'
12
24
  task :default => :test
13
25
 
14
- desc 'Test the validates_as_email_address plugin.'
26
+ desc "Test the #{spec.name} plugin."
15
27
  Rake::TestTask.new(:test) do |t|
16
28
  t.libs << 'lib'
17
- t.pattern = 'test/**/*_test.rb'
29
+ t.test_files = spec.test_files
18
30
  t.verbose = true
19
31
  end
20
32
 
21
- desc 'Generate documentation for the validates_as_email_address plugin.'
33
+ begin
34
+ require 'rcov/rcovtask'
35
+ namespace :test do
36
+ desc "Test the #{spec.name} plugin with Rcov."
37
+ Rcov::RcovTask.new(:rcov) do |t|
38
+ t.libs << 'lib'
39
+ t.test_files = spec.test_files
40
+ t.rcov_opts << '--exclude="^(?!lib/)"'
41
+ t.verbose = true
42
+ end
43
+ end
44
+ rescue LoadError
45
+ end
46
+
47
+ desc "Generate documentation for the #{spec.name} plugin."
22
48
  Rake::RDocTask.new(:rdoc) do |rdoc|
23
49
  rdoc.rdoc_dir = 'rdoc'
24
- rdoc.title = 'ValidatesAsEmailAddress'
50
+ rdoc.title = spec.name
25
51
  rdoc.template = '../rdoc_template.rb'
26
52
  rdoc.options << '--line-numbers' << '--inline-source'
27
- rdoc.rdoc_files.include('README')
28
- rdoc.rdoc_files.include('lib/**/*.rb')
29
- end
30
-
31
- spec = Gem::Specification.new do |s|
32
- s.name = PKG_NAME
33
- s.version = PKG_VERSION
34
- s.platform = Gem::Platform::RUBY
35
- s.summary = 'Adds support for validating the format/length of email addresses'
36
-
37
- s.files = FileList['{lib,test}/**/*'].to_a - FileList['test/app_root/log/*'].to_a + %w(CHANGELOG init.rb LICENSE Rakefile README)
38
- s.require_path = 'lib'
39
- s.autorequire = 'validates_as_email_address'
40
- s.has_rdoc = true
41
- s.test_files = Dir['test/**/*_test.rb']
42
-
43
- s.author = 'Aaron Pfeifer'
44
- s.email = 'aaron@pluginaweek.org'
45
- s.homepage = 'http://www.pluginaweek.org'
53
+ rdoc.rdoc_files.include('README.rdoc', 'CHANGELOG.rdoc', 'LICENSE', 'lib/**/*.rb')
46
54
  end
47
55
 
48
56
  Rake::GemPackageTask.new(spec) do |p|
@@ -51,14 +59,14 @@ Rake::GemPackageTask.new(spec) do |p|
51
59
  p.need_zip = true
52
60
  end
53
61
 
54
- desc 'Publish the beta gem'
62
+ desc 'Publish the beta gem.'
55
63
  task :pgem => [:package] do
56
- Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
64
+ Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{spec.name}-#{spec.version}.gem").upload
57
65
  end
58
66
 
59
- desc 'Publish the API documentation'
67
+ desc 'Publish the API documentation.'
60
68
  task :pdoc => [:rdoc] do
61
- Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{PKG_NAME}", 'rdoc').upload
69
+ Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{spec.name}", 'rdoc').upload
62
70
  end
63
71
 
64
72
  desc 'Publish the API docs and gem'
@@ -71,10 +79,10 @@ task :release => [:gem, :package] do
71
79
  ruby_forge = RubyForge.new.configure
72
80
  ruby_forge.login
73
81
 
74
- %w( gem tgz zip ).each do |ext|
75
- file = "pkg/#{PKG_FILE_NAME}.#{ext}"
82
+ %w(gem tgz zip).each do |ext|
83
+ file = "pkg/#{spec.name}-#{spec.version}.#{ext}"
76
84
  puts "Releasing #{File.basename(file)}..."
77
85
 
78
- ruby_forge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, file)
86
+ ruby_forge.add_release(spec.rubyforge_project, spec.name, spec.version, file)
79
87
  end
80
88
  end
@@ -1,40 +1,18 @@
1
1
  require 'validates_as_email_address/rfc_822'
2
+ require 'validates_as_email_address/rfc_1035'
2
3
 
3
4
  module PluginAWeek #:nodoc:
4
5
  # Adds validations for email addresses
5
6
  module ValidatesAsEmailAddress
6
- # The options that can be used when validating the format of the email address
7
- EMAIL_FORMAT_OPTIONS = [
8
- :wrong_format,
9
- :allow_nil,
10
- :on,
11
- :if
12
- ]
13
-
14
- # The options that can be used when validating the length of the email address
15
- EMAIL_LENGTH_OPTIONS = [
16
- :minimum,
17
- :maximum,
18
- :is,
19
- :within,
20
- :in,
21
- :too_long,
22
- :too_short,
23
- :wrong_length,
24
- :allow_nil,
25
- :on,
26
- :if
27
- ]
28
-
29
7
  # Validates whether the value of the specific attribute matches against the
30
- # RFC822 specificiation.
8
+ # RFC822/RFC1035 specification.
31
9
  #
32
10
  # class Person < ActiveRecord::Base
33
11
  # validates_as_email_address :email, :on => :create
34
12
  # end
35
13
  #
36
14
  # This will also validate that the email address is within the specification
37
- # limits, specifically between 3 and 320 characters in length.
15
+ # limits, i.e. between 3 and 320 characters in length.
38
16
  #
39
17
  # Configuration options for length:
40
18
  # * +minimum+ - The minimum size of the attribute
@@ -49,28 +27,29 @@ module PluginAWeek #:nodoc:
49
27
  # Configuration options for format:
50
28
  # * +wrong_format+ - A custom error message (default is: "is an invalid email address")
51
29
  #
52
- # Configuration options for both length and format:
30
+ # Miscellaneous configuration options:
53
31
  # * +allow_nil+ - Attribute may be nil; skip validation.
54
32
  # * +on+ - Specifies when this validation is active (default is :save, other options :create, :update)
55
33
  # * +if+ - Specifies a method, proc or string to call to determine if the validation should
56
- # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
34
+ # occur (e.g. :if => :allow_validation, or :if => lambda { |user| user.signup_step > 2 }). The
57
35
  # method, proc or string should return or evaluate to a true or false value.
36
+ # * +strict+ - Specifies if the domain part of the email should be compliant to RFC 1035 (default is true). If set to false domains such as '-online.com', '[127.0.0.1]' become valid.
58
37
  def validates_as_email_address(*attr_names)
59
38
  configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
60
- configuration.assert_valid_keys(EMAIL_FORMAT_OPTIONS | EMAIL_LENGTH_OPTIONS)
61
39
  configuration.reverse_merge!(
62
- :wrong_format => ActiveRecord::Errors.default_error_messages[:invalid_email]
40
+ :wrong_format => ActiveRecord::Errors.default_error_messages[:invalid_email],
41
+ :strict => true
63
42
  )
64
43
 
65
44
  # Add format validation
66
- format_configuration = configuration.reject {|key, value| !EMAIL_FORMAT_OPTIONS.include?(key)}
67
- format_configuration[:message] = format_configuration.delete(:wrong_format)
68
- format_configuration[:with] = RFC822::EmailAddress
45
+ format_configuration = configuration.dup
46
+ format_configuration[:message] = configuration.delete(:wrong_format)
47
+ format_configuration[:with] = configuration[:strict] ? RFC1035::EmailAddress : RFC822::EmailAddress
69
48
  validates_format_of attr_names, format_configuration
70
49
 
71
50
  # Add length validation
72
- length_configuration = configuration.reject {|key, value| !EMAIL_LENGTH_OPTIONS.include?(key)}
73
- length_configuration.reverse_merge!(:within => 3..320)
51
+ length_configuration = configuration.dup
52
+ length_configuration.reverse_merge!(:within => 3..320) unless ([:minimum, :maximum, :is, :within, :in] & configuration.keys).any?
74
53
  validates_length_of attr_names, length_configuration
75
54
  end
76
55
  end
@@ -80,6 +59,7 @@ ActiveRecord::Base.class_eval do
80
59
  extend PluginAWeek::ValidatesAsEmailAddress
81
60
  end
82
61
 
62
+ # Add error messages specific to this validation
83
63
  ActiveRecord::Errors.default_error_messages.update(
84
64
  :invalid_email => 'is an invalid email address'
85
65
  )
@@ -0,0 +1,28 @@
1
+ module PluginAWeek #:nodoc:
2
+ module ValidatesAsEmailAddress
3
+ # The standard describing the format of domains
4
+ module RFC1035
5
+ # Matches domain according to the RFC 1035 standard
6
+ Domain = begin
7
+ digit = "[\\d]"
8
+ letter = "[\\x61-\\x7a\\x41-\\x5a]"
9
+ let_dig = "(?:#{letter}|#{digit})"
10
+ ldh_str = "(?:#{let_dig}|[\\x2d])+"
11
+ label = "#{let_dig}(?:(?:#{ldh_str})*#{let_dig})"
12
+ subdomain = "(?:#{label}\\.)*#{label}"
13
+ domain = "(?:#{subdomain}|\\x20)"
14
+
15
+ /#{domain}/
16
+ end
17
+
18
+ # Matches email addresses with domains that follow the RFC 1035 standard
19
+ EmailAddress = begin
20
+ local_part = RFC822::LocalPart.source
21
+ domain = Domain.source
22
+ addr_spec = "(#{local_part})\\x40(#{domain})"
23
+
24
+ /\A#{addr_spec}\z/
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,18 +1,37 @@
1
- # The standard describing the format of email addresses
2
- module RFC822
3
- EmailAddress = begin
4
- qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
5
- dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
6
- atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
7
- quoted_pair = '\\x5c[\\x00-\\x7f]'
8
- domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
9
- quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
10
- domain_ref = atom
11
- sub_domain = "(?:#{domain_ref}|#{domain_literal})"
12
- word = "(?:#{atom}|#{quoted_string})"
13
- domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
14
- local_part = "#{word}(?:\\x2e#{word})*"
15
- addr_spec = "(#{local_part})\\x40(#{domain})"
16
- pattern = /\A#{addr_spec}\z/
1
+ module PluginAWeek #:nodoc:
2
+ module ValidatesAsEmailAddress
3
+ # The standard describing the format of email addresses
4
+ module RFC822
5
+ # Matches the two parts of an email address (before/after @)
6
+ LocalPart, Domain = begin
7
+ # Shared
8
+ qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
9
+ dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
10
+ atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
11
+ quoted_pair = '\\x5c[\\x00-\\x7f]'
12
+ domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
13
+ quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
14
+
15
+ # Local part
16
+ word = "(?:#{atom}|#{quoted_string})"
17
+ local_part = "#{word}(?:\\x2e#{word})*"
18
+
19
+ # Domain
20
+ domain_ref = atom
21
+ sub_domain = "(?:#{domain_ref}|#{domain_literal})"
22
+ domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
23
+
24
+ [/#{local_part}/, /#{domain}/]
25
+ end
26
+
27
+ # Matches email addresses according to the RFC822 standard
28
+ EmailAddress = begin
29
+ local_part = LocalPart.source
30
+ domain = Domain.source
31
+ addr_spec = "(#{local_part})\\x40(#{domain})"
32
+
33
+ /\A#{addr_spec}\z/
34
+ end
35
+ end
17
36
  end
18
37
  end
@@ -1,3 +1,2 @@
1
1
  class User < ActiveRecord::Base
2
- validates_as_email_address :email
3
2
  end
data/test/factory.rb CHANGED
@@ -1,26 +1,32 @@
1
1
  module Factory
2
- # Build actions for the class
3
- def self.build(klass, &block)
4
- name = klass.to_s.underscore
5
- define_method("#{name}_attributes", block)
2
+ # Build actions for the model
3
+ def self.build(model, &block)
4
+ name = model.to_s.underscore
6
5
 
7
- module_eval <<-end_eval
8
- def valid_#{name}_attributes(attributes = {})
9
- #{name}_attributes(attributes)
10
- attributes
11
- end
12
-
13
- def new_#{name}(attributes = {})
14
- #{klass}.new(valid_#{name}_attributes(attributes))
15
- end
16
-
17
- def create_#{name}(*args)
18
- record = new_#{name}(*args)
19
- record.save!
20
- record.reload
21
- record
22
- end
23
- end_eval
6
+ define_method("#{name}_attributes", block)
7
+ define_method("valid_#{name}_attributes") {|*args| valid_attributes_for(model, *args)}
8
+ define_method("new_#{name}") {|*args| new_record(model, *args)}
9
+ define_method("create_#{name}") {|*args| create_record(model, *args)}
10
+ end
11
+
12
+ # Get valid attributes for the model
13
+ def valid_attributes_for(model, attributes = {})
14
+ name = model.to_s.underscore
15
+ send("#{name}_attributes", attributes)
16
+ attributes
17
+ end
18
+
19
+ # Build an unsaved record
20
+ def new_record(model, *args)
21
+ model.new(valid_attributes_for(model, *args))
22
+ end
23
+
24
+ # Build and save/reload a record
25
+ def create_record(model, *args)
26
+ record = new_record(model, *args)
27
+ record.save!
28
+ record.reload
29
+ record
24
30
  end
25
31
 
26
32
  build User do |attributes|
@@ -1,9 +1,20 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper'
2
2
 
3
- class ValidatesAsEmailAddressTest < Test::Unit::TestCase
4
- def test_should_be_valid_with_a_valid_set_of_attributes
5
- user = new_user
6
- assert user.valid?
3
+ class ValidatesAsEmailAddressByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ User.validates_as_email_address :email
6
+ end
7
+
8
+ def test_should_not_allow_email_addresses_shorter_than_3_characters
9
+ user = new_user(:email => 'a@')
10
+ assert !user.valid?
11
+ assert_equal 2, Array(user.errors.on(:email)).size
12
+ end
13
+
14
+ def test_should_not_allow_email_addresses_longer_than_320_characters
15
+ user = new_user(:email => 'a@' + 'a' * 315 + '.com')
16
+ assert !user.valid?
17
+ assert_equal 1, Array(user.errors.on(:email)).size
7
18
  end
8
19
 
9
20
  def test_should_allow_legal_rfc822_formats
@@ -11,9 +22,8 @@ class ValidatesAsEmailAddressTest < Test::Unit::TestCase
11
22
  'test@example',
12
23
  'test@example.com',
13
24
  'test@example.co.uk',
14
- '"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
15
- 'me@[187.223.45.119]',
16
- 'someone@123.com',
25
+ '"J. Smith\'s House, a.k.a. Home!"@example.com',
26
+ 'test@123.com',
17
27
  ].each do |address|
18
28
  user = new_user(:email => address)
19
29
  assert user.valid?, "#{address} should be legal."
@@ -22,9 +32,9 @@ class ValidatesAsEmailAddressTest < Test::Unit::TestCase
22
32
 
23
33
  def test_should_not_allow_illegal_rfc822_formats
24
34
  [
25
- 'Max@Job 3:14',
26
- 'Job@Book of Job',
27
- 'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
35
+ 'test@Monday 1:00',
36
+ 'test@Monday the first',
37
+ 'J. Smith\'s House, a.k.a. Home!@example.com',
28
38
  ].each do |address|
29
39
  user = new_user(:email => address)
30
40
  assert !user.valid?, "#{address} should be illegal."
@@ -32,15 +42,193 @@ class ValidatesAsEmailAddressTest < Test::Unit::TestCase
32
42
  end
33
43
  end
34
44
 
35
- def test_not_allow_emails_longer_than_320_characters
36
- user = new_user(:email => 'a' * 313 + '@a.com')
45
+ def test_should_not_allow_illegal_rfc1035_formats
46
+ [
47
+ 'test@[127.0.0.1]',
48
+ 'test@-domain_not_starting_with_letter.com',
49
+ 'test@domain_not_ending_with_alphanum-.com'
50
+ ].each do |address|
51
+ user = new_user(:email => address)
52
+ assert !user.valid?, "#{address} should be illegal."
53
+ end
54
+ end
55
+
56
+ def teardown
57
+ User.class_eval do
58
+ @validate_callbacks = ActiveSupport::Callbacks::CallbackChain.new
59
+ @validate_on_create_callbacks = ActiveSupport::Callbacks::CallbackChain.new
60
+ @validate_on_update_callbacks = ActiveSupport::Callbacks::CallbackChain.new
61
+ end
62
+ end
63
+ end
64
+
65
+ class ValidatesAsEmailAddressTest < Test::Unit::TestCase
66
+ def test_should_allow_minimum_length
67
+ User.validates_as_email_address :email, :minimum => 8
68
+
69
+ user = new_user(:email => 'a@aa.com')
70
+ assert user.valid?
71
+
72
+ user = new_user(:email => 'a@a.com')
73
+ assert !user.valid?
74
+ end
75
+
76
+ def test_should_not_check_maximum_length_if_minimum_length_defined
77
+ User.validates_as_email_address :email, :minimum => 10
78
+
79
+ user = new_user(:email => 'a@' + 'a' * 315 + '.com')
37
80
  assert user.valid?
81
+ end
82
+
83
+ def test_should_allow_maximum_length
84
+ User.validates_as_email_address :email, :maximum => 8
38
85
 
39
- user.email = 'a' + user.email
86
+ user = new_user(:email => 'a@aa.com')
40
87
  assert user.valid?
41
88
 
42
- user.email = 'a' + user.email
89
+ user = new_user(:email => 'a@aaa.com')
90
+ assert !user.valid?
91
+ end
92
+
93
+ def test_should_not_check_minimum_length_if_maximum_length_defined
94
+ User.validates_as_email_address :email, :maximum => 8
95
+
96
+ user = new_user(:email => 'a@a')
43
97
  assert !user.valid?
44
98
  assert_equal 1, Array(user.errors.on(:email)).size
45
99
  end
100
+
101
+ def test_should_allow_exact_length
102
+ User.validates_as_email_address :email, :is => 8
103
+
104
+ user = new_user(:email => 'a@aa.com')
105
+ assert user.valid?
106
+
107
+ user = new_user(:email => 'a@a.com')
108
+ assert !user.valid?
109
+
110
+ user = new_user(:email => 'a@aaa.com')
111
+ assert !user.valid?
112
+ end
113
+
114
+ def test_should_allow_within_range
115
+ User.validates_as_email_address :email, :within => 8..10
116
+
117
+ user = new_user(:email => 'a@a.com')
118
+ assert !user.valid?
119
+
120
+ user = new_user(:email => 'a@aa.com')
121
+ assert user.valid?
122
+
123
+ user = new_user(:email => 'a@aaaa.com')
124
+ assert user.valid?
125
+
126
+ user = new_user(:email => 'a@aaaaa.com')
127
+ assert !user.valid?
128
+ end
129
+
130
+ def test_should_allow_in_range
131
+ User.validates_as_email_address :email, :in => 8..10
132
+
133
+ user = new_user(:email => 'a@a.com')
134
+ assert !user.valid?
135
+
136
+ user = new_user(:email => 'a@aa.com')
137
+ assert user.valid?
138
+
139
+ user = new_user(:email => 'a@aaaa.com')
140
+ assert user.valid?
141
+
142
+ user = new_user(:email => 'a@aaaaa.com')
143
+ assert !user.valid?
144
+ end
145
+
146
+ def test_should_allow_too_long_message
147
+ User.validates_as_email_address :email, :too_long => 'custom'
148
+
149
+ user = new_user(:email => 'a@' + 'a' * 315 + '.com')
150
+ user.valid?
151
+
152
+ assert_equal 'custom', Array(user.errors.on(:email)).last
153
+ end
154
+
155
+ def test_should_allow_too_short_message
156
+ User.validates_as_email_address :email, :too_short => 'custom'
157
+
158
+ user = new_user(:email => 'a@')
159
+ user.valid?
160
+
161
+ assert_equal 'custom', Array(user.errors.on(:email)).last
162
+ end
163
+
164
+ def test_should_allow_wrong_length_message
165
+ User.validates_as_email_address :email, :is => 8, :wrong_length => 'custom'
166
+
167
+ user = new_user(:email => 'a@a.com')
168
+ user.valid?
169
+
170
+ assert_equal 'custom', Array(user.errors.on(:email)).last
171
+ end
172
+
173
+ def test_should_allow_wrong_format_message
174
+ User.validates_as_email_address :email, :is => 8, :wrong_format => 'custom'
175
+
176
+ user = new_user(:email => 'a@a')
177
+ user.valid?
178
+
179
+ assert_equal 'custom', Array(user.errors.on(:email)).first
180
+ end
181
+
182
+ def test_should_validate_if_if_is_true
183
+ User.validates_as_email_address :email, :if => lambda {true}
184
+
185
+ user = new_user(:email => 'a')
186
+ assert !user.valid?
187
+ end
188
+
189
+ def test_should_not_validate_if_if_is_false
190
+ User.validates_as_email_address :email, :if => lambda {false}
191
+
192
+ user = new_user(:email => 'a')
193
+ assert user.valid?
194
+ end
195
+
196
+ def test_should_validate_if_unless_is_false
197
+ User.validates_as_email_address :email, :unless => lambda {false}
198
+
199
+ user = new_user(:email => 'a')
200
+ assert !user.valid?
201
+ end
202
+
203
+ def test_should_not_validate_if_unless_is_true
204
+ User.validates_as_email_address :email, :unless => lambda {true}
205
+
206
+ user = new_user(:email => 'a')
207
+ assert user.valid?
208
+ end
209
+
210
+ def teardown
211
+ User.class_eval do
212
+ @validate_callbacks = ActiveSupport::Callbacks::CallbackChain.new
213
+ @validate_on_create_callbacks = ActiveSupport::Callbacks::CallbackChain.new
214
+ @validate_on_update_callbacks = ActiveSupport::Callbacks::CallbackChain.new
215
+ end
216
+ end
217
+ end
218
+
219
+ class ValidatesAsEmailAddressUnrestrictedTest < Test::Unit::TestCase
220
+ def setup
221
+ User.validates_as_email_address :email, :strict => false
222
+ end
223
+
224
+ def test_should_allow_illegal_rfc1035_formats
225
+ [
226
+ 'test@[127.0.0.1]',
227
+ 'test@-domain_not_starting_with_letter.com',
228
+ 'test@domain_not_ending_with_alphanum-.com'
229
+ ].each do |address|
230
+ user = new_user(:email => address)
231
+ assert user.valid?, "#{address} should be legal."
232
+ end
233
+ end
46
234
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_as_email_address
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
8
- autorequire: validates_as_email_address
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-22 00:00:00 -04:00
12
+ date: 2008-09-12 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,7 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - lib/validates_as_email_address
26
+ - lib/validates_as_email_address/rfc_1035.rb
26
27
  - lib/validates_as_email_address/rfc_822.rb
27
28
  - lib/validates_as_email_address.rb
28
29
  - test/app_root
@@ -32,16 +33,15 @@ files:
32
33
  - test/app_root/db
33
34
  - test/app_root/db/migrate
34
35
  - test/app_root/db/migrate/001_create_users.rb
35
- - test/app_root/log
36
36
  - test/test_helper.rb
37
37
  - test/factory.rb
38
38
  - test/unit
39
39
  - test/unit/validates_as_email_address_test.rb
40
- - CHANGELOG
40
+ - CHANGELOG.rdoc
41
41
  - init.rb
42
42
  - LICENSE
43
43
  - Rakefile
44
- - README
44
+ - README.rdoc
45
45
  has_rdoc: true
46
46
  homepage: http://www.pluginaweek.org
47
47
  post_install_message:
@@ -63,8 +63,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  version:
64
64
  requirements: []
65
65
 
66
- rubyforge_project:
67
- rubygems_version: 1.1.1
66
+ rubyforge_project: pluginaweek
67
+ rubygems_version: 1.2.0
68
68
  signing_key:
69
69
  specification_version: 2
70
70
  summary: Adds support for validating the format/length of email addresses
data/CHANGELOG DELETED
@@ -1,13 +0,0 @@
1
- *SVN*
2
-
3
- *0.0.3* (June 22nd, 2008)
4
-
5
- * Remove log files from gems
6
-
7
- *0.0.2* (May 5th, 2008)
8
-
9
- * Update documentation
10
-
11
- *0.0.1* (September 26th, 2007)
12
-
13
- * Add documentation