date_validator 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/Gemfile.lock +1 -1
- data/LICENSE +8 -0
- data/Readme.md +27 -6
- data/date_validator.gemspec +1 -1
- data/lib/active_model/validations/date_validator.rb +23 -2
- data/lib/date_validator/version.rb +1 -1
- data/locales/nl.yml +8 -0
- data/locales/ru.yml +8 -0
- data/spec/date_validator_spec.rb +14 -5
- metadata +10 -6
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm
|
1
|
+
rvm use ruby-1.9.2@date_validator --create
|
data/Gemfile.lock
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright (c) 2011 Codegram
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
data/Readme.md
CHANGED
@@ -3,19 +3,40 @@
|
|
3
3
|
A simple date validator for Rails 3. Compatible with Ruby 1.8.7, 1.9.2 and
|
4
4
|
Rubinius 1.2.2.
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
```shell
|
7
|
+
$ gem sources -a http://gemcutter.org/
|
8
|
+
$ gem install date_validator
|
9
|
+
```
|
8
10
|
|
9
11
|
And I mean simple. In your model:
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
```ruby
|
14
|
+
validates :expiration_date,
|
15
|
+
:date => {:after => Proc.new { Time.now },
|
16
|
+
:before => Proc.new { Time.now + 1.year } }
|
17
|
+
# Using Proc.new prevents production cache issues
|
18
|
+
```
|
19
|
+
|
20
|
+
If you want to check the date against another attribute, you can pass it
|
21
|
+
a Symbol instead of a block:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# Ensure the expiration date is after the packaging date
|
25
|
+
validates :expiration_date,
|
26
|
+
:date => {:after => :packaging_date}
|
27
|
+
```
|
15
28
|
|
16
29
|
For now the available options you can use are `:after`, `:before`,
|
17
30
|
`:after_or_equal_to` and `:before_or_equal_to`.
|
18
31
|
|
32
|
+
If you want to specify a custom message, you can do so in the options hash:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
validates :start_date,
|
36
|
+
:date => {:after => Date.today, :message => 'must be after today'},
|
37
|
+
:on => :create
|
38
|
+
```
|
39
|
+
|
19
40
|
Pretty much self-explanatory! :)
|
20
41
|
|
21
42
|
## Note on Patches/Pull Requests
|
data/date_validator.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.rubyforge_project = "date_validator"
|
16
16
|
|
17
|
-
s.add_runtime_dependency 'activemodel', '
|
17
|
+
s.add_runtime_dependency 'activemodel', ['>= 3.0.0', '< 3.2.0']
|
18
18
|
|
19
19
|
s.add_development_dependency 'rspec', '~> 2.5.0'
|
20
20
|
s.add_development_dependency 'simplecov'
|
@@ -24,7 +24,7 @@ module ActiveModel
|
|
24
24
|
|
25
25
|
# Validates the arguments passed to the validator.
|
26
26
|
#
|
27
|
-
# They must be either any kind of
|
27
|
+
# They must be either any kind of Time, a Proc, or a Symbol.
|
28
28
|
def check_validity!
|
29
29
|
keys = CHECKS.keys
|
30
30
|
options.slice(*keys).each do |option, value|
|
@@ -62,7 +62,10 @@ module ActiveModel
|
|
62
62
|
end
|
63
63
|
|
64
64
|
unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
|
65
|
-
record.errors.add(attr_name, option, options.merge(
|
65
|
+
record.errors.add(attr_name, option, options.merge(
|
66
|
+
:value => original_value,
|
67
|
+
:date => (I18n.localize(original_option_value) rescue original_option_value)
|
68
|
+
))
|
66
69
|
end
|
67
70
|
end
|
68
71
|
end
|
@@ -73,5 +76,23 @@ module ActiveModel
|
|
73
76
|
object.is_a?(Time) || (defined?(Date) and object.is_a?(Date)) || (defined?(ActiveSupport::TimeWithZone) and object.is_a?(ActiveSupport::TimeWithZone))
|
74
77
|
end
|
75
78
|
end
|
79
|
+
|
80
|
+
module HelperMethods
|
81
|
+
# Validates whether the value of the specified attribute is a validate Date
|
82
|
+
#
|
83
|
+
# class Person < ActiveRecord::Base
|
84
|
+
# validates_date_of :payment_date, :after => :packaging_date
|
85
|
+
# validates_date_of :expiration_date, :before => Proc.new { Time.now }
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# Configuration options:
|
89
|
+
# * <tt>:after</tt> - check that a Date is after the specified one.
|
90
|
+
# * <tt>:before</tt> - check that a Date is before the specified one.
|
91
|
+
# * <tt>:after_or_equal_to</tt> - check that a Date is after or equal to the specified one.
|
92
|
+
# * <tt>:before_or_equal_to</tt> - check that a Date is before or equal to the specified one.
|
93
|
+
def validates_date_of(*attr_names)
|
94
|
+
validates_with DateValidator, _merge_attributes(attr_names)
|
95
|
+
end
|
96
|
+
end
|
76
97
|
end
|
77
98
|
end
|
data/locales/nl.yml
ADDED
data/locales/ru.yml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
ru:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
not_a_date: "неверный формат"
|
5
|
+
after: "должна быть позднее чем %{date}"
|
6
|
+
after_or_equal_to: "должна равняться или быть позднее чем %{date}"
|
7
|
+
before: "должна быть ранее чем %{date}"
|
8
|
+
before_or_equal_to: "должна равняться или быть ранее чем %{date}"
|
data/spec/date_validator_spec.rb
CHANGED
@@ -27,6 +27,13 @@ module ActiveModel
|
|
27
27
|
model.errors[:expiration_date].should eq(["is not a date"])
|
28
28
|
end
|
29
29
|
|
30
|
+
it "works with helper methods" do
|
31
|
+
time = Time.now
|
32
|
+
TestRecord.validates_date_of :expiration_date, :before => time
|
33
|
+
model = TestRecord.new(time + 20000)
|
34
|
+
model.should_not be_valid
|
35
|
+
end
|
36
|
+
|
30
37
|
[:valid,:invalid].each do |should_be|
|
31
38
|
_context = should_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
|
32
39
|
|
@@ -43,7 +50,7 @@ module ActiveModel
|
|
43
50
|
|
44
51
|
it "ensures that an attribute is #{should_be} when #{should_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
|
45
52
|
TestRecord.validates :expiration_date,
|
46
|
-
:date => {:"#{check}" =>
|
53
|
+
:date => {:"#{check}" => now}
|
47
54
|
|
48
55
|
model = TestRecord.new(model_date)
|
49
56
|
should_be == :valid ? model.should(be_valid, "an attribute should be valid when respecting the #{check} check")\
|
@@ -53,22 +60,24 @@ module ActiveModel
|
|
53
60
|
if _context == 'when value does not match validation requirements'
|
54
61
|
it "yields a default error message indicating that value must be #{check} validation requirements" do
|
55
62
|
TestRecord.validates :expiration_date,
|
56
|
-
:date => {:"#{check}" =>
|
63
|
+
:date => {:"#{check}" => now}
|
57
64
|
|
58
65
|
model = TestRecord.new(model_date)
|
59
66
|
model.should_not be_valid
|
60
|
-
model.errors[:expiration_date].should eq(["must be " + check.to_s.gsub('_',' ') + " #{
|
67
|
+
model.errors[:expiration_date].should eq(["must be " + check.to_s.gsub('_',' ') + " #{I18n.localize(now)}"])
|
61
68
|
end
|
62
69
|
end
|
63
70
|
end
|
64
71
|
|
65
72
|
if _context == 'when value does not match validation requirements'
|
73
|
+
now = Time.now.to_datetime
|
74
|
+
|
66
75
|
it "allows for a custom validation message" do
|
67
76
|
TestRecord.validates :expiration_date,
|
68
|
-
:date => {:before_or_equal_to =>
|
77
|
+
:date => {:before_or_equal_to => now,
|
69
78
|
:message => 'must be after Christmas'}
|
70
79
|
|
71
|
-
model = TestRecord.new(
|
80
|
+
model = TestRecord.new(now + 21000)
|
72
81
|
model.should_not be_valid
|
73
82
|
model.errors[:expiration_date].should eq(["must be after Christmas"])
|
74
83
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: date_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.6.
|
5
|
+
version: 0.6.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Oriol Gual
|
@@ -12,8 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2011-
|
16
|
-
default_executable:
|
15
|
+
date: 2011-06-08 00:00:00 Z
|
17
16
|
dependencies:
|
18
17
|
- !ruby/object:Gem::Dependency
|
19
18
|
name: activemodel
|
@@ -21,9 +20,12 @@ dependencies:
|
|
21
20
|
requirement: &id001 !ruby/object:Gem::Requirement
|
22
21
|
none: false
|
23
22
|
requirements:
|
24
|
-
- -
|
23
|
+
- - ">="
|
25
24
|
- !ruby/object:Gem::Version
|
26
25
|
version: 3.0.0
|
26
|
+
- - <
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.2.0
|
27
29
|
type: :runtime
|
28
30
|
version_requirements: *id001
|
29
31
|
- !ruby/object:Gem::Dependency
|
@@ -109,6 +111,7 @@ files:
|
|
109
111
|
- .rvmrc
|
110
112
|
- Gemfile
|
111
113
|
- Gemfile.lock
|
114
|
+
- LICENSE
|
112
115
|
- Rakefile
|
113
116
|
- Readme.md
|
114
117
|
- date_validator.gemspec
|
@@ -118,9 +121,10 @@ files:
|
|
118
121
|
- locales/ca.yml
|
119
122
|
- locales/en.yml
|
120
123
|
- locales/es.yml
|
124
|
+
- locales/nl.yml
|
125
|
+
- locales/ru.yml
|
121
126
|
- spec/date_validator_spec.rb
|
122
127
|
- spec/spec_helper.rb
|
123
|
-
has_rdoc: true
|
124
128
|
homepage: http://github.com/codegram/date_validator
|
125
129
|
licenses: []
|
126
130
|
|
@@ -144,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
148
|
requirements: []
|
145
149
|
|
146
150
|
rubyforge_project: date_validator
|
147
|
-
rubygems_version: 1.5
|
151
|
+
rubygems_version: 1.8.5
|
148
152
|
signing_key:
|
149
153
|
specification_version: 3
|
150
154
|
summary: A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel.
|