Skip to content

Commit

Permalink
Finish renaming to validates_as_email_address.
Browse files Browse the repository at this point in the history
  • Loading branch information
obrie committed Jul 30, 2007
1 parent 4026198 commit 9405218
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 106 deletions.
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
http://creativecommons.org/licenses/by-sa/2.5/
20 changes: 0 additions & 20 deletions MIT-LICENSE

This file was deleted.

25 changes: 17 additions & 8 deletions README
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
= validates_anyclusion_of_with_partial
= validates_as_email_address

validates_anyclusion_of_with_partial .
validates_as_email_address is a monkey match of validates_as_email which adds a
broader range of options, as well as an additional validation regarding the
length of the email address.

== Resources

Wiki

* http://wiki.pluginaweek.org/Validates_anyclusion_of_with_partial
* http://wiki.pluginaweek.org/Validates_as_email_address

Announcement

* http://www.pluginaweek.org/

Source

* http://svn.pluginaweek.org/trunk/plugins/active_record/validations/validates_anyclusion_of_with_partial
* http://svn.pluginaweek.org/trunk/plugins/active_record/validations/validates_as_email_address

Development

* http://dev.pluginaweek.org/browser/trunk/plugins/active_record/validations/validates_anyclusion_of_with_partial
* http://dev.pluginaweek.org/browser/trunk/plugins/active_record/validations/validates_as_email_address

== Description


== References

Ximon Eighteen <[email protected]>
Dan Kubb <[email protected]>
Thijs van der Vossen <[email protected]>
=== RFC822 Regular Expression

Originally written by Cal Henderson
c.f. http://iamcal.com/publish/articles/php/parsing_email/

Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb <[email protected]>

=== validates_as_email

Original code by Ximon Eighteen <[email protected]> and Thijs van der Vossen <[email protected]>
10 changes: 5 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ require 'rake/rdoctask'
require 'rake/gempackagetask'
require 'rake/contrib/sshpublisher'

PKG_NAME = 'validates_as_email'
PKG_NAME = 'validates_as_email_address'
PKG_VERSION = '0.0.1'
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
RUBY_FORGE_PROJECT = 'pluginaweek'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the validates_as_email plugin.'
desc 'Test the validates_as_email_address plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the validates_as_email plugin.'
desc 'Generate documentation for the validates_as_email_address plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ValidatesAsEmail'
rdoc.title = 'ValidatesAsEmailAddress'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
Expand All @@ -35,7 +35,7 @@ spec = Gem::Specification.new do |s|

s.files = FileList['{lib,tasks,test}/**/*'].to_a + %w(init.rb MIT-LICENSE Rakefile README)
s.require_path = 'lib'
s.autorequire = 'validates_as_email'
s.autorequire = 'validates_as_email_address'
s.has_rdoc = true
s.test_files = Dir['test/**/*_test.rb']

Expand Down
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'validates_as_email'
require 'validates_as_email_address'
82 changes: 32 additions & 50 deletions lib/validates_as_email.rb → lib/validates_as_email_address.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
#
# RFC822 Email Address Regex
# --------------------------
#
# Originally written by Cal Henderson
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
#
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
# http://creativecommons.org/licenses/by-sa/2.5/
#
# The standard describing the format of email addresses
module RFC822
EmailAddress = begin
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
quoted_pair = '\\x5c[\\x00-\\x7f]'
domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
Expand All @@ -29,29 +17,18 @@ module RFC822
end
end

# Validation helper for ActiveRecord derived objects that cleanly and simply
# allows the model to check if the given string is a syntactically valid email
# address (by using the RFC822 module above).
#
# Original code by Ximon Eighteen <[email protected]> which was
# heavily based on code I can no longer find on the net, my apologies to the
# author!
#
# Huge credit goes to Dan Kubb <[email protected]> for
# submitting a patch to massively simplify this code and thereby instruct me
# in the ways of Rails too! I reflowed the patch a little to keep the line
# length to a maximum of 78 characters, an old habit.
#
module ActiveRecord #:nodoc:
class Errors #:nodoc:
default_error_messages.update(
:invalid_email => 'is an invalid email'
)
end

module Validations #:nodoc:
module ClassMethods
#
module PluginAWeek #:nodoc:
module Validates #:nodoc:
module EmailAddress
# The options that can be used when validating the format of the email address
EMAIL_FORMAT_OPTIONS = [
:message,
:allow_nil,
:on,
:if
]

# The options that can be used when validating the length of the email address
EMAIL_LENGTH_OPTIONS = [
:minimum,
:maximum,
Expand All @@ -60,18 +37,13 @@ module ClassMethods
:in,
:too_long,
:too_short,
:wrong_length
]

#
EMAIL_BOTH_OPTIONS = [
:message,
:wrong_length,
:allow_nil,
:on,
:if
]

# Configuration options:
# Configuration options for length:
# * <tt>minimum</tt> - The minimum size of the attribute
# * <tt>maximum</tt> - The maximum size of the attribute
# * <tt>is</tt> - The exact size of the attribute
Expand All @@ -81,30 +53,40 @@ module ClassMethods
# * <tt>too_short</tt> - The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)")
# * <tt>wrong_length</tt> - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)")
#
# Configuration options for both length and format:
# Configuration options for format:
# * <tt>message</tt> - A custom error message
#
# Configuration options for both length and format:
# * <tt>allow_nil</tt> - Attribute may be nil; skip validation.
# * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
# * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
# method, proc or string should return or evaluate to a true or false value.
#
def validates_as_email(*attr_names)
def validates_as_email_address(*attr_names)
configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
configuration.assert_valid_keys(EMAIL_FORMAT_OPTIONS & EMAIL_LENGTH_OPTIONS)
configuration.reverse_merge!(
:message => ActiveRecord::Errors.default_error_messages[:invalid_email]
)

# Add format validation
format_configuration = configuration.reject {|key, value| !EMAIL_BOTH_OPTIONS.include?(key)}
format_configuration = configuration.reject {|key, value| !EMAIL_FORMAT_OPTIONS.include?(key)}
format_configuration[:with] = RFC822::EmailAddress
validates_format_of attr_names, format_configuration

# Add length validation
length_configuration = configuration.reject {|key, value| !(EMAIL_LENGTH_OPTIONS + EMAIL_BOTH_OPTIONS).include?(key)}
length_configuration.reverse_merge!(:within => 3..384)
length_configuration = configuration.reject {|key, value| !EMAIL_LENGTH_OPTIONS.include?(key)}
length_configuration.reverse_merge!(:within => 3..320)
validates_length_of attr_names, length_configuration
end
end
end
end

ActiveRecord::Base.class_eval do
extend PluginAWeek::Validates::EmailAddress
end

ActiveRecord::Errors.default_error_messages.update(
:invalid_email => 'is an invalid email'
)
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'test/unit'

require 'rubygems'
require_gem 'activerecord'
require File.dirname(__FILE__) + '/../lib/validates_as_email_address'
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
require 'test/unit'

begin
require File.dirname(__FILE__) + '/../../../../config/boot'
require 'active_record'
require 'validates_as_email'
rescue LoadError
require 'rubygems'
require_gem 'activerecord'
require File.dirname(__FILE__) + '/../lib/validates_as_email'
end
require File.dirname(__FILE__) + '/test_helper'

class TestRecord < ActiveRecord::Base
def self.columns; []; end
attr_accessor :email
validates_as_email :email
validates_as_email_address :email
end

class ValidatesAsEmailTest < Test::Unit::TestCase
def test_illegal_rfc822_email_address
addresses = [
class ValidatesAsEmailAddressTest < Test::Unit::TestCase
def test_illegal_rfc822_email_address_should_not_be_valid
[
'Max@Job 3:14',
'Job@Book of Job',
'J. P. \'s-Gravezande, a.k.a. The [email protected]',
]
addresses.each do |address|
].each do |address|
assert !TestRecord.new(:email => address).valid?, "#{address} should be illegal."
end
end

def test_legal_rfc822_email_address
addresses = [
def test_legal_rfc822_email_address_should_be_valid
[
'test@example',
'[email protected]',
'[email protected]',
'"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
'me@[187.223.45.119]',
'[email protected]',
]
addresses.each do |address|
].each do |address|
assert TestRecord.new(:email => address).valid?, "#{address} should be legal."
end
end
Expand Down

0 comments on commit 9405218

Please sign in to comment.