-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish renaming to validates_as_email_address.
- Loading branch information
Showing
8 changed files
with
72 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require 'validates_as_email' | ||
require 'validates_as_email_address' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
32 changes: 10 additions & 22 deletions
32
test/validates_as_email_test.rb → test/validates_as_email_address_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|