donncha-validates_as_email 0.6.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 +33 -0
- data/LICENSE +5 -0
- data/README +54 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/validates_as_email.rb +74 -0
- data/test/validates_as_email_test.rb +72 -0
- metadata +72 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
0.6.0 - Jul 24th 2009
|
2
|
+
* Added :restrict_domain option to exclude single-part domains, e.g: user@example
|
3
|
+
* Added workaround for 'ActiveRecord::Base::Validations (NameError)' bug when running tests
|
4
|
+
|
5
|
+
0.6.0 - Jul 24th 2009
|
6
|
+
* Compatibility with Ruby 1.9
|
7
|
+
* Support for I18n
|
8
|
+
|
9
|
+
0.5.1 - Aug 15th 2008
|
10
|
+
* Repackaged original plugin by Ximon Eighteen for GitHub
|
11
|
+
* Gem'ified plugin
|
12
|
+
|
13
|
+
0.1.4 - May 26th 2006
|
14
|
+
* Thijs van der Vossen fixed a plugin load issue in the test suite and
|
15
|
+
provided another test to illustrate that test@example is considered
|
16
|
+
valid by RFC822, something most people wouldn't realise.
|
17
|
+
|
18
|
+
0.1.3 - May 9th 2006
|
19
|
+
* Thijs van der Vossen contributed some test cases.
|
20
|
+
|
21
|
+
0.1.2 - April 24th 2006
|
22
|
+
* Dan Kubb and Tim Fletcher have updated the RFC822 regular expression
|
23
|
+
on Tims website so I've put the latest version into this plugin. The
|
24
|
+
changes prevent matching of email addresses containing line breaks.
|
25
|
+
|
26
|
+
0.1.1 - April 23rd 2006
|
27
|
+
* Huge code cleanup & simplification by Dan Kubb.
|
28
|
+
* Credit Tim Fletcher with the Ruby version of the RFC822 regular
|
29
|
+
expression.
|
30
|
+
|
31
|
+
0.1.0 - April 4th 2006
|
32
|
+
* First release, advertised on the Rails plugins page:
|
33
|
+
http://wiki.rubyonrails.org/rails/pages/Plugins
|
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
ValidatesAsEmail
|
2
|
+
================
|
3
|
+
|
4
|
+
This gem/plugin is a re-packaged and gem'ified version of the original plugin with credit as follows:
|
5
|
+
|
6
|
+
Donncha Redmond <dredmond@e-xact.com>
|
7
|
+
Michal Zima <xhire@mujmalysvet.cz>
|
8
|
+
Ximon Eighteen <ximon.eighteen@int.greenpeace.org>
|
9
|
+
Dan Kubb <dan.kubb@autopilotmarketing.com>
|
10
|
+
Thijs van der Vossen <thijs@fngtps.com>
|
11
|
+
|
12
|
+
This Ruby on Rails plugin implements an ActiveRecord validation helper called
|
13
|
+
validates_as_email. The helper acts as if validates_format_of was used with a
|
14
|
+
regular expression that defines an RFC822 email address conformance test.
|
15
|
+
|
16
|
+
The plugin implements the regular expression here:
|
17
|
+
|
18
|
+
http://tfletcher.com/lib/rfc822.rb
|
19
|
+
|
20
|
+
Which is an implementation in Ruby of a regular expression published by Cal
|
21
|
+
Henderson for PHP here:
|
22
|
+
|
23
|
+
http://www.iamcal.com/publish/articles/php/parsing_email
|
24
|
+
|
25
|
+
Installation:
|
26
|
+
=============
|
27
|
+
gem sources -a http://gems.github.com
|
28
|
+
|
29
|
+
Install the gem(s):
|
30
|
+
sudo gem install donncha-validates_as_email
|
31
|
+
|
32
|
+
Add to environment.rb initializer block:
|
33
|
+
config.gem 'donncha-validates_as_email', :lib => 'validates_as_email', :source => 'http://gems.github.com'
|
34
|
+
|
35
|
+
Usage:
|
36
|
+
======
|
37
|
+
In your model file do something like:
|
38
|
+
|
39
|
+
class MyClass < ActiveRecord::Base
|
40
|
+
validates_as_email :email, :message => 'Invalid Email Address', :allow_nil => true
|
41
|
+
end
|
42
|
+
|
43
|
+
class MyOtherClass < ActiveRecord::Base
|
44
|
+
# will not accept addresses with a single part domain, e.g user@example
|
45
|
+
validates_as_email :email, :message => 'Invalid Email Address', :allow_nil => true, :restrict_domain => true
|
46
|
+
end
|
47
|
+
|
48
|
+
Tests:
|
49
|
+
======
|
50
|
+
Some tests have been added.
|
51
|
+
|
52
|
+
License:
|
53
|
+
========
|
54
|
+
See the LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the validates_as_email plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the validates_as_email plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ValidatesAsEmail'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_as_email'
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#
|
4
|
+
# RFC822 Email Address Regex
|
5
|
+
# --------------------------
|
6
|
+
#
|
7
|
+
# Originally written by Cal Henderson
|
8
|
+
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
|
9
|
+
#
|
10
|
+
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
|
11
|
+
#
|
12
|
+
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
|
13
|
+
# http://creativecommons.org/licenses/by-sa/2.5/
|
14
|
+
#
|
15
|
+
module RFC822
|
16
|
+
module Patterns
|
17
|
+
def self.compile(string)
|
18
|
+
Regexp.new string, nil, 'n'
|
19
|
+
end
|
20
|
+
|
21
|
+
QTEXT = compile "[^\\x0d\\x22\\x5c\\x80-\\xff]"
|
22
|
+
DTEXT = compile "[^\\x0d\\x5b-\\x5d\\x80-\\xff]"
|
23
|
+
|
24
|
+
ATOM_CORE = compile "[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+"
|
25
|
+
ATOM_EDGE = compile "[^\\x00-\\x20\\x22\\x28\\x29\\x2c-\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]"
|
26
|
+
ATOM = compile "(?:#{ATOM_EDGE}{1,2}|#{ATOM_EDGE}#{ATOM_CORE}#{ATOM_EDGE})"
|
27
|
+
|
28
|
+
QPAIR = compile "\\x5c[\\x00-\\x7f]"
|
29
|
+
QSTRING = compile "\\x22(?:#{QTEXT}|#{QPAIR})*\\x22"
|
30
|
+
|
31
|
+
WORD = compile "(?:#{ATOM}|#{QSTRING})"
|
32
|
+
|
33
|
+
DOMAIN_PT = compile "(?:[a-zA-Z0-9][\-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z0-9]+)"
|
34
|
+
|
35
|
+
DOMAIN = compile "#{DOMAIN_PT}(?:\\x2e#{DOMAIN_PT})*"
|
36
|
+
LOCAL_PT = compile "#{WORD}(?:\\x2e#{WORD})*"
|
37
|
+
|
38
|
+
ADDRESS = compile "#{LOCAL_PT}\\x40#{DOMAIN}"
|
39
|
+
NON_LOCAL_ADDRESS = compile "#{LOCAL_PT}\\x40#{DOMAIN_PT}(\\x2e#{DOMAIN_PT})+"
|
40
|
+
end
|
41
|
+
EmailAddress = /\A#{Patterns::ADDRESS}\z/
|
42
|
+
NonLocalEmailAddress = /\A#{Patterns::NON_LOCAL_ADDRESS}\z/
|
43
|
+
end
|
44
|
+
|
45
|
+
# Validation helper for ActiveRecord derived objects that cleanly and simply
|
46
|
+
# allows the model to check if the given string is a syntactically valid email
|
47
|
+
# address (by using the RFC822 module above).
|
48
|
+
#
|
49
|
+
# Original code by Ximon Eighteen <ximon.eightee@int.greenpeace.org> which was
|
50
|
+
# heavily based on code I can no longer find on the net, my apologies to the
|
51
|
+
# author!
|
52
|
+
#
|
53
|
+
# Huge credit goes to Dan Kubb <dan.kubb@autopilotmarketing.com> for
|
54
|
+
# submitting a patch to massively simplify this code and thereby instruct me
|
55
|
+
# in the ways of Rails too! I reflowed the patch a little to keep the line
|
56
|
+
# length to a maximum of 78 characters, an old habit.
|
57
|
+
|
58
|
+
module ActiveRecord
|
59
|
+
module Validations
|
60
|
+
module ClassMethods
|
61
|
+
def validates_as_email(*attr_names)
|
62
|
+
options = attr_names.pop if attr_names.last.is_a?(Hash)
|
63
|
+
restrict_domain = options.delete(:restrict_domain) if options
|
64
|
+
configuration = {
|
65
|
+
:message => (I18n.translate(:'activerecord.errors.messages.invalid_email', :raise => true) rescue 'is an invalid email'),
|
66
|
+
:with => restrict_domain ? RFC822::NonLocalEmailAddress : RFC822::EmailAddress,
|
67
|
+
:allow_nil => false }
|
68
|
+
configuration.update(options) if options
|
69
|
+
|
70
|
+
validates_format_of attr_names, configuration
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'validates_as_email'
|
7
|
+
rescue LoadError
|
8
|
+
require 'rubygems'
|
9
|
+
require 'active_record'
|
10
|
+
# BUG: https://rails.lighthouseapp.com/projects/8994/tickets/2577-when-using-activerecordassociations-outside-of-rails-a-nameerror-is-thrown
|
11
|
+
ActiveRecord::ActiveRecordError
|
12
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_email'
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestRecord < ActiveRecord::Base
|
16
|
+
def self.columns; []; end
|
17
|
+
attr_accessor :email
|
18
|
+
validates_as_email :email
|
19
|
+
end
|
20
|
+
class LocalTestRecord < ActiveRecord::Base
|
21
|
+
def self.columns; []; end
|
22
|
+
attr_accessor :email
|
23
|
+
validates_as_email :email, :restrict_domain => true
|
24
|
+
end
|
25
|
+
|
26
|
+
class ValidatesAsEmailTest < Test::Unit::TestCase
|
27
|
+
def test_illegal_rfc822_email_address
|
28
|
+
addresses = [
|
29
|
+
'Max@Job 3:14',
|
30
|
+
'Job@Book of Job',
|
31
|
+
'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
|
32
|
+
]
|
33
|
+
addresses.each do |address|
|
34
|
+
assert !TestRecord.new(:email => address).valid?, "#{address} should be illegal."
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_legal_rfc822_email_address
|
39
|
+
addresses = [
|
40
|
+
'test@example',
|
41
|
+
'test@example.com',
|
42
|
+
'test@example.co.uk',
|
43
|
+
'"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
|
44
|
+
'me@[187.223.45.119]',
|
45
|
+
'someone@123.com',
|
46
|
+
]
|
47
|
+
addresses.each do |address|
|
48
|
+
assert TestRecord.new(:email => address).valid?, "#{address} should be legal."
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# insist on a domain of at least two parts, and no IP addresses
|
53
|
+
def test_restricted_domains
|
54
|
+
addresses = [
|
55
|
+
'test@example.com',
|
56
|
+
'test@example.co.uk',
|
57
|
+
'"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
|
58
|
+
'someone@123.com',
|
59
|
+
]
|
60
|
+
addresses.each do |address|
|
61
|
+
assert LocalTestRecord.new(:email => address).valid?, "#{address} should be legal."
|
62
|
+
end
|
63
|
+
|
64
|
+
addresses = [
|
65
|
+
'test@example',
|
66
|
+
'me@[187.223.45.119]',
|
67
|
+
]
|
68
|
+
addresses.each do |address|
|
69
|
+
assert !LocalTestRecord.new(:email => address).valid?, "non-traditional domain #{address} should not be legal."
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: donncha-validates_as_email
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 6
|
8
|
+
- 1
|
9
|
+
version: 0.6.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michal Zima
|
13
|
+
- Ximon Eighteen
|
14
|
+
- Dan Kubb
|
15
|
+
- Thijs van der Vossen
|
16
|
+
- Donncha Redmond
|
17
|
+
autorequire:
|
18
|
+
bindir: bin
|
19
|
+
cert_chain: []
|
20
|
+
|
21
|
+
date: 2010-03-08 00:00:00 +11:00
|
22
|
+
default_executable:
|
23
|
+
dependencies: []
|
24
|
+
|
25
|
+
description: Rails gem/plugin that implements an ActiveRecord validation helper called validates_as_email which validates email address (RFC822)
|
26
|
+
email: dredmond@e-xact.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- CHANGELOG
|
35
|
+
- LICENSE
|
36
|
+
- README
|
37
|
+
- Rakefile
|
38
|
+
- init.rb
|
39
|
+
- lib/validates_as_email.rb
|
40
|
+
- test/validates_as_email_test.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/donncha/validates_as_email
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.6
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Rails gem/plugin to validate format of email addresses (RFC822)
|
71
|
+
test_files:
|
72
|
+
- test/validates_as_email_test.rb
|