ar-validations 1.1.0

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 [Nicolas Cavigneaux]
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 ADDED
@@ -0,0 +1,49 @@
1
+ -*-markdown-*-
2
+
3
+
4
+ AR Validations
5
+ ==============
6
+
7
+ This ActiveRecord extension provides more validation schemes. There are some useful validation missing added by this plugin to fill the gap.
8
+
9
+ Available validations are :
10
+
11
+ * *email*: It checks for presence, length, format and uniqueness. There is no guarantee that a validated email is real and deliverable.
12
+ * *URLs*: It checks for validity of the link by contacting it. If not protocol is not specified, it'll automatically add "http://" in front of the url.
13
+
14
+ Installation
15
+ ------------
16
+
17
+ In your Rails app root, use the following command-line :
18
+
19
+ cd vendor/plugins
20
+ hg clone http://bitbucket.org/Bounga/ar-validations/
21
+
22
+ or install it system-wide :
23
+
24
+ $ sudo gem install ar-validations
25
+
26
+ and require it in Rails::Initializer (environment.rb) :
27
+
28
+ config.gem 'ar-validations'
29
+
30
+
31
+ Example
32
+ -------
33
+
34
+ In your model :
35
+
36
+ class User < ActiveRecord::Base
37
+ validates_email :email
38
+ validates_url :url
39
+ end
40
+
41
+ Other
42
+ -----
43
+
44
+ For more information see [Project homepage](http://www.bitbucket.org/Bounga/ar-validations/)
45
+
46
+ Problems, comments, and suggestions are welcome on the [ticket system](http://www.bitbucket.org/Bounga/ar-validations/issues/new/)
47
+
48
+
49
+ Copyright (c) 2009 Nicolas Cavigneaux, released under the MIT license
@@ -0,0 +1,70 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/contrib/rubyforgepublisher'
6
+ require 'rubyforge'
7
+
8
+ SPEC = Gem::Specification.new do |s|
9
+ s.name = 'ar-validations'
10
+ s.rubyforge_project = 'ar-validations'
11
+ s.version = '1.1.0'
12
+ s.authors = ['Nicolas Cavigneaux']
13
+ s.email = 'nico@bounga.org'
14
+ s.homepage = 'http://www.bitbucket.org/Bounga/ar-validations'
15
+ s.summary = 'An ActiveRecord extension provides more validation schemes'
16
+ s.description = 'This ActiveRecord extension provides more validation schemes. There are some useful validation missing added by this plugin to fill the gap.'
17
+ s.files = [ "Rakefile", "init.rb", "README", "LICENSE"] +
18
+ Dir.glob("{bin,assets,doc,lib,tasks,test}/**/*")
19
+ s.test_file = "test/validations_test.rb"
20
+ s.has_rdoc = true
21
+ s.extra_rdoc_files = ['README']
22
+ s.require_paths = ["lib"]
23
+ s.add_dependency('activerecord')
24
+ end
25
+
26
+ desc 'Default: run unit tests.'
27
+ task :default => :test
28
+
29
+ task :gem
30
+ Rake::GemPackageTask.new(SPEC) do |pkg|
31
+ pkg.need_zip = true
32
+ pkg.need_tar_bz2 = true
33
+ end
34
+
35
+ desc "Install gem file #{SPEC.name}-#{SPEC.version}.gem"
36
+ task :install => [:gem] do
37
+ sh "gem install pkg/#{SPEC.name}-#{SPEC.version}.gem"
38
+ end
39
+
40
+ desc "Publish documentation to RubyForge"
41
+ task :publish_doc => [:rdoc] do
42
+ rf = Rake::RubyForgePublisher.new(SPEC.rubyforge_project, 'bounga')
43
+ rf.upload
44
+ puts "Published documentation to RubyForge"
45
+ end
46
+
47
+ desc "Release gem #{SPEC.name}-#{SPEC.version}.gem"
48
+ task :release => [:gem, :publish_doc] do
49
+ rf = RubyForge.new.configure
50
+ puts "Logging in"
51
+ rf.login
52
+
53
+ puts "Releasing #{SPEC.name} v.#{SPEC.version}"
54
+
55
+ files = Dir.glob('pkg/*.{zip,bz2,gem}')
56
+ rf.add_release SPEC.rubyforge_project, SPEC.rubyforge_project, SPEC.version, *files
57
+ end
58
+
59
+ Rake::TestTask.new(:test) do |t|
60
+ t.libs << 'lib'
61
+ t.pattern = 'test/**/*_test.rb'
62
+ t.verbose = true
63
+ end
64
+
65
+ Rake::RDocTask.new(:rdoc) do |rdoc|
66
+ rdoc.title = 'AR Validations'
67
+ rdoc.options << '--line-numbers' << '--inline-source'
68
+ rdoc.rdoc_files.include('README')
69
+ rdoc.rdoc_files.include('lib/**/*.rb')
70
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/lib"
2
+ require 'ar-validations'
@@ -0,0 +1,96 @@
1
+ require 'open-uri'
2
+
3
+ module Bounga
4
+ module ActiveRecord
5
+ module Validations
6
+ RE_EMAIL_NAME = '[\w\.%\+\-]+' # what you actually see in practice
7
+ #RE_EMAIL_NAME = '0-9A-Z!#\$%\&\'\*\+_/=\?^\-`\{|\}~\.' # technically allowed by RFC-2822
8
+ RE_DOMAIN_HEAD = '(?:[A-Z0-9\-]+\.)+'
9
+ RE_DOMAIN_TLD = '(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)'
10
+ RE_EMAIL_OK = /\A#{RE_EMAIL_NAME}@#{RE_DOMAIN_HEAD}#{RE_DOMAIN_TLD}\z/i
11
+ MSG_EMAIL_BAD = "should look like an email address."
12
+ RE_URL_OK = /^(http|https|ftp)/i
13
+ MSG_URL_BAD = "should be a valid url."
14
+
15
+ def self.included(base)
16
+ base.extend ClassMethods
17
+ end
18
+
19
+ # This ActiveRecord extension provides more validation schemes.
20
+ # You can validate:
21
+ # * *email*: It checks for presence, length, format and uniqueness.
22
+ # There is no guarantee that a validated email is real and deliverable.
23
+ # * *URLs*: It checks for validity of the link by contacting it.
24
+ # If not protocol is not specified, it'll automatically add "http://" in front of the url.
25
+ #
26
+ # User example:
27
+ #
28
+ # class User < ActiveRecord::Base
29
+ # validates_email :email
30
+ # validates_url :url
31
+ # end
32
+ module ClassMethods
33
+ # Configuration options are:
34
+ #
35
+ # * <tt>:allow_blank</tt> - If set to <tt>true</tt>, skips this validation if the attribute is blank (default: <tt>false</tt>)
36
+ # * <tt>:allow_nil</tt> - If set to <tt>true</tt>, skips this validation if the attribute is nil (default: <tt>false</tt>)
37
+ # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>)
38
+ # * <tt>:with</tt> - The regular expression used to validate the email format with (default: RFC-2822 compliant)
39
+ # * <tt>:uniq</tt> - If set to <tt>true</tt>, ensure uniqueness of the attribute (default: <tt>false</tt>)
40
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should occur (e.g.
41
+ # <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
42
+ # or string should return or evaluate to a true or false value.
43
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should not occur
44
+ # (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>).
45
+ # The method, proc or string should return or evaluate to a true or false value.
46
+ # * <tt>:message</tt> - A custom error message (default: <tt>"should look like an email address."</tt>)
47
+ def validates_email(*fields)
48
+ options = { :message => MSG_EMAIL_BAD, :on => :save, :with => RE_EMAIL_OK, :allow_blank => false, :allow_nil => false }
49
+ options.update(fields.extract_options!)
50
+
51
+ validates_each(fields, options) do |record, attr, value|
52
+ validates_presence_of(attr) unless options[:allow_nil] or options[:allow_blank]
53
+
54
+ with_options(:allow_blank => options[:allow_blank], :allow_nil => options[:allow_nil]) do |el|
55
+ el.validates_length_of attr, :within => 6..100
56
+ el.validates_uniqueness_of attr, :case_sensitive => false if options[:uniq]
57
+ el.validates_format_of attr, :with => options[:with], :message => options[:message]
58
+ end
59
+ end
60
+ end
61
+
62
+ # Configuration options are:
63
+ #
64
+ # * <tt>:allow_blank</tt> - If set to <tt>true</tt>, skips this validation if the attribute is blank (default: <tt>false</tt>)
65
+ # * <tt>:allow_nil</tt> - If set to <tt>true</tt>, skips this validation if the attribute is nil (default: <tt>false</tt>)
66
+ # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>)
67
+ # * <tt>:with</tt> - The regular expression used to validate the format with. Custom regexp will disable
68
+ # real URI verification (no contact with URI) (default: RE_URL_OK)
69
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should occur (e.g.
70
+ # <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
71
+ # or string should return or evaluate to a true or false value.
72
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should not occur
73
+ # (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>).
74
+ # The method, proc or string should return or evaluate to a true or false value.
75
+ # * <tt>:message</tt> - A custom error message (default: <tt>"should be a valid url."</tt>)
76
+ def validates_url(*fields)
77
+ options = { :message => MSG_URL_BAD, :on => :save, :with => RE_URL_OK, :allow_blank => false, :allow_nil => false }
78
+ options.update(fields.extract_options!)
79
+
80
+ validates_each(fields, options) do |record, attr, value|
81
+ validates_presence_of(attr) unless options[:allow_nil] or options[:allow_blank]
82
+
83
+ if options[:with] == RE_URL_OK
84
+ record.send(attr.to_s + "=", "http://#{value}") unless value =~ options[:with]
85
+ open(record.send(attr)) rescue record.errors.add(attr, options[:message]) unless value.blank?
86
+ else
87
+ record.errors.add(attr, options[:message]) unless value =~ options[:with]
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ ActiveRecord::Base.send(:include, Bounga::ActiveRecord::Validations)
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :validations do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,110 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ $KCODE = 'u'
3
+
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require 'active_record'
7
+ require "#{File.dirname(__FILE__)}/../init"
8
+
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
10
+
11
+ ActiveRecord::Schema.define :version => 0 do
12
+ create_table :users, :force => true do |t|
13
+ t.column :email, :string
14
+ t.column :email2, :string
15
+ t.column :url, :string
16
+ t.column :url2, :string
17
+ end
18
+ end
19
+
20
+ class Mixin < ActiveRecord::Base
21
+ def self.table_name() "users" end
22
+ end
23
+
24
+ class User < Mixin
25
+
26
+ end
27
+
28
+ class UserEmail < Mixin
29
+ validates_email :email
30
+ end
31
+
32
+ class UserEmailCustom < Mixin
33
+ validates_email :email, :message => "must be valid!"
34
+ end
35
+
36
+ class UserEmailUpdate < Mixin
37
+ validates_email :email, :on => :update
38
+ end
39
+
40
+ class UserEmailUniq < Mixin
41
+ validates_email :email, :uniq => true
42
+ end
43
+
44
+ class UserEmailAllowBlank < Mixin
45
+ validates_email :email, :allow_blank => true
46
+ end
47
+
48
+ class UserEmailAllowNil < Mixin
49
+ validates_email :email, :allow_nil => true
50
+ end
51
+
52
+ class UserMultipleEmail < Mixin
53
+ validates_email :email, :email2
54
+ end
55
+
56
+ class UserMultipleEmailAllowBlank < Mixin
57
+ validates_email :email, :email2, :allow_blank => true
58
+ end
59
+
60
+ class UserEmailIf < Mixin
61
+ validates_url :email, :if => Proc.new { |user| user.email.length > 10 }
62
+ end
63
+
64
+ class UserEmailUnless < Mixin
65
+ validates_url :email, :unless => Proc.new { |user| user.email.length < 10 }
66
+ end
67
+
68
+ class UserUrl < Mixin
69
+ validates_url :url
70
+ end
71
+
72
+ class UserUrlCustom < Mixin
73
+ validates_url :url, :message => "must be valid!"
74
+ end
75
+
76
+ class UserUrlCustomRe < Mixin
77
+ validates_url :url, :with => /^vnc/i
78
+ end
79
+
80
+ class UserUrlUpdate < Mixin
81
+ validates_url :url, :on => :update
82
+ end
83
+
84
+ class UserUrlAllowBlank < Mixin
85
+ validates_url :url, :allow_blank => true
86
+ end
87
+
88
+ class UserUrlAllowNil < Mixin
89
+ validates_url :url, :allow_nil => true
90
+ end
91
+
92
+ class UserMultipleUrl < Mixin
93
+ validates_url :url, :url2
94
+ end
95
+
96
+ class UserMultipleUrlAllowBlank < Mixin
97
+ validates_url :url, :url2, :allow_blank => true
98
+ end
99
+
100
+ class UserMultipleUrlAllowNil < Mixin
101
+ validates_url :url, :url2, :allow_nil => true
102
+ end
103
+
104
+ class UserUrlIf < Mixin
105
+ validates_url :url, :if => Proc.new { |user| user.url.length > 20 }
106
+ end
107
+
108
+ class UserUrlUnless < Mixin
109
+ validates_url :url, :unless => Proc.new { |user| user.url.length < 20 }
110
+ end
@@ -0,0 +1,295 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ValidationsTest < Test::Unit::TestCase
4
+ def teardown
5
+ User.delete_all
6
+ end
7
+
8
+ def test_without_url
9
+ user = UserUrl.create
10
+ assert_not_nil user.errors, user.errors.inspect
11
+ assert !user.valid?, user.inspect
12
+ end
13
+
14
+ def test_bad_url
15
+ user = UserUrl.create :url => 'http://rorvalidations.com/'
16
+ assert_not_nil user.errors, user.errors.inspect
17
+ assert !user.valid?, user.inspect
18
+ end
19
+
20
+ def test_good_url
21
+ user = UserUrl.create :url => 'http://www.google.com'
22
+ assert user.errors.empty?, user.errors.inspect
23
+ assert user.valid?, user.inspect
24
+ end
25
+
26
+ def test_good_url_without_http
27
+ user = UserUrl.create :url => 'www.google.com'
28
+ assert user.errors.empty?, user.errors.inspect
29
+ assert user.valid?, user.inspect
30
+ assert user.url, "http://www.google.com"
31
+ end
32
+
33
+ def test_bad_url_with_custom_message
34
+ user = UserUrlCustom.create :url => 'http://rorvalidations.com/'
35
+ assert_not_nil user.errors, user.errors.inspect
36
+ assert !user.valid?, user.inspect
37
+ assert_equal("must be valid!", user.errors['url'].to_s)
38
+ end
39
+
40
+ def test_bad_url_with_allow_blank
41
+ user = UserUrlAllowBlank.create :url => 'http://rorvalidations.com/'
42
+ assert_not_nil user.errors, user.errors.inspect
43
+ assert !user.valid?, user.inspect
44
+ end
45
+
46
+ def test_good_url_with_allow_blank
47
+ user = UserUrlAllowBlank.create :url => 'http://www.google.com'
48
+ assert user.errors.empty?, user.errors.inspect
49
+ assert user.valid?, user.inspect
50
+ end
51
+
52
+ def test_good_url_without_http_with_allow_blank
53
+ user = UserUrlAllowBlank.create :url => 'www.google.com'
54
+ assert user.errors.empty?, user.errors.inspect
55
+ assert user.valid?, user.inspect
56
+ assert user.url, "http://www.google.com"
57
+ end
58
+
59
+ def test_without_url_with_allow_blank
60
+ user = UserUrlAllowBlank.create
61
+ assert user.errors.empty?, user.errors.inspect
62
+ assert user.valid?, user.inspect
63
+ end
64
+
65
+ def test_multiple_urls
66
+ user = UserMultipleUrl.create :url => 'http://www.google.com', :url2 => 'http://www.yahoo.com'
67
+ assert user.errors.empty?, user.errors.inspect
68
+ assert user.valid?, user.inspect
69
+
70
+ user = UserMultipleUrl.create :url => 'http://rorvalidations.com/', :url2 => 'http://www.yahoo.com'
71
+ assert_not_nil user.errors, user.errors.inspect
72
+ assert !user.valid?, user.inspect
73
+
74
+ user = UserMultipleUrl.create :url => 'http://www.google.com'
75
+ assert_not_nil user.errors, user.errors.inspect
76
+ assert !user.valid?, user.inspect
77
+ end
78
+
79
+ def test_multiple_urls_with_allow_blank
80
+ user = UserMultipleUrlAllowBlank.create :url => 'http://www.google.com', :url2 => 'http://www.yahoo.com'
81
+ assert user.errors.empty?, user.errors.inspect
82
+ assert user.valid?, user.inspect
83
+
84
+ user = UserMultipleUrlAllowBlank.create :url => 'http://rorvalidations.com/', :url2 => 'http://www.yahoo.com'
85
+ assert_not_nil user.errors, user.errors.inspect
86
+ assert !user.valid?, user.inspect
87
+
88
+ user = UserMultipleUrlAllowBlank.create :url => 'http://www.google.com'
89
+ assert user.errors.empty?, user.errors.inspect
90
+ assert user.valid?, user.inspect
91
+
92
+ user = UserMultipleUrlAllowBlank.create
93
+ assert user.errors.empty?, user.errors.inspect
94
+ assert user.valid?, user.inspect
95
+ end
96
+
97
+ def test_without_url_with_allow_nil
98
+ user = UserUrlAllowNil.create
99
+ assert user.errors.empty?, user.errors.inspect
100
+ assert user.valid?, user.inspect
101
+
102
+ user = UserUrlAllowNil.create(:url => '')
103
+ assert_not_nil user.errors, user.errors.inspect
104
+ assert !user.valid?, user.inspect
105
+
106
+ user = UserMultipleUrlAllowNil.create
107
+ assert user.errors.empty?, user.errors.inspect
108
+ assert user.valid?, user.inspect
109
+
110
+ user = UserMultipleUrlAllowNil.create(:url => '')
111
+ assert_not_nil user.errors, user.errors.inspect
112
+ assert !user.valid?, user.inspect
113
+ end
114
+
115
+ def test_without_url_on_update
116
+ user = UserUrlUpdate.create
117
+ assert user.errors.empty?, user.errors.inspect
118
+ assert user.valid?, user.errors.inspect
119
+
120
+ user.update_attribute(:email, 'test@example.com')
121
+ assert_not_nil user.errors, user.errors.inspect
122
+ assert !user.valid?, user.inspect
123
+
124
+ user = UserUrlUpdate.first
125
+ user.update_attribute(:url, 'http://www.google.com')
126
+ assert user.errors.empty?, user.errors.inspect
127
+ assert user.valid?, user.inspect
128
+ end
129
+
130
+ def test_url_with_custom_regexp
131
+ user = UserUrlCustomRe.create(:url => 'http://www.google.com')
132
+ assert_not_nil user.errors, user.errors.inspect
133
+ assert !user.valid?, user.inspect
134
+
135
+ user = UserUrlCustomRe.create(:url => 'vnc://localhost')
136
+ assert user.errors.empty?, user.errors.inspect
137
+ assert user.valid?, user.inspect
138
+ end
139
+
140
+ def test_url_with_if
141
+ user = UserUrlIf.create(:url => 'http://www.google.com')
142
+ assert user.errors.empty?, user.errors.inspect
143
+ assert user.valid?, user.inspect
144
+
145
+ user = UserUrlIf.create(:url => 'http://www.a.com')
146
+ assert user.errors.empty?, user.errors.inspect
147
+ assert user.valid?, user.inspect
148
+ end
149
+
150
+ def test_url_with_unless
151
+ user = UserUrlUnless.create(:url => 'http://www.google.com')
152
+ assert user.errors.empty?, user.errors.inspect
153
+ assert user.valid?, user.inspect
154
+
155
+ user = UserUrlUnless.create(:url => 'http://www.a.com')
156
+ assert user.errors.empty?, user.errors.inspect
157
+ assert user.valid?, user.inspect
158
+ end
159
+
160
+ def test_without_email
161
+ user = UserEmail.create
162
+ assert_not_nil user.errors, user.errors.inspect
163
+ assert !user.valid?, user.inspect
164
+ end
165
+
166
+ def test_without_email_on_update
167
+ user = UserEmailUpdate.create
168
+ assert user.errors.empty?, user.errors.inspect
169
+ assert user.valid?, user.errors.inspect
170
+
171
+ user.update_attribute(:url, 'http://www.google.com')
172
+ assert_not_nil user.errors, user.errors.inspect
173
+ assert !user.valid?, user.inspect
174
+
175
+ user = UserEmailUpdate.first
176
+ user.update_attribute(:email, 'test@example.com')
177
+ assert user.errors.empty?, user.errors.inspect
178
+ assert user.valid?, user.inspect
179
+ end
180
+
181
+ def test_good_email
182
+ user = UserEmail.create :email => 'test@example.com'
183
+ assert user.errors.empty?, user.errors.inspect
184
+ assert user.valid?, user.inspect
185
+ end
186
+
187
+ def test_bad_email
188
+ user = UserEmail.create :email => 'test AT example.com'
189
+ assert_not_nil user.errors, user.errors.inspect
190
+ assert !user.valid?, user.inspect
191
+ end
192
+
193
+ def test_email_uniqueness
194
+ user = UserEmailUniq.create :email => 'test@example.com'
195
+ assert user.errors.empty?, user.errors.inspect
196
+ assert user.valid?, user.inspect
197
+
198
+ user2 = UserEmailUniq.create :email => 'test@example.com'
199
+ assert_not_nil user2.errors, user2.errors.inspect
200
+ assert !user2.valid?, user2.inspect
201
+
202
+ assert_equal(1, User.count)
203
+
204
+ 2.times do
205
+ user = UserEmail.create :email => 'test@example.com'
206
+ assert user.errors.empty?, user.errors.inspect
207
+ assert user.valid?, user.inspect
208
+ end
209
+
210
+ assert_equal(3, User.count)
211
+ end
212
+
213
+ def test_bad_email_with_custom_message
214
+ user = UserEmailCustom.create :email => 'test AT example.com'
215
+ assert_not_nil user.errors, user.errors.inspect
216
+ assert !user.valid?, user.inspect
217
+ assert_equal("must be valid!", user.errors['email'].to_s)
218
+ end
219
+
220
+ def test_multiple_emails
221
+ user = UserMultipleEmail.create :email => 'test@example.com', :email2 => 'test2@example.com'
222
+ assert user.errors.empty?, user.errors.inspect
223
+ assert user.valid?, user.inspect
224
+
225
+ user = UserMultipleEmail.create :email => 'test AT example.com', :email2 => 'test3@example.com'
226
+ assert_not_nil user.errors, user.errors.inspect
227
+ assert !user.valid?, user.inspect
228
+
229
+ user = UserMultipleEmail.create :email2 => 'test4@example.com'
230
+ assert_not_nil user.errors, user.errors.inspect
231
+ assert !user.valid?, user.inspect
232
+ end
233
+
234
+ def test_multiple_emails_with_allow_blank
235
+ user = UserMultipleEmailAllowBlank.create :email => 'test@example.com', :email2 => 'test2@example.com'
236
+ assert user.errors.empty?, user.errors.inspect
237
+ assert user.valid?, user.inspect
238
+
239
+ user = UserMultipleEmailAllowBlank.create :email => 'test AT example.com', :email2 => 'test3@example.com'
240
+ assert_not_nil user.errors, user.errors.inspect
241
+ assert !user.valid?, user.inspect
242
+
243
+ user = UserMultipleEmailAllowBlank.create :email2 => 'test4@example.com'
244
+ assert user.errors.empty?, user.errors.inspect
245
+ assert user.valid?, user.inspect
246
+ end
247
+
248
+ def test_without_email_with_allow_blank
249
+ user = UserEmailAllowBlank.create
250
+ assert user.errors.empty?, user.errors.inspect
251
+ assert user.valid?, user.inspect
252
+ end
253
+
254
+ def test_without_email_with_allow_nil
255
+ user = UserEmailAllowNil.create
256
+ assert user.errors.empty?, user.errors.inspect
257
+ assert user.valid?, user.inspect
258
+
259
+ user = UserEmailAllowNil.create(:email => '')
260
+ assert_not_nil user.errors, user.errors.inspect
261
+ assert !user.valid?, user.inspect
262
+ end
263
+
264
+ def test_good_email_with_allow_blank
265
+ user = UserEmailAllowBlank.create :email => 'test@example.com'
266
+ assert user.errors.empty?, user.errors.inspect
267
+ assert user.valid?, user.inspect
268
+ end
269
+
270
+ def test_bad_email_with_allow_blank
271
+ user = UserEmailAllowBlank.create :email => 'test AT example.com'
272
+ assert_not_nil user.errors, user.errors.inspect
273
+ assert !user.valid?, user.inspect
274
+ end
275
+
276
+ def test_email_with_if
277
+ user = UserEmailIf.create(:email => 'test@example.com')
278
+ assert user.errors.empty?, user.errors.inspect
279
+ assert user.valid?, user.inspect
280
+
281
+ user = UserEmailIf.create(:email => 'tc.com')
282
+ assert user.errors.empty?, user.errors.inspect
283
+ assert user.valid?, user.inspect
284
+ end
285
+
286
+ def test_email_with_unless
287
+ user = UserEmailIf.create(:email => 'test@example.com')
288
+ assert user.errors.empty?, user.errors.inspect
289
+ assert user.valid?, user.inspect
290
+
291
+ user = UserEmailIf.create(:email => 'tc.com')
292
+ assert user.errors.empty?, user.errors.inspect
293
+ assert user.valid?, user.inspect
294
+ end
295
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar-validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Cavigneaux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-24 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: This ActiveRecord extension provides more validation schemes. There are some useful validation missing added by this plugin to fill the gap.
26
+ email: nico@bounga.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - Rakefile
35
+ - init.rb
36
+ - README
37
+ - LICENSE
38
+ - lib/ar-validations.rb
39
+ - tasks/validations_tasks.rake
40
+ - test/test_helper.rb
41
+ - test/validations_test.rb
42
+ has_rdoc: true
43
+ homepage: http://www.bitbucket.org/Bounga/ar-validations
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project: ar-validations
64
+ rubygems_version: 1.3.1
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: An ActiveRecord extension provides more validation schemes
68
+ test_files:
69
+ - test/validations_test.rb