validates_email-san 0.1.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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Fingertips, Manfred Stienstra <manfred@fngtps.com>, Eloy Duran <eloy@fngtps.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,10 @@
1
+ === ValidatesEmail-San
2
+
3
+ A simple Rails plugin which adds a validates_email class method to
4
+ ActiveRecord::Base.
5
+
6
+ class Member < ActiveRecord::Base
7
+ validates_email :email, :message => "is not a valid email address"
8
+ end
9
+
10
+ What else is there to say?
data/Rakefile ADDED
@@ -0,0 +1,35 @@
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_email_san plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the validates_email_san plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ValidatesEmail-san'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |s|
28
+ s.name = "validates_email-san"
29
+ s.summary = s.description = "A simple Rails plugin which adds a validates_email class method to ActiveRecord::Base."
30
+ s.homepage = "http://fingertips.github.com"
31
+ s.email = "eloy@fngtps.com"
32
+ s.authors = ["Eloy Duran", "Manfred Stienstra"]
33
+ end
34
+ rescue LoadError
35
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 1
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,23 @@
1
+ module ActiveRecord
2
+ module Validations #:nodoc:
3
+ module ClassMethods
4
+ local_part_illegal_chars = '[^@<>\(\)\[\]:;\\\\\s\.]'
5
+ EMAIL_REGEXP = /^[^\.](#{local_part_illegal_chars}|\.#{local_part_illegal_chars})+@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
6
+
7
+ # Takes a list of attributes that should be validated to be valid
8
+ # formatted email addresses. Takes all other options that
9
+ # validates_format_of does.
10
+ #
11
+ # class Member < ActiveRecord::Base
12
+ # validates_email :email, :message => "is not a valid email address"
13
+ # end
14
+ #
15
+ # Note that the example message is the default.
16
+ def validates_email(*attr_names)
17
+ options = { :with => EMAIL_REGEXP, :message => "is not a valid email address" }
18
+ options.merge!(attr_names.extract_options!)
19
+ validates_format_of attr_names, options
20
+ end
21
+ end
22
+ end
23
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "validates_email_san"
@@ -0,0 +1,61 @@
1
+ module ValidatesEmailSanTest
2
+ module Initializer
3
+ VENDOR_RAILS = File.expand_path('../../../../../vendor/rails', __FILE__)
4
+ OTHER_RAILS = File.expand_path('../../../rails', __FILE__)
5
+ PLUGIN_ROOT = File.expand_path('../../', __FILE__)
6
+
7
+ def self.rails_directory
8
+ if File.exist?(VENDOR_RAILS)
9
+ VENDOR_RAILS
10
+ elsif File.exist?(OTHER_RAILS)
11
+ OTHER_RAILS
12
+ end
13
+ end
14
+
15
+ def self.load_dependencies
16
+ if rails_directory
17
+ $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
18
+ $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
19
+ else
20
+ require 'rubygems' rescue LoadError
21
+ end
22
+
23
+ require 'test/unit'
24
+
25
+ require 'active_support'
26
+ require 'active_support/test_case'
27
+ require 'active_record'
28
+ require 'active_record/test_case'
29
+ require 'active_record/base' # this is needed because of dependency hell
30
+
31
+ $:.unshift File.expand_path('../../lib', __FILE__)
32
+ require File.join(PLUGIN_ROOT, 'rails', 'init')
33
+ end
34
+
35
+ def self.configure_database
36
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
37
+ ActiveRecord::Migration.verbose = false
38
+ end
39
+
40
+ def self.setup_database
41
+ ActiveRecord::Schema.define(:version => 1) do
42
+ create_table :members do |t|
43
+ t.column :email, :string
44
+ end
45
+ end
46
+ end
47
+
48
+ def self.teardown_database
49
+ ActiveRecord::Base.connection.tables.each do |table|
50
+ ActiveRecord::Base.connection.drop_table(table)
51
+ end
52
+ end
53
+
54
+ def self.start
55
+ load_dependencies
56
+ configure_database
57
+ end
58
+ end
59
+ end
60
+
61
+ ValidatesEmailSanTest::Initializer.start
@@ -0,0 +1,121 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class Member < ActiveRecord::Base
4
+ validates_email :email
5
+
6
+ def run_validation?
7
+ false
8
+ end
9
+ end
10
+
11
+ class ValidatesEmailTest < ActiveSupport::TestCase
12
+ def setup
13
+ ValidatesEmailSanTest::Initializer.setup_database
14
+ @obj = Member.new
15
+ end
16
+
17
+ def teardown
18
+ ValidatesEmailSanTest::Initializer.teardown_database
19
+ Member.validates_email :email
20
+ end
21
+
22
+ test "accepts valid email addresses" do
23
+ %w{
24
+ sasha@example.com
25
+ foo.bar.baz@example.com
26
+ FOO.bar.BAZ@EXAMPLE.COM
27
+ ML+foo@example.com
28
+ foo@bar.example.com
29
+ foo@in.nl
30
+ }.each do |email|
31
+ assert_valid_email email
32
+ end
33
+ end
34
+
35
+ test "does not allow with missing @" do
36
+ assert_not_valid_email 'foo.example.com'
37
+ end
38
+
39
+ test "does not allow slashes" do
40
+ assert_not_valid_email 'foo.\@example.com'
41
+ assert_not_valid_email 'foo\bar@example.com'
42
+ end
43
+
44
+ test "does not allow colons" do
45
+ assert_not_valid_email 'foo.:@example.com'
46
+ assert_not_valid_email 'foo:bar@example.com'
47
+ end
48
+
49
+ test "does not allow semi-colons" do
50
+ assert_not_valid_email 'foo.;@example.com'
51
+ assert_not_valid_email 'foo;bar@example.com'
52
+ end
53
+
54
+ test "does not allow parentheses" do
55
+ assert_not_valid_email 'foo(bar@example.com'
56
+ assert_not_valid_email 'foo)bar@example.com'
57
+ assert_not_valid_email 'foo(bar)baz@example.com'
58
+ assert_not_valid_email 'foo.(@example.com'
59
+ assert_not_valid_email 'foo.)@example.com'
60
+ end
61
+
62
+ test "does not allow angle brackets" do
63
+ assert_not_valid_email 'foo<bar@example.com'
64
+ assert_not_valid_email 'foo>bar@example.com'
65
+ assert_not_valid_email 'foo<bar>baz@example.com'
66
+ assert_not_valid_email 'foo.<@example.com'
67
+ assert_not_valid_email 'foo.>baz@example.com'
68
+ end
69
+
70
+ test "does not allow square brackets" do
71
+ assert_not_valid_email 'foo[bar@example.com'
72
+ assert_not_valid_email 'foo]bar@example.com'
73
+ assert_not_valid_email 'foo[bar]baz@example.com'
74
+ assert_not_valid_email 'foo.[@example.com'
75
+ assert_not_valid_email 'foo.]@example.com'
76
+ end
77
+
78
+ test "does not allow consecutive dots" do
79
+ assert_not_valid_email 'foo..bar@example.com'
80
+ end
81
+
82
+ test "does not allow first character of local part to be a dot" do
83
+ assert_not_valid_email '.foo@example.com'
84
+ end
85
+
86
+ test "does not allow last character of local part to be a dot" do
87
+ assert_not_valid_email 'foo.@example.com'
88
+ end
89
+
90
+ test "adds a sensible default error message" do
91
+ @obj.email = 'foo.@example.com'; @obj.valid?
92
+ assert_match /is not a valid email address/, @obj.errors.on(:email).to_s
93
+ end
94
+
95
+ test "allows the passing of all options allowed by validates_format_of" do
96
+ Member.validates_email :email, :message => "dude, that's sooo not an email address", :if => :run_validation?
97
+ @obj.email = 'foo.@example.com';
98
+
99
+ def @obj.run_validation?; false; end
100
+ @obj.valid?
101
+ assert_no_match(/dude, that's sooo not an email address/, @obj.errors.on(:email).to_s)
102
+
103
+ def @obj.run_validation?; true; end
104
+ @obj.valid?
105
+ assert_match /dude, that's sooo not an email address/, @obj.errors.on(:email).to_s
106
+ end
107
+
108
+ private
109
+
110
+ def assert_valid_email(email)
111
+ @obj.email = email
112
+ @obj.valid?
113
+ assert @obj.errors.on(:email).blank?, "Expected `#{email}' to be valid"
114
+ end
115
+
116
+ def assert_not_valid_email(email)
117
+ @obj.email = email
118
+ @obj.valid?
119
+ assert !@obj.errors.on(:email).blank?, "Expected `#{email}' to NOT be valid"
120
+ end
121
+ end
@@ -0,0 +1,45 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{validates_email-san}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Eloy Duran", "Manfred Stienstra"]
9
+ s.date = %q{2009-09-07}
10
+ s.description = %q{A simple Rails plugin which adds a validates_email class method to ActiveRecord::Base.}
11
+ s.email = %q{eloy@fngtps.com}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE",
14
+ "README.rdoc"
15
+ ]
16
+ s.files = [
17
+ "LICENSE",
18
+ "README.rdoc",
19
+ "Rakefile",
20
+ "VERSION.yml",
21
+ "lib/validates_email_san.rb",
22
+ "rails/init.rb",
23
+ "test/test_helper.rb",
24
+ "test/validates_email_san_test.rb"
25
+ ]
26
+ s.homepage = %q{http://fingertips.github.com}
27
+ s.rdoc_options = ["--charset=UTF-8"]
28
+ s.require_paths = ["lib"]
29
+ s.rubygems_version = %q{1.3.5}
30
+ s.summary = %q{A simple Rails plugin which adds a validates_email class method to ActiveRecord::Base.}
31
+ s.test_files = [
32
+ "test/test_helper.rb",
33
+ "test/validates_email_san_test.rb"
34
+ ]
35
+
36
+ if s.respond_to? :specification_version then
37
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
41
+ else
42
+ end
43
+ else
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_email-san
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Eloy Duran
8
+ - Manfred Stienstra
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-03-26 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: A simple Rails plugin which adds a validates_email class method to ActiveRecord::Base.
18
+ email: eloy@fngtps.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - LICENSE
25
+ - README.rdoc
26
+ files:
27
+ - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - lib/validates_email_san.rb
32
+ - rails/init.rb
33
+ - test/test_helper.rb
34
+ - test/validates_email_san_test.rb
35
+ - validates_email-san.gemspec
36
+ has_rdoc: true
37
+ homepage: http://fingertips.github.com
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A simple Rails plugin which adds a validates_email class method to ActiveRecord::Base.
64
+ test_files:
65
+ - test/test_helper.rb
66
+ - test/validates_email_san_test.rb