more_validators 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -4
- data/lib/more_validators.rb +1 -0
- data/lib/validates_as_age.rb +27 -0
- data/test/validates_as_age_test.rb +45 -0
- metadata +6 -3
data/README.rdoc
CHANGED
@@ -10,14 +10,15 @@ The latest version of this library provides the following validation methods:
|
|
10
10
|
* validates_as_email: email address validation;
|
11
11
|
* validates_as_phonenumber_br: brazilian phone number validation;
|
12
12
|
* validates_as_uf_br: brazilian state validation;
|
13
|
-
* validates_as_website: website URL validation
|
13
|
+
* validates_as_website: website URL validation;
|
14
|
+
* validates_as_age: age validation for date fields.
|
14
15
|
|
15
16
|
Contact us if you have any questions:
|
16
17
|
contato@infosimples.com.br
|
17
18
|
|
18
19
|
== Latest version:
|
19
20
|
|
20
|
-
Version: 0.2.
|
21
|
+
Version: 0.2.1
|
21
22
|
|
22
23
|
== Installation:
|
23
24
|
|
@@ -39,9 +40,9 @@ You can install MoreValidators with:
|
|
39
40
|
|
40
41
|
gem install more_validators --version=0.1.3
|
41
42
|
|
42
|
-
You can add it to your
|
43
|
+
You can add it to your config/environment.rb:
|
43
44
|
|
44
|
-
gem 'more_validators', '0.1.3'
|
45
|
+
config.gem 'more_validators', :lib => 'more_validators', :version => '0.1.3'
|
45
46
|
|
46
47
|
== Usage:
|
47
48
|
|
@@ -67,6 +68,10 @@ In your model file do something like:
|
|
67
68
|
# Brazilian UF validation
|
68
69
|
# Both upcase and downcase are allowed by default
|
69
70
|
validates_as_uf_br :uf
|
71
|
+
|
72
|
+
# Age validation
|
73
|
+
# :min_age and :max_age are both optional and the default values are 18.year and 100.year, respectively
|
74
|
+
validates_as_age :birthday, :min_age => 10.year, :max_age => 50.year
|
70
75
|
|
71
76
|
end
|
72
77
|
|
data/lib/more_validators.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
|
4
|
+
class AgeValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
# Default agen range.
|
7
|
+
options[:min_age] ||= 18.year
|
8
|
+
options[:max_age] ||= 100.year
|
9
|
+
|
10
|
+
if value.is_a?(Date) and options[:min_age] <= options[:max_age]
|
11
|
+
# Checks if value is inside the given date range.
|
12
|
+
return if ((Time.now.to_date - options[:max_age])..(Time.now.to_date - options[:min_age])) === value.to_date
|
13
|
+
end
|
14
|
+
|
15
|
+
record.errors.add(attribute,
|
16
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
17
|
+
options.merge(:value => value))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def validates_as_age(*attr_names)
|
23
|
+
validates_with AgeValidator, _merge_attributes(attr_names)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'i18n'
|
7
|
+
require 'validates_as_age'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_record'
|
11
|
+
ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
|
12
|
+
require 'i18n'
|
13
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_age'
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestRecord < ActiveRecord::Base
|
17
|
+
def self.columns; []; end
|
18
|
+
attr_accessor :birthday
|
19
|
+
validates_as_age :birthday, :min_age => 18.year, :max_age => 78.year
|
20
|
+
end
|
21
|
+
|
22
|
+
class ValidatesAsAgeTest < Test::Unit::TestCase
|
23
|
+
def test_illegal_age
|
24
|
+
values = [
|
25
|
+
nil,
|
26
|
+
Time.now.to_date - 10.year,
|
27
|
+
Time.now.to_date - 83.year
|
28
|
+
]
|
29
|
+
values.each do |value|
|
30
|
+
assert !TestRecord.new(:birthday => value).valid?, "#{value} should be illegal."
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_legal_age
|
35
|
+
values = [
|
36
|
+
Time.now.to_date - 18.year,
|
37
|
+
Time.now.to_date - 22.year,
|
38
|
+
Time.now.to_date - 78.year
|
39
|
+
]
|
40
|
+
|
41
|
+
values.each do |value|
|
42
|
+
assert TestRecord.new(:birthday => value).valid?, "#{value} should be legal."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Rafael Barbolo Lopes
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-05-12 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/validates_as_phonenumber_br.rb
|
44
44
|
- lib/validates_as_website.rb
|
45
45
|
- lib/validates_as_uf_br.rb
|
46
|
+
- lib/validates_as_age.rb
|
46
47
|
- test/validates_as_cep_test.rb
|
47
48
|
- test/validates_as_cnpj_test.rb
|
48
49
|
- test/validates_as_cpf_test.rb
|
@@ -50,6 +51,7 @@ files:
|
|
50
51
|
- test/validates_as_phonenumber_br_test.rb
|
51
52
|
- test/validates_as_website_test.rb
|
52
53
|
- test/validates_as_uf_br_test.rb
|
54
|
+
- test/validates_as_age_test.rb
|
53
55
|
has_rdoc: true
|
54
56
|
homepage: http://github.com/infosimples/more_validators
|
55
57
|
licenses: []
|
@@ -90,3 +92,4 @@ test_files:
|
|
90
92
|
- test/validates_as_phonenumber_br_test.rb
|
91
93
|
- test/validates_as_website_test.rb
|
92
94
|
- test/validates_as_uf_br_test.rb
|
95
|
+
- test/validates_as_age_test.rb
|