validate_email 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,52 @@
1
+ # ValidateEmail
2
+
3
+ This gem adds the capability of validating email addresses to `ActiveModel`.
4
+ The gem only supports Rails 3 (has dependencies in ActiveModel and ActiveSupport 3.0)
5
+
6
+ ### Installation
7
+ # add this to your Gemfile
8
+ gem "validate_email"
9
+
10
+ # and then run
11
+ rake gems:install
12
+
13
+ # or just run
14
+ sudo gem install validate_email
15
+
16
+ ### Usage
17
+
18
+ #### With ActiveRecord
19
+ class Pony < ActiveRecord::Base
20
+ # standard validation
21
+ validates :email_address, :email => true
22
+
23
+ # with allow_nil
24
+ validates :email_address, :email => {:allow_nil => true}
25
+
26
+ # with allow_blank
27
+ validates :email_address, :email => {:allow_blank => true}
28
+ end
29
+
30
+ #### With ActiveModel
31
+ class Unicorn
32
+ include ActiveModel::Validations
33
+
34
+ attr_accessor :email_address
35
+
36
+ # with legacy syntax (the syntax above works also)
37
+ validates_email :email_address, :allow_blank => true
38
+ end
39
+
40
+ #### I18n
41
+
42
+ The default error message `is not valid email`.
43
+ You can pass the `:message => "my custom error"` option to your validation to define your own, custom message.
44
+
45
+ ## Authors
46
+
47
+ **Tanel Suurhans** (<http://twitter.com/tanelsuurhans>)
48
+ **Tarmo Lehtpuu** (<http://twitter.com/tarmolehtpuu>)
49
+
50
+ ## License
51
+ Copyright 2010 by PerfectLine LLC (<http://www.perfectline.co.uk>) and is released under the MIT license.
52
+
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'README.markdown'))
@@ -0,0 +1,36 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+ require 'active_support/concern'
4
+ require 'mail'
5
+
6
+ module Perfectline
7
+ module ValidateEmail
8
+ extend ActiveSupport::Concern
9
+
10
+ class EmailValidator < ActiveModel::EachValidator
11
+ def initialize(options)
12
+ options[:message] ||= "is not a valid email"
13
+ super(options)
14
+ end
15
+
16
+ def validate_each(record, attribute, value)
17
+ begin
18
+ mail = Mail::Address.new(value)
19
+ unless mail.address == value && mail.domain.split(".").length > 1
20
+ record.errors.add(attribute, options[:message])
21
+ end
22
+ rescue
23
+ record.errors.add(attribute, options[:message])
24
+ end
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def validates_email(*attr_names)
30
+ validates_with EmailValidator, _merge_attributes(attr_names)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ ActiveModel::Validations.send(:include, Perfectline::ValidateEmail)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validate_email'
@@ -0,0 +1,9 @@
1
+ require 'active_model/validations'
2
+
3
+ class User
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :email_address
7
+
8
+ validates :email_address, :email => true
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_model/validations'
2
+
3
+ class UserWithBlank
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :email_address
7
+
8
+ validates :email_address, :email => {:allow_blank => true}
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_model/validations'
2
+
3
+ class UserWithLegacySyntax
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :email_address
7
+
8
+ validates_email :email_address, :allow_blank => true
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_model/validations'
2
+
3
+ class UserWithNil
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :email_address
7
+
8
+ validates :email_address, :email => {:allow_nil => true}
9
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/resources')
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ require 'spec'
6
+ require 'validate_email'
7
+
8
+ autoload :User, 'resources/user'
9
+ autoload :UserWithNil, 'resources/user_with_nil'
10
+ autoload :UserWithBlank, 'resources/user_with_blank'
11
+ autoload :UserWithLegacySyntax, 'resources/user_with_legacy_syntax'
12
+
@@ -0,0 +1,114 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Enail validation", Perfectline::ValidateEmail::EmailValidator do
4
+
5
+ context "with regular validator" do
6
+ before do
7
+ @user = User.new
8
+ end
9
+
10
+ it "should not allow nil as email" do
11
+ @user.email_address = nil
12
+ @user.should_not be_valid
13
+ end
14
+
15
+ it "should not allow blank as email" do
16
+ @user.email_address = ""
17
+ @user.should_not be_valid
18
+ end
19
+
20
+ it "should not allow an email without domain extension" do
21
+ @user.email_address = "user@example"
22
+ @user.should_not be_valid
23
+ end
24
+
25
+ it "should not allow an email without @" do
26
+ @user.email_address = "user"
27
+ @user.should_not be_valid
28
+ end
29
+
30
+ it "should not allow an email without prefix" do
31
+ @user.email_address = "@example.com"
32
+ @user.should_not be_valid
33
+ end
34
+
35
+ it "should not allow an email without domain" do
36
+ @user.email_address = "user@.com"
37
+ @user.should_not be_valid
38
+ end
39
+
40
+ it "should accept a valid email address" do
41
+ @user.email_address = "user@example.com"
42
+ @user.should be_valid
43
+ end
44
+ end
45
+
46
+ context "with allow nil" do
47
+ before do
48
+ @user = UserWithNil.new
49
+ end
50
+
51
+ it "should allow nil as email" do
52
+ @user.email_address = nil
53
+ @user.should be_valid
54
+ end
55
+
56
+ it "should not allow blank as email" do
57
+ @user.email_address = ""
58
+ @user.should_not be_valid
59
+ end
60
+
61
+ it "shoild allow a valid email address" do
62
+ @user.email_address = "user@example.com"
63
+ @user.should be_valid
64
+ end
65
+ end
66
+
67
+ context "with allow blank" do
68
+ before do
69
+ @user = UserWithBlank.new
70
+ end
71
+
72
+ it "should allow nil as email" do
73
+ @user.email_address = nil
74
+ @user.should be_valid
75
+ end
76
+
77
+ it "should allow blank as email" do
78
+ @user.email_address = ""
79
+ @user.should be_valid
80
+ end
81
+
82
+ it "should allow a valid email address" do
83
+ @user.email_address = "user@example.com"
84
+ @user.should be_valid
85
+ end
86
+ end
87
+
88
+ context "with legacy syntax" do
89
+ before do
90
+ @user = UserWithLegacySyntax.new
91
+ end
92
+
93
+ it "should allow nil as email" do
94
+ @user.email_address = nil
95
+ @user.should be_valid
96
+ end
97
+
98
+ it "should allow blank as email" do
99
+ @user.email_address = ""
100
+ @user.should be_valid
101
+ end
102
+
103
+ it "should allow a valid email address" do
104
+ @user.email_address = "user@example.com"
105
+ @user.should be_valid
106
+ end
107
+
108
+ it "should not allow invalid email" do
109
+ @user.email_address = "random"
110
+ @user.should_not be_valid
111
+ end
112
+ end
113
+
114
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validate_email
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Tanel Suurhans
13
+ - Tarmo Lehtpuu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-07 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: diff-lcs
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 1
45
+ - 2
46
+ version: 1.1.2
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: mail
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 2
59
+ - 2
60
+ - 5
61
+ version: 2.2.5
62
+ type: :runtime
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: activesupport
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 3
74
+ - 0
75
+ version: "3.0"
76
+ type: :runtime
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: activemodel
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 3
88
+ - 0
89
+ version: "3.0"
90
+ type: :runtime
91
+ version_requirements: *id005
92
+ description: Library for validating email addresses in Rails 3 models.
93
+ email:
94
+ - tanel.suurhans@perfectline.ee
95
+ - tarmo.lehtpuu@perfectline.ee
96
+ executables: []
97
+
98
+ extensions: []
99
+
100
+ extra_rdoc_files:
101
+ - README.markdown
102
+ files:
103
+ - README.markdown
104
+ - install.rb
105
+ - lib/validate_email.rb
106
+ - rails/init.rb
107
+ - spec/resources/user.rb
108
+ - spec/resources/user_with_blank.rb
109
+ - spec/resources/user_with_legacy_syntax.rb
110
+ - spec/resources/user_with_nil.rb
111
+ - spec/spec_helper.rb
112
+ - spec/validate_email_spec.rb
113
+ has_rdoc: true
114
+ homepage: http://github.com/perfectline/validate_email/tree/master
115
+ licenses: []
116
+
117
+ post_install_message:
118
+ rdoc_options:
119
+ - --charset=UTF-8
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project:
141
+ rubygems_version: 1.3.7
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: Library for validating email addresses in Rails 3 models.
145
+ test_files:
146
+ - spec/resources/user.rb
147
+ - spec/resources/user_with_blank.rb
148
+ - spec/resources/user_with_legacy_syntax.rb
149
+ - spec/resources/user_with_nil.rb
150
+ - spec/spec_helper.rb
151
+ - spec/validate_email_spec.rb