date_validator 0.7.0 → 0.7.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.
- checksums.yaml +4 -4
- data/.travis.yml +4 -2
- data/Gemfile +18 -0
- data/Rakefile +2 -2
- data/Readme.md +16 -10
- data/date_validator.gemspec +1 -1
- data/lib/active_model/validations/date_validator.rb +18 -15
- data/lib/date_validator.rb +2 -2
- data/lib/date_validator/version.rb +1 -1
- data/locales/en.yml +1 -0
- data/locales/it.yml +8 -0
- data/locales/pt-BR.yml +8 -0
- data/locales/ru.yml +1 -1
- data/test/date_validator_test.rb +22 -21
- data/test/test_helper.rb +3 -2
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10530abca992905ef4966abc59bde16277badc80
|
4
|
+
data.tar.gz: ec91b26e1d5c4ec2f2a021da33b785e4c60d5371
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33d51b09b3101e09ceb24e72fdab255c32be4e7ee766610163bbaaac793b7ab5d64c12a27a693642238e290c233e673d34a34c60a83603d75ffbadd70035c589
|
7
|
+
data.tar.gz: 0bb7e42f9e6bd99894952e29c5e5b1cffa388bb643cbe896b069097a36b74d3022bfe51855d372509b0ee32022da3851d7f71bf6f7e5eb3d0b852fcdd4d06ff0
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,3 +1,21 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
3
|
gemspec
|
4
|
+
|
5
|
+
active_model_version = ENV['ACTIVE_MODEL_VERSION'] || 'default'
|
6
|
+
|
7
|
+
active_model_opts =
|
8
|
+
case active_model_version
|
9
|
+
when 'master'
|
10
|
+
{ github: 'rails/rails' }
|
11
|
+
when 'default'
|
12
|
+
'~> 3'
|
13
|
+
else
|
14
|
+
"~> #{active_model_version}"
|
15
|
+
end
|
16
|
+
|
17
|
+
gem 'activemodel', active_model_opts
|
18
|
+
|
19
|
+
platforms :rbx do
|
20
|
+
gem 'rubysl', '~> 2.0'
|
21
|
+
end
|
data/Rakefile
CHANGED
data/Readme.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# date_validator [](https://travis-ci.org/codegram/date_validator)
|
2
2
|
|
3
3
|
|
4
|
-
A simple date validator for Rails
|
4
|
+
A simple date validator for Rails. Should be compatible with all latest Rubies.
|
5
5
|
|
6
6
|
|
7
7
|
```shell
|
@@ -10,10 +10,16 @@ $ gem install date_validator
|
|
10
10
|
|
11
11
|
And I mean simple. In your model:
|
12
12
|
|
13
|
+
```ruby
|
14
|
+
validates :expiration_date, date: true
|
15
|
+
```
|
16
|
+
|
17
|
+
or with some options, such as:
|
18
|
+
|
13
19
|
```ruby
|
14
20
|
validates :expiration_date,
|
15
|
-
:
|
16
|
-
|
21
|
+
date: { after: Proc.new { Time.now },
|
22
|
+
before: Proc.new { Time.now + 1.year } }
|
17
23
|
# Using Proc.new prevents production cache issues
|
18
24
|
```
|
19
25
|
|
@@ -23,30 +29,30 @@ a Symbol instead of a block:
|
|
23
29
|
```ruby
|
24
30
|
# Ensure the expiration date is after the packaging date
|
25
31
|
validates :expiration_date,
|
26
|
-
:
|
32
|
+
date: { after: :packaging_date }
|
27
33
|
```
|
28
34
|
|
29
35
|
For now the available options you can use are `:after`, `:before`,
|
30
|
-
`:after_or_equal_to` and `:
|
36
|
+
`:after_or_equal_to`, `:before_or_equal_to` and `:equal_to`.
|
31
37
|
|
32
38
|
If you want to specify a custom message, you can do so in the options hash:
|
33
39
|
|
34
40
|
```ruby
|
35
41
|
validates :start_date,
|
36
|
-
:
|
37
|
-
:
|
42
|
+
date: { after: Proc.new { Date.today }, message: 'must be after today' },
|
43
|
+
on: :create
|
38
44
|
```
|
39
45
|
|
40
|
-
Pretty much self-explanatory! :)
|
46
|
+
Pretty much self-explanatory! :)
|
41
47
|
|
42
48
|
If you want to make sure an attribute is before/after another attribute, use:
|
43
49
|
|
44
50
|
```ruby
|
45
|
-
validates :start_date, :
|
51
|
+
validates :start_date, date: { before: :end_date }
|
46
52
|
```
|
47
53
|
|
48
54
|
## Note on Patches/Pull Requests
|
49
|
-
|
55
|
+
|
50
56
|
* Fork the project.
|
51
57
|
* Make your feature addition or bug fix.
|
52
58
|
* Add tests for it. This is important so I don't break it in a
|
data/date_validator.gemspec
CHANGED
@@ -13,13 +13,15 @@ module ActiveModel
|
|
13
13
|
class DateValidator < ActiveModel::EachValidator
|
14
14
|
|
15
15
|
# Implemented checks and their associated operators.
|
16
|
-
CHECKS = { :
|
17
|
-
:
|
16
|
+
CHECKS = { after: :>, after_or_equal_to: :>=,
|
17
|
+
before: :<, before_or_equal_to: :<=,
|
18
|
+
equal_to: :==
|
19
|
+
}.freeze
|
18
20
|
|
19
21
|
# Call `#initialize` on the superclass, adding a default
|
20
|
-
#
|
22
|
+
# `allow_nil: false` option.
|
21
23
|
def initialize(options)
|
22
|
-
super(options.reverse_merge(:
|
24
|
+
super(options.reverse_merge(allow_nil: false))
|
23
25
|
end
|
24
26
|
|
25
27
|
# Validates the arguments passed to the validator.
|
@@ -65,29 +67,29 @@ module ActiveModel
|
|
65
67
|
record.errors.add(attr_name, :not_a_date, options)
|
66
68
|
return
|
67
69
|
end
|
68
|
-
|
70
|
+
|
69
71
|
options.slice(*CHECKS.keys).each do |option, option_value|
|
70
72
|
option_value = option_value.call(record) if option_value.is_a?(Proc)
|
71
73
|
option_value = record.send(option_value) if option_value.is_a?(Symbol)
|
72
|
-
|
74
|
+
|
73
75
|
original_value = value
|
74
76
|
original_option_value = option_value
|
75
77
|
|
76
78
|
# To enable to_i conversion, these types must be converted to Datetimes
|
77
79
|
if defined?(ActiveSupport::TimeWithZone)
|
78
|
-
option_value = option_value.to_datetime if option_value.is_a?(ActiveSupport::TimeWithZone)
|
79
|
-
value = value.to_datetime if value.is_a?(ActiveSupport::TimeWithZone)
|
80
|
+
option_value = option_value.to_datetime if option_value.is_a?(ActiveSupport::TimeWithZone)
|
81
|
+
value = value.to_datetime if value.is_a?(ActiveSupport::TimeWithZone)
|
80
82
|
end
|
81
83
|
|
82
84
|
if defined?(Date)
|
83
|
-
option_value = option_value.to_datetime if option_value.is_a?(Date)
|
84
|
-
value = value.to_datetime if value.is_a?(Date)
|
85
|
+
option_value = option_value.to_datetime if option_value.is_a?(Date)
|
86
|
+
value = value.to_datetime if value.is_a?(Date)
|
85
87
|
end
|
86
|
-
|
88
|
+
|
87
89
|
unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
|
88
90
|
record.errors.add(attr_name, option, options.merge(
|
89
|
-
:
|
90
|
-
:
|
91
|
+
value: original_value,
|
92
|
+
date: (I18n.localize(original_option_value) rescue original_option_value)
|
91
93
|
))
|
92
94
|
end
|
93
95
|
end
|
@@ -104,8 +106,8 @@ module ActiveModel
|
|
104
106
|
# Validates whether the value of the specified attribute is a validate Date
|
105
107
|
#
|
106
108
|
# class Person < ActiveRecord::Base
|
107
|
-
# validates_date_of :payment_date, :
|
108
|
-
# validates_date_of :expiration_date, :
|
109
|
+
# validates_date_of :payment_date, after: :packaging_date
|
110
|
+
# validates_date_of :expiration_date, before: Proc.new { Time.now }
|
109
111
|
# end
|
110
112
|
#
|
111
113
|
# Configuration options:
|
@@ -113,6 +115,7 @@ module ActiveModel
|
|
113
115
|
# * <tt>:before</tt> - check that a Date is before the specified one.
|
114
116
|
# * <tt>:after_or_equal_to</tt> - check that a Date is after or equal to the specified one.
|
115
117
|
# * <tt>:before_or_equal_to</tt> - check that a Date is before or equal to the specified one.
|
118
|
+
# * <tt>:equal_to</tt> - check that a Date is equal to the specified one.
|
116
119
|
def validates_date_of(*attr_names)
|
117
120
|
validates_with DateValidator, _merge_attributes(attr_names)
|
118
121
|
end
|
data/lib/date_validator.rb
CHANGED
@@ -5,8 +5,8 @@ require 'active_support/i18n'
|
|
5
5
|
#
|
6
6
|
# @example
|
7
7
|
# validates :expiration_date,
|
8
|
-
# :
|
9
|
-
#
|
8
|
+
# date: { after: Proc.new { Time.now },
|
9
|
+
# before: Proc.new { Time.now + 1.year } }
|
10
10
|
# # Using Proc.new prevents production cache issues
|
11
11
|
#
|
12
12
|
module DateValidator
|
data/locales/en.yml
CHANGED
data/locales/it.yml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
it:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
not_a_date: "non è una data"
|
5
|
+
after: "deve essere successiva a %{date}"
|
6
|
+
after_or_equal_to: "deve essere successiva o uguale a %{date}"
|
7
|
+
before: "deve essere antecedente a %{date}"
|
8
|
+
before_or_equal_to: "deve essere antecedente o uguale a %{date}"
|
data/locales/pt-BR.yml
ADDED
data/locales/ru.yml
CHANGED
data/test/date_validator_test.rb
CHANGED
@@ -12,7 +12,7 @@ module ActiveModel
|
|
12
12
|
it "checks validity of the arguments" do
|
13
13
|
[3, "foo", 1..6].each do |wrong_argument|
|
14
14
|
proc {
|
15
|
-
TestRecord.validates(:expiration_date, :
|
15
|
+
TestRecord.validates(:expiration_date, date: { before: wrong_argument })
|
16
16
|
}.must_raise(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc")
|
17
17
|
end
|
18
18
|
end
|
@@ -20,7 +20,7 @@ module ActiveModel
|
|
20
20
|
it "complains when no options are provided" do
|
21
21
|
I18n.backend.reload!
|
22
22
|
TestRecord.validates :expiration_date,
|
23
|
-
:
|
23
|
+
date: { before: Time.now }
|
24
24
|
|
25
25
|
model = TestRecord.new(nil)
|
26
26
|
model.valid?.must_equal false
|
@@ -29,7 +29,7 @@ module ActiveModel
|
|
29
29
|
|
30
30
|
it "works with helper methods" do
|
31
31
|
time = Time.now
|
32
|
-
TestRecord.validates_date_of :expiration_date, :
|
32
|
+
TestRecord.validates_date_of :expiration_date, before: time
|
33
33
|
model = TestRecord.new(time + 20000)
|
34
34
|
model.valid?.must_equal false
|
35
35
|
end
|
@@ -38,19 +38,20 @@ module ActiveModel
|
|
38
38
|
_context = must_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
|
39
39
|
|
40
40
|
describe _context do
|
41
|
-
[:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
|
41
|
+
[:after, :before, :after_or_equal_to, :before_or_equal_to, :equal_to].each do |check|
|
42
42
|
now = Time.now.to_datetime
|
43
43
|
|
44
44
|
model_date = case check
|
45
|
-
when :after
|
46
|
-
when :before
|
47
|
-
when :after_or_equal_to
|
45
|
+
when :after then must_be == :valid ? now + 21000 : now - 1
|
46
|
+
when :before then must_be == :valid ? now - 21000 : now + 1
|
47
|
+
when :after_or_equal_to then must_be == :valid ? now : now - 21000
|
48
48
|
when :before_or_equal_to then must_be == :valid ? now : now + 21000
|
49
|
+
when :equal_to then must_be == :valid ? now : now + 21000
|
49
50
|
end
|
50
51
|
|
51
52
|
it "ensures that an attribute is #{must_be} when #{must_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
|
52
53
|
TestRecord.validates :expiration_date,
|
53
|
-
:
|
54
|
+
date: {:"#{check}" => now}
|
54
55
|
|
55
56
|
model = TestRecord.new(model_date)
|
56
57
|
must_be == :valid ? model.valid?.must_equal(true) : model.valid?.must_equal(false)
|
@@ -59,7 +60,7 @@ module ActiveModel
|
|
59
60
|
if _context == 'when value does not match validation requirements'
|
60
61
|
it "yields a default error message indicating that value must be #{check} validation requirements" do
|
61
62
|
TestRecord.validates :expiration_date,
|
62
|
-
:
|
63
|
+
date: {:"#{check}" => now}
|
63
64
|
|
64
65
|
model = TestRecord.new(model_date)
|
65
66
|
model.valid?.must_equal false
|
@@ -73,8 +74,8 @@ module ActiveModel
|
|
73
74
|
|
74
75
|
it "allows for a custom validation message" do
|
75
76
|
TestRecord.validates :expiration_date,
|
76
|
-
:
|
77
|
-
|
77
|
+
date: { before_or_equal_to: now,
|
78
|
+
message: 'must be after Christmas' }
|
78
79
|
|
79
80
|
model = TestRecord.new(now + 21000)
|
80
81
|
model.valid?.must_equal false
|
@@ -83,9 +84,9 @@ module ActiveModel
|
|
83
84
|
|
84
85
|
it "allows custom validation message to be handled by I18n" do
|
85
86
|
custom_message = 'Custom Date Message'
|
86
|
-
I18n.backend.store_translations('en', {:
|
87
|
+
I18n.backend.store_translations('en', { errors: { messages: { not_a_date: custom_message }}})
|
87
88
|
|
88
|
-
TestRecord.validates :expiration_date, :
|
89
|
+
TestRecord.validates :expiration_date, date: true
|
89
90
|
|
90
91
|
model = TestRecord.new(nil)
|
91
92
|
model.valid?.must_equal false
|
@@ -104,22 +105,22 @@ module ActiveModel
|
|
104
105
|
it "accepts a #{type} as an argument to a check" do
|
105
106
|
case type
|
106
107
|
when :proc then
|
107
|
-
TestRecord.validates(:expiration_date, :
|
108
|
+
TestRecord.validates(:expiration_date, date: { after: Proc.new {Time.now + 21000} }).must_be_kind_of Hash
|
108
109
|
when :symbol then
|
109
110
|
TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
|
110
|
-
TestRecord.validates(:expiration_date, :
|
111
|
+
TestRecord.validates(:expiration_date, date: { after: :min_date }).must_be_kind_of Hash
|
111
112
|
when :date then
|
112
|
-
TestRecord.validates(:expiration_date, :
|
113
|
+
TestRecord.validates(:expiration_date, date: { after: Time.now.to_date }).must_be_kind_of Hash
|
113
114
|
when :time_with_zone then
|
114
115
|
Time.zone = "Hawaii"
|
115
|
-
TestRecord.validates(:expiration_date, :
|
116
|
+
TestRecord.validates(:expiration_date, date: { before: Time.zone.parse((Time.now + 21000).to_s) }).must_be_kind_of Hash
|
116
117
|
end
|
117
118
|
end
|
118
119
|
end
|
119
120
|
|
120
121
|
it "gracefully handles an unexpected result from a proc argument evaluation" do
|
121
122
|
TestRecord.validates :expiration_date,
|
122
|
-
:
|
123
|
+
date: { after: Proc.new { nil } }
|
123
124
|
|
124
125
|
TestRecord.new(Time.now).valid?.must_equal false
|
125
126
|
end
|
@@ -127,7 +128,7 @@ module ActiveModel
|
|
127
128
|
it "gracefully handles an unexpected result from a symbol argument evaluation" do
|
128
129
|
TestRecord.send(:define_method, :min_date, lambda { nil })
|
129
130
|
TestRecord.validates :expiration_date,
|
130
|
-
:
|
131
|
+
date: { after: :min_date }
|
131
132
|
|
132
133
|
TestRecord.new(Time.now).valid?.must_equal false
|
133
134
|
end
|
@@ -138,12 +139,12 @@ module ActiveModel
|
|
138
139
|
end
|
139
140
|
|
140
141
|
it "should detect invalid date expressions when nil is allowed" do
|
141
|
-
TestRecord.validates(:expiration_date, :
|
142
|
+
TestRecord.validates(:expiration_date, date: true, allow_nil: true)
|
142
143
|
TestRecord.new(nil).valid?.must_equal false
|
143
144
|
end
|
144
145
|
|
145
146
|
it "should detect invalid date expressions when blank is allowed" do
|
146
|
-
TestRecord.validates(:expiration_date, :
|
147
|
+
TestRecord.validates(:expiration_date, date: true, allow_blank: true)
|
147
148
|
TestRecord.new(nil).valid?.must_equal false
|
148
149
|
end
|
149
150
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
begin
|
2
|
-
require 'simplecov'
|
2
|
+
require 'simplecov'
|
3
3
|
SimpleCov.start do
|
4
4
|
add_group "Lib", 'lib'
|
5
5
|
end
|
@@ -8,9 +8,10 @@ end
|
|
8
8
|
|
9
9
|
begin; require 'turn'; rescue LoadError; end
|
10
10
|
|
11
|
-
|
11
|
+
gem 'minitest'
|
12
12
|
require 'minitest/autorun'
|
13
13
|
|
14
|
+
require 'active_support/core_ext'
|
14
15
|
require 'active_support/time' # For testing Date and TimeWithZone objects
|
15
16
|
|
16
17
|
require 'active_model'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oriol Gual
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2014-10-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
@@ -18,14 +18,14 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
21
|
+
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - '>='
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '
|
28
|
+
version: '0'
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: minitest
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,8 +110,10 @@ files:
|
|
110
110
|
- locales/en.yml
|
111
111
|
- locales/es.yml
|
112
112
|
- locales/fr.yml
|
113
|
+
- locales/it.yml
|
113
114
|
- locales/nl.yml
|
114
115
|
- locales/pl.yml
|
116
|
+
- locales/pt-BR.yml
|
115
117
|
- locales/ru.yml
|
116
118
|
- test/date_validator_test.rb
|
117
119
|
- test/test_helper.rb
|
@@ -134,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
136
|
version: '0'
|
135
137
|
requirements: []
|
136
138
|
rubyforge_project: date_validator
|
137
|
-
rubygems_version: 2.0.
|
139
|
+
rubygems_version: 2.0.14
|
138
140
|
signing_key:
|
139
141
|
specification_version: 4
|
140
142
|
summary: A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based
|
@@ -142,3 +144,4 @@ summary: A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3,
|
|
142
144
|
test_files:
|
143
145
|
- test/date_validator_test.rb
|
144
146
|
- test/test_helper.rb
|
147
|
+
has_rdoc:
|