brodock-validates_as_email 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +34 -0
- data/LICENSE +5 -0
- data/README +48 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/validates_as_email.rb +43 -0
- data/test/validates_as_email_test.rb +43 -0
- metadata +73 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
0.7.0 - April 24th 2010
|
2
|
+
* Updated to support Sexy Validations on Rails 3
|
3
|
+
* Requires Rails 3
|
4
|
+
* Removed RFC stuffs
|
5
|
+
|
6
|
+
0.6.0 - Jul 24th 2009
|
7
|
+
* Compatibility with Ruby 1.9
|
8
|
+
* Support for I18n
|
9
|
+
|
10
|
+
0.5.1 - Aug 15th 2008
|
11
|
+
* Repackaged original plugin by Ximon Eighteen for GitHub
|
12
|
+
* Gem'ified plugin
|
13
|
+
|
14
|
+
0.1.4 - May 26th 2006
|
15
|
+
* Thijs van der Vossen fixed a plugin load issue in the test suite and
|
16
|
+
provided another test to illustrate that test@example is considered
|
17
|
+
valid by RFC822, something most people wouldn't realise.
|
18
|
+
|
19
|
+
0.1.3 - May 9th 2006
|
20
|
+
* Thijs van der Vossen contributed some test cases.
|
21
|
+
|
22
|
+
0.1.2 - April 24th 2006
|
23
|
+
* Dan Kubb and Tim Fletcher have updated the RFC822 regular expression
|
24
|
+
on Tims website so I've put the latest version into this plugin. The
|
25
|
+
changes prevent matching of email addresses containing line breaks.
|
26
|
+
|
27
|
+
0.1.1 - April 23rd 2006
|
28
|
+
* Huge code cleanup & simplification by Dan Kubb.
|
29
|
+
* Credit Tim Fletcher with the Ruby version of the RFC822 regular
|
30
|
+
expression.
|
31
|
+
|
32
|
+
0.1.0 - April 4th 2006
|
33
|
+
* First release, advertised on the Rails plugins page:
|
34
|
+
http://wiki.rubyonrails.org/rails/pages/Plugins
|
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,48 @@
|
|
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
|
+
Gabriel Mazetto <brodock@gmail.com>
|
7
|
+
Donncha Redmond <dredmond@e-xact.com>
|
8
|
+
Michal Zima <xhire@mujmalysvet.cz>
|
9
|
+
Ximon Eighteen <ximon.eighteen@int.greenpeace.org>
|
10
|
+
Dan Kubb <dan.kubb@autopilotmarketing.com>
|
11
|
+
Thijs van der Vossen <thijs@fngtps.com>
|
12
|
+
|
13
|
+
This Ruby on Rails plugin implements an ActiveRecord validation helper called
|
14
|
+
validates_as_email. The helper acts as if validates_format_of was used with a
|
15
|
+
regular expression that defines an email address conformance test.
|
16
|
+
|
17
|
+
It does not follow any RFC as RFC822, RFC2822, RFC3696 as it's intended for common bn
|
18
|
+
|
19
|
+
Installation:
|
20
|
+
=============
|
21
|
+
Make sure you have rubygems >= 1.3.6
|
22
|
+
|
23
|
+
Install the gem(s):
|
24
|
+
sudo gem install brodock-validates_as_email
|
25
|
+
|
26
|
+
Add to environment.rb initializer block:
|
27
|
+
config.gem 'brodock-validates_as_email', :lib => 'validates_as_email'
|
28
|
+
|
29
|
+
Usage:
|
30
|
+
======
|
31
|
+
In your model file do something like:
|
32
|
+
|
33
|
+
class MyClass < ActiveRecord::Base
|
34
|
+
validates_as_email :email, :message => 'Invalid Email Address', :allow_nil => true
|
35
|
+
end
|
36
|
+
|
37
|
+
class MyOtherClass < ActiveRecord::Base
|
38
|
+
# will not accept addresses with a single part domain, e.g user@example
|
39
|
+
validates_as_email :email, :message => 'Invalid Email Address', :allow_nil => true, :restrict_domain => true
|
40
|
+
end
|
41
|
+
|
42
|
+
Tests:
|
43
|
+
======
|
44
|
+
Some tests have been added.
|
45
|
+
|
46
|
+
License:
|
47
|
+
========
|
48
|
+
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,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Validation helper for ActiveRecord derived objects that cleanly and simply
|
4
|
+
# allows the model to check if the given string is a valid email
|
5
|
+
# address.
|
6
|
+
#
|
7
|
+
# This code does not comply with any RFC as I don't find usual to do so,
|
8
|
+
# if you don't agree with me, please read: http://www.dominicsayers.com/isemail/
|
9
|
+
#
|
10
|
+
# Original code by Ximon Eighteen <ximon.eightee@int.greenpeace.org> which was
|
11
|
+
# heavily based on code I can no longer find on the net, my apologies to the
|
12
|
+
# author!
|
13
|
+
#
|
14
|
+
|
15
|
+
module ActiveModel
|
16
|
+
module Validations
|
17
|
+
class EmailValidator < EachValidator
|
18
|
+
def initialize(options)
|
19
|
+
configuration = {
|
20
|
+
:message => (I18n.translate(:'activerecord.errors.messages.invalid_email', :raise => true) rescue 'is an invalid email address'),
|
21
|
+
:allow_nil => false
|
22
|
+
}
|
23
|
+
super(options.reverse_merge(configuration))
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate_each(record, attribute, value)
|
27
|
+
|
28
|
+
return if options[:allow_nil] && value.nil?
|
29
|
+
|
30
|
+
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
31
|
+
record.errors[attribute] << (options[:message])
|
32
|
+
return
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module ClassMethods
|
38
|
+
def validates_as_email(*attr_names)
|
39
|
+
validates_with EmailValidator, _merge_attributes(attr_names)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
4
|
+
require 'active_model'
|
5
|
+
require 'validates_as_email'
|
6
|
+
|
7
|
+
|
8
|
+
class TestRecord
|
9
|
+
include ActiveModel::Validations
|
10
|
+
attr_accessor :email
|
11
|
+
def initialize(email)
|
12
|
+
@email = email
|
13
|
+
end
|
14
|
+
validates :email, :email => true
|
15
|
+
end
|
16
|
+
|
17
|
+
class ValidatesAsEmailTest < Test::Unit::TestCase
|
18
|
+
def test_illegal_email_address
|
19
|
+
addresses = [
|
20
|
+
'Max@Job 3:14',
|
21
|
+
'Job@Book of Job',
|
22
|
+
'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
|
23
|
+
'Nobody',
|
24
|
+
'Nobody@Knows',
|
25
|
+
'"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
|
26
|
+
'me@[187.223.45.119]',
|
27
|
+
]
|
28
|
+
addresses.each do |address|
|
29
|
+
assert !TestRecord.new(address).valid?, "#{address} should be illegal."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_legal_email_address
|
34
|
+
addresses = [
|
35
|
+
'test@example.com',
|
36
|
+
'test@example.co.uk',
|
37
|
+
'someone@123.com',
|
38
|
+
]
|
39
|
+
addresses.each do |address|
|
40
|
+
assert TestRecord.new(address).valid?, "#{address} should be legal."
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brodock-validates_as_email
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 7
|
8
|
+
- 0
|
9
|
+
version: 0.7.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michal Zima
|
13
|
+
- Ximon Eighteen
|
14
|
+
- Dan Kubb
|
15
|
+
- Thijs van der Vossen
|
16
|
+
- Donncha Redmond
|
17
|
+
- Gabriel Mazetto (brodock)
|
18
|
+
autorequire:
|
19
|
+
bindir: bin
|
20
|
+
cert_chain: []
|
21
|
+
|
22
|
+
date: 2010-04-24 00:00:00 -03:00
|
23
|
+
default_executable:
|
24
|
+
dependencies: []
|
25
|
+
|
26
|
+
description: Rails gem/plugin that implements an ActiveRecord validation helper called validates_as_email which validates email address (RFC822)
|
27
|
+
email: dredmond@e-xact.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files: []
|
33
|
+
|
34
|
+
files:
|
35
|
+
- CHANGELOG
|
36
|
+
- LICENSE
|
37
|
+
- README
|
38
|
+
- Rakefile
|
39
|
+
- init.rb
|
40
|
+
- lib/validates_as_email.rb
|
41
|
+
- test/validates_as_email_test.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/brodock/validates_as_email
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Rails gem/plugin to validate format of email addresses (RFC822)
|
72
|
+
test_files:
|
73
|
+
- test/validates_as_email_test.rb
|