validates_timeliness 3.0.14 → 3.0.15
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 +7 -0
- data/CHANGELOG.rdoc +5 -0
- data/README.rdoc +7 -4
- data/Rakefile +1 -1
- data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +1 -1
- data/lib/validates_timeliness/orm/mongoid.rb +6 -20
- data/lib/validates_timeliness/validator.rb +7 -1
- data/lib/validates_timeliness/version.rb +1 -1
- data/lib/validates_timeliness.rb +8 -12
- data/spec/spec_helper.rb +1 -0
- data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +1 -1
- data/spec/validates_timeliness/helper_methods_spec.rb +9 -9
- data/spec/validates_timeliness/orm/active_record_spec.rb +53 -63
- data/spec/validates_timeliness/orm/mongoid_spec.rb +51 -17
- data/spec/validates_timeliness/validator_spec.rb +16 -5
- data/validates_timeliness.gemspec +1 -1
- metadata +35 -57
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a60055680e49760bf31b2a820e30d35c125701e5
|
4
|
+
data.tar.gz: 846e163f120d01337b2e55356cc1a01fca513ea7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46b5e4bfba055d99c8e6ba8dc3e122bc8e3dc872efb35ee957802f7b3f827b92ec5efd19d64f2054f2597656b91409432a7dcc731b48090cd91603e6323e5fb4
|
7
|
+
data.tar.gz: 93ed8ea91b878022a29aee4c5ad0b0586e4c1cff6cc531168c7bfe2aab4e8a232b26c4dfe683bdc99c7d3897dc97b4d91f2c4af0d5e4dffdf90f6362aea3f581
|
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
= 3.0.15 [2015-12-29]
|
2
|
+
* Fixes mongoid 3 support and removes mongoid 2 support(johnnyshields)
|
3
|
+
* Some documentation/comments tidying
|
4
|
+
* Some general tidying up
|
5
|
+
|
1
6
|
= 3.0.14 [2012-08-23]
|
2
7
|
* Fix for using validates :timeliness => {} form to correctly add attributes to timeliness validated attributes.
|
3
8
|
|
data/README.rdoc
CHANGED
@@ -39,7 +39,7 @@ Then run
|
|
39
39
|
|
40
40
|
$ rails generate validates_timeliness:install
|
41
41
|
|
42
|
-
This creates configuration initializer and locale files. In the initializer,
|
42
|
+
This creates configuration initializer and locale files. In the initializer, there are a number of config
|
43
43
|
options to customize the plugin.
|
44
44
|
|
45
45
|
NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
|
@@ -55,7 +55,10 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea
|
|
55
55
|
validates_datetime :finish_time, :after => :start_time # Method symbol
|
56
56
|
|
57
57
|
validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
|
58
|
-
|
58
|
+
|
59
|
+
validates_time :booked_at, :between => ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM
|
60
|
+
validates_time :booked_at, :between => '9:00am'..'5:00pm' # The same as previous example
|
61
|
+
validates_time :booked_at, :between => '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM
|
59
62
|
|
60
63
|
validates_time :breakfast_time, :on_or_after => '6:00am',
|
61
64
|
:on_or_after_message => 'must be after opening time',
|
@@ -173,8 +176,8 @@ You can also use validation options for custom error messages. The following opt
|
|
173
176
|
:after_message
|
174
177
|
:on_or_after_message
|
175
178
|
|
176
|
-
Note: There is no :between_message option. The between error message should be defined using the
|
177
|
-
:
|
179
|
+
Note: There is no :between_message option. The between error message should be defined using the :on_or_after and :on_or_before
|
180
|
+
(:before in case when :between argument is a Range with excluded high value, see Examples) messages.
|
178
181
|
|
179
182
|
It is highly recommended you use the I18n system for error messages.
|
180
183
|
|
data/Rakefile
CHANGED
@@ -32,7 +32,7 @@ ValidatesTimeliness.setup do |config|
|
|
32
32
|
# Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
|
33
33
|
# config.parser.remove_formats()
|
34
34
|
#
|
35
|
-
# Change the
|
35
|
+
# Change the ambiguous year threshold when parsing a 2 digit year
|
36
36
|
# config.parser.ambiguous_year_threshold = 30
|
37
37
|
#
|
38
38
|
# Treat ambiguous dates, such as 01/02/1950, as a Non-US date.
|
@@ -6,7 +6,7 @@ module ValidatesTimeliness
|
|
6
6
|
# It is best to use the plugin parser to avoid errors on a bad
|
7
7
|
# field value in Mongoid. Parser will return nil rather than error.
|
8
8
|
|
9
|
-
module ClassMethods
|
9
|
+
module ClassMethods
|
10
10
|
public
|
11
11
|
|
12
12
|
# Mongoid has no bulk attribute method definition hook. It defines
|
@@ -23,7 +23,7 @@ module ValidatesTimeliness
|
|
23
23
|
Date => :date,
|
24
24
|
Time => :time,
|
25
25
|
DateTime => :datetime
|
26
|
-
}[fields[attr_name
|
26
|
+
}[fields[database_field_name(attr_name)].type] || :datetime
|
27
27
|
end
|
28
28
|
|
29
29
|
protected
|
@@ -33,31 +33,17 @@ module ValidatesTimeliness
|
|
33
33
|
|
34
34
|
"#{var_name} = Timeliness::Parser.parse(value, :#{type})"
|
35
35
|
end
|
36
|
-
|
37
36
|
end
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
super
|
43
|
-
end
|
38
|
+
def reload(*args)
|
39
|
+
_clear_timeliness_cache
|
40
|
+
super
|
44
41
|
end
|
45
42
|
end
|
46
43
|
end
|
47
44
|
end
|
48
|
-
|
45
|
+
|
49
46
|
module Mongoid::Document
|
50
47
|
include ValidatesTimeliness::AttributeMethods
|
51
48
|
include ValidatesTimeliness::ORM::Mongoid
|
52
|
-
|
53
|
-
# Pre-2.3 reload
|
54
|
-
if (instance_methods & ['reload', :reload]).present?
|
55
|
-
def reload_with_timeliness
|
56
|
-
_clear_timeliness_cache
|
57
|
-
reload_without_timeliness
|
58
|
-
end
|
59
|
-
alias_method_chain :reload, :timeliness
|
60
|
-
else
|
61
|
-
include ValidatesTimeliness::ORM::Mongoid::Reload
|
62
|
-
end
|
63
49
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_model'
|
1
2
|
require 'active_model/validator'
|
2
3
|
|
3
4
|
module ValidatesTimeliness
|
@@ -32,7 +33,12 @@ module ValidatesTimeliness
|
|
32
33
|
|
33
34
|
if range = options.delete(:between)
|
34
35
|
raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array)
|
35
|
-
options[:on_or_after]
|
36
|
+
options[:on_or_after] = range.first
|
37
|
+
if range.is_a?(Range) && range.exclude_end?
|
38
|
+
options[:before] = range.last
|
39
|
+
else
|
40
|
+
options[:on_or_before] = range.last
|
41
|
+
end
|
36
42
|
end
|
37
43
|
|
38
44
|
@restrictions_to_check = RESTRICTIONS.keys & options.keys
|
data/lib/validates_timeliness.rb
CHANGED
@@ -24,26 +24,24 @@ module ValidatesTimeliness
|
|
24
24
|
|
25
25
|
class << self
|
26
26
|
delegate :default_timezone, :default_timezone=, :dummy_date_for_time_type, :dummy_date_for_time_type=, :to => Timeliness
|
27
|
+
|
28
|
+
attr_accessor :extend_orms, :ignore_restriction_errors, :restriction_shorthand_symbols, :use_plugin_parser
|
27
29
|
end
|
28
30
|
|
29
31
|
# Extend ORM/ODMs for full support (:active_record, :mongoid).
|
30
|
-
|
31
|
-
@@extend_orms = []
|
32
|
+
self.extend_orms = []
|
32
33
|
|
33
34
|
# Ignore errors when restriction options are evaluated
|
34
|
-
|
35
|
-
@@ignore_restriction_errors = false
|
35
|
+
self.ignore_restriction_errors = false
|
36
36
|
|
37
37
|
# Shorthand time and date symbols for restrictions
|
38
|
-
|
39
|
-
@@restriction_shorthand_symbols = {
|
38
|
+
self.restriction_shorthand_symbols = {
|
40
39
|
:now => lambda { Time.current },
|
41
40
|
:today => lambda { Date.current }
|
42
41
|
}
|
43
42
|
|
44
43
|
# Use the plugin date/time parser which is stricter and extensible
|
45
|
-
|
46
|
-
@@use_plugin_parser = false
|
44
|
+
self.use_plugin_parser = false
|
47
45
|
|
48
46
|
# Default timezone
|
49
47
|
self.default_timezone = :utc
|
@@ -51,10 +49,6 @@ module ValidatesTimeliness
|
|
51
49
|
# Set the dummy date part for a time type values.
|
52
50
|
self.dummy_date_for_time_type = [ 2000, 1, 1 ]
|
53
51
|
|
54
|
-
def self.parser
|
55
|
-
Timeliness
|
56
|
-
end
|
57
|
-
|
58
52
|
# Setup method for plugin configuration
|
59
53
|
def self.setup
|
60
54
|
yield self
|
@@ -64,6 +58,8 @@ module ValidatesTimeliness
|
|
64
58
|
def self.load_orms
|
65
59
|
extend_orms.each {|orm| require "validates_timeliness/orm/#{orm}" }
|
66
60
|
end
|
61
|
+
|
62
|
+
def self.parser; Timeliness end
|
67
63
|
end
|
68
64
|
|
69
65
|
require 'validates_timeliness/conversion'
|
data/spec/spec_helper.rb
CHANGED
@@ -57,6 +57,7 @@ class PersonWithShim < Person
|
|
57
57
|
include TestModelShim
|
58
58
|
end
|
59
59
|
|
60
|
+
ActiveRecord::Base.default_timezone = :utc
|
60
61
|
ActiveRecord::Base.time_zone_aware_attributes = true
|
61
62
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
62
63
|
ActiveRecord::Migration.verbose = false
|
@@ -10,7 +10,7 @@ describe ValidatesTimeliness::Extensions::MultiparameterHandler do
|
|
10
10
|
|
11
11
|
it 'should assign a Time value for valid datetimes' do
|
12
12
|
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
|
13
|
-
employee.birth_datetime_before_type_cast.should eq Time.local(2000, 2, 28, 12, 0, 0)
|
13
|
+
employee.birth_datetime_before_type_cast.should eq Time.zone.local(2000, 2, 28, 12, 0, 0)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should assign a string value for incomplete time' do
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ValidatesTimeliness, 'HelperMethods' do
|
4
|
+
let(:record) { Person.new }
|
5
|
+
|
4
6
|
it 'should define class validation methods' do
|
5
7
|
Person.should respond_to(:validates_date)
|
6
8
|
Person.should respond_to(:validates_time)
|
@@ -8,23 +10,21 @@ describe ValidatesTimeliness, 'HelperMethods' do
|
|
8
10
|
end
|
9
11
|
|
10
12
|
it 'should define instance validation methods' do
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
record.should respond_to(:validates_date)
|
14
|
+
record.should respond_to(:validates_time)
|
15
|
+
record.should respond_to(:validates_datetime)
|
14
16
|
end
|
15
17
|
|
16
18
|
it 'should validate instance using class validation defined' do
|
17
19
|
Person.validates_date :birth_date
|
18
|
-
|
19
|
-
r.valid?
|
20
|
+
record.valid?
|
20
21
|
|
21
|
-
|
22
|
+
record.errors[:birth_date].should_not be_empty
|
22
23
|
end
|
23
24
|
|
24
25
|
it 'should validate instance using instance valiation method' do
|
25
|
-
|
26
|
-
r.validates_date :birth_date
|
26
|
+
record.validates_date :birth_date
|
27
27
|
|
28
|
-
|
28
|
+
record.errors[:birth_date].should_not be_empty
|
29
29
|
end
|
30
30
|
end
|
@@ -3,6 +3,8 @@ require 'spec_helper'
|
|
3
3
|
describe ValidatesTimeliness, 'ActiveRecord' do
|
4
4
|
|
5
5
|
context "validation methods" do
|
6
|
+
let(:record) { Employee.new }
|
7
|
+
|
6
8
|
it 'should be defined for the class' do
|
7
9
|
ActiveRecord::Base.should respond_to(:validates_date)
|
8
10
|
ActiveRecord::Base.should respond_to(:validates_time)
|
@@ -10,33 +12,30 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
10
12
|
end
|
11
13
|
|
12
14
|
it 'should defines for the instance' do
|
13
|
-
|
14
|
-
|
15
|
-
|
15
|
+
record.should respond_to(:validates_date)
|
16
|
+
record.should respond_to(:validates_time)
|
17
|
+
record.should respond_to(:validates_datetime)
|
16
18
|
end
|
17
19
|
|
18
20
|
it "should validate a valid value string" do
|
19
|
-
|
20
|
-
r.birth_date = '2012-01-01'
|
21
|
+
record.birth_date = '2012-01-01'
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
record.valid?
|
24
|
+
record.errors[:birth_date].should be_empty
|
24
25
|
end
|
25
26
|
|
26
27
|
it "should validate a invalid value string" do
|
27
|
-
|
28
|
-
r.birth_date = 'not a date'
|
28
|
+
record.birth_date = 'not a date'
|
29
29
|
|
30
|
-
|
31
|
-
|
30
|
+
record.valid?
|
31
|
+
record.errors[:birth_date].should_not be_empty
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should validate a nil value" do
|
35
|
-
|
36
|
-
r.birth_date = nil
|
35
|
+
record.birth_date = nil
|
37
36
|
|
38
|
-
|
39
|
-
|
37
|
+
record.valid?
|
38
|
+
record.errors[:birth_date].should be_empty
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
@@ -83,36 +82,36 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
83
82
|
end
|
84
83
|
|
85
84
|
context 'value cache' do
|
85
|
+
let(:record) { EmployeeWithCache.new }
|
86
|
+
|
86
87
|
context 'for datetime column' do
|
87
88
|
it 'should store raw value' do
|
88
|
-
|
89
|
-
r.birth_datetime = datetime_string = '2010-01-01 12:30'
|
89
|
+
record.birth_datetime = datetime_string = '2010-01-01 12:30'
|
90
90
|
|
91
|
-
|
91
|
+
record._timeliness_raw_value_for('birth_datetime').should eq datetime_string
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
95
|
context 'for date column' do
|
96
96
|
it 'should store raw value' do
|
97
|
-
|
98
|
-
r.birth_date = date_string = '2010-01-01'
|
97
|
+
record.birth_date = date_string = '2010-01-01'
|
99
98
|
|
100
|
-
|
99
|
+
record._timeliness_raw_value_for('birth_date').should eq date_string
|
101
100
|
end
|
102
101
|
end
|
103
102
|
|
104
103
|
context 'for time column' do
|
105
104
|
it 'should store raw value' do
|
106
|
-
|
107
|
-
r.birth_time = time_string = '12:12'
|
105
|
+
record.birth_time = time_string = '12:12'
|
108
106
|
|
109
|
-
|
107
|
+
record._timeliness_raw_value_for('birth_time').should eq time_string
|
110
108
|
end
|
111
109
|
end
|
112
110
|
end
|
113
111
|
|
114
112
|
context "with plugin parser" do
|
115
113
|
with_config(:use_plugin_parser, true)
|
114
|
+
let(:record) { EmployeeWithParser.new }
|
116
115
|
|
117
116
|
class EmployeeWithParser < ActiveRecord::Base
|
118
117
|
self.table_name = 'employees'
|
@@ -125,22 +124,20 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
125
124
|
it 'should parse a string value' do
|
126
125
|
Timeliness::Parser.should_receive(:parse)
|
127
126
|
|
128
|
-
|
129
|
-
r.birth_date = '2010-01-01'
|
127
|
+
record.birth_date = '2010-01-01'
|
130
128
|
end
|
131
129
|
|
132
130
|
it 'should parse a invalid string value as nil' do
|
133
131
|
Timeliness::Parser.should_receive(:parse)
|
134
|
-
|
135
|
-
|
132
|
+
|
133
|
+
record.birth_date = 'not valid'
|
136
134
|
end
|
137
135
|
|
138
136
|
it 'should store a Date value after parsing string' do
|
139
|
-
|
140
|
-
r.birth_date = '2010-01-01'
|
137
|
+
record.birth_date = '2010-01-01'
|
141
138
|
|
142
|
-
|
143
|
-
|
139
|
+
record.birth_date.should be_kind_of(Date)
|
140
|
+
record.birth_date.should eq Date.new(2010, 1, 1)
|
144
141
|
end
|
145
142
|
end
|
146
143
|
|
@@ -148,23 +145,20 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
148
145
|
it 'should parse a string value' do
|
149
146
|
Timeliness::Parser.should_receive(:parse)
|
150
147
|
|
151
|
-
|
152
|
-
r.birth_time = '12:30'
|
148
|
+
record.birth_time = '12:30'
|
153
149
|
end
|
154
150
|
|
155
151
|
it 'should parse a invalid string value as nil' do
|
156
152
|
Timeliness::Parser.should_receive(:parse)
|
157
153
|
|
158
|
-
|
159
|
-
r.birth_time = 'not valid'
|
154
|
+
record.birth_time = 'not valid'
|
160
155
|
end
|
161
156
|
|
162
157
|
it 'should store a Time value after parsing string' do
|
163
|
-
|
164
|
-
r.birth_time = '12:30'
|
158
|
+
record.birth_time = '12:30'
|
165
159
|
|
166
|
-
|
167
|
-
|
160
|
+
record.birth_time.should be_kind_of(Time)
|
161
|
+
record.birth_time.should eq Time.utc(2000, 1, 1, 12, 30)
|
168
162
|
end
|
169
163
|
end
|
170
164
|
|
@@ -174,29 +168,25 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
174
168
|
it 'should parse a string value' do
|
175
169
|
Timeliness::Parser.should_receive(:parse)
|
176
170
|
|
177
|
-
|
178
|
-
r.birth_datetime = '2010-01-01 12:00'
|
171
|
+
record.birth_datetime = '2010-01-01 12:00'
|
179
172
|
end
|
180
173
|
|
181
174
|
it 'should parse a invalid string value as nil' do
|
182
175
|
Timeliness::Parser.should_receive(:parse)
|
183
176
|
|
184
|
-
|
185
|
-
r.birth_datetime = 'not valid'
|
177
|
+
record.birth_datetime = 'not valid'
|
186
178
|
end
|
187
179
|
|
188
180
|
it 'should parse string into Time value' do
|
189
|
-
|
190
|
-
r.birth_datetime = '2010-01-01 12:00'
|
181
|
+
record.birth_datetime = '2010-01-01 12:00'
|
191
182
|
|
192
|
-
|
183
|
+
record.birth_datetime.should be_kind_of(Time)
|
193
184
|
end
|
194
185
|
|
195
186
|
it 'should parse string as current timezone' do
|
196
|
-
|
197
|
-
r.birth_datetime = '2010-06-01 12:00'
|
187
|
+
record.birth_datetime = '2010-06-01 12:00'
|
198
188
|
|
199
|
-
|
189
|
+
record.birth_datetime.utc_offset.should eq Time.zone.utc_offset
|
200
190
|
end
|
201
191
|
end
|
202
192
|
end
|
@@ -204,43 +194,43 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
204
194
|
|
205
195
|
context "reload" do
|
206
196
|
it 'should clear cache value' do
|
207
|
-
|
208
|
-
|
197
|
+
record = Employee.create!
|
198
|
+
record.birth_date = '2010-01-01'
|
209
199
|
|
210
|
-
|
200
|
+
record.reload
|
211
201
|
|
212
|
-
|
202
|
+
record._timeliness_raw_value_for('birth_date').should be_nil
|
213
203
|
end
|
214
204
|
end
|
215
205
|
|
216
206
|
context "before_type_cast method" do
|
207
|
+
let(:record) { Employee.new }
|
208
|
+
|
217
209
|
it 'should be defined on class if ORM supports it' do
|
218
|
-
|
210
|
+
record.should respond_to(:birth_datetime_before_type_cast)
|
219
211
|
end
|
220
212
|
|
221
213
|
it 'should return original value' do
|
222
|
-
|
223
|
-
r.birth_datetime = date_string = '2010-01-01'
|
214
|
+
record.birth_datetime = date_string = '2010-01-01'
|
224
215
|
|
225
|
-
|
216
|
+
record.birth_datetime_before_type_cast.should eq date_string
|
226
217
|
end
|
227
218
|
|
228
219
|
it 'should return attribute if no attribute assignment has been made' do
|
229
220
|
datetime = Time.zone.local(2010,01,01)
|
230
221
|
Employee.create(:birth_datetime => datetime)
|
231
222
|
|
232
|
-
|
233
|
-
|
223
|
+
record = Employee.last
|
224
|
+
record.birth_datetime_before_type_cast.should match(/#{datetime.utc.to_s[0...-4]}/)
|
234
225
|
end
|
235
226
|
|
236
227
|
context "with plugin parser" do
|
237
228
|
with_config(:use_plugin_parser, true)
|
238
229
|
|
239
230
|
it 'should return original value' do
|
240
|
-
|
241
|
-
r.birth_datetime = date_string = '2010-01-31'
|
231
|
+
record.birth_datetime = date_string = '2010-01-31'
|
242
232
|
|
243
|
-
|
233
|
+
record.birth_datetime_before_type_cast.should eq date_string
|
244
234
|
end
|
245
235
|
end
|
246
236
|
|
@@ -7,13 +7,13 @@ require 'mongoid'
|
|
7
7
|
require 'validates_timeliness/orm/mongoid'
|
8
8
|
|
9
9
|
Mongoid.configure do |config|
|
10
|
-
|
11
|
-
host = "localhost"
|
12
|
-
config.master = Mongo::Connection.new.db(name)
|
13
|
-
config.persist_in_safe_mode = false
|
10
|
+
config.connect_to('validates_timeliness_test')
|
14
11
|
end
|
15
12
|
|
16
13
|
describe ValidatesTimeliness, 'Mongoid' do
|
14
|
+
after(:each) do
|
15
|
+
Mongoid.purge!
|
16
|
+
end
|
17
17
|
|
18
18
|
class Article
|
19
19
|
include Mongoid::Document
|
@@ -47,16 +47,6 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|
47
47
|
record.errors[:publish_date].should be_empty
|
48
48
|
end
|
49
49
|
|
50
|
-
it "should validate a invalid value string" do
|
51
|
-
begin
|
52
|
-
record.publish_date = 'not a date'
|
53
|
-
rescue
|
54
|
-
end
|
55
|
-
|
56
|
-
record.valid?
|
57
|
-
record.errors[:publish_date].should_not be_empty
|
58
|
-
end
|
59
|
-
|
60
50
|
it "should validate a nil value" do
|
61
51
|
record.publish_date = nil
|
62
52
|
|
@@ -67,6 +57,8 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|
67
57
|
|
68
58
|
it 'should determine type for attribute' do
|
69
59
|
Article.timeliness_attribute_type(:publish_date).should == :date
|
60
|
+
Article.timeliness_attribute_type(:publish_time).should == :time
|
61
|
+
Article.timeliness_attribute_type(:publish_datetime).should == :datetime
|
70
62
|
end
|
71
63
|
|
72
64
|
context "attribute write method" do
|
@@ -157,7 +149,7 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|
157
149
|
record.publish_datetime.should be_kind_of(DateTime)
|
158
150
|
end
|
159
151
|
|
160
|
-
|
152
|
+
it 'should parse string as current timezone' do
|
161
153
|
record.publish_datetime = '2010-06-01 12:00'
|
162
154
|
|
163
155
|
record.publish_datetime.utc_offset.should eq Time.zone.utc_offset
|
@@ -176,8 +168,50 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|
176
168
|
end
|
177
169
|
|
178
170
|
context "before_type_cast method" do
|
179
|
-
|
180
|
-
|
171
|
+
let(:record){ Article.new }
|
172
|
+
|
173
|
+
it 'should be defined on class if ORM supports it' do
|
174
|
+
record.should respond_to(:publish_datetime_before_type_cast)
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'should return original value' do
|
178
|
+
record.publish_datetime = date_string = '2010-01-01'
|
179
|
+
|
180
|
+
record.publish_datetime_before_type_cast.should eq date_string
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'should return attribute if no attribute assignment has been made' do
|
184
|
+
time = Time.zone.local(2010,01,01)
|
185
|
+
Article.create(:publish_datetime => time)
|
186
|
+
record = Article.last
|
187
|
+
record.publish_datetime_before_type_cast.should eq time.to_datetime
|
188
|
+
end
|
189
|
+
|
190
|
+
context "with plugin parser" do
|
191
|
+
with_config(:use_plugin_parser, true)
|
192
|
+
|
193
|
+
it 'should return original value' do
|
194
|
+
record.publish_datetime = date_string = '2010-01-31'
|
195
|
+
record.publish_datetime_before_type_cast.should eq date_string
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context "with aliased fields" do
|
201
|
+
class ArticleWithAliasedFields
|
202
|
+
include Mongoid::Document
|
203
|
+
field :pd, as: :publish_date, :type => Date
|
204
|
+
field :pt, as: :publish_time, :type => Time
|
205
|
+
field :pdt, as: :publish_datetime, :type => DateTime
|
206
|
+
validates_date :publish_date, :allow_nil => true
|
207
|
+
validates_time :publish_time, :allow_nil => true
|
208
|
+
validates_datetime :publish_datetime, :allow_nil => true
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'should determine type for attribute' do
|
212
|
+
ArticleWithAliasedFields.timeliness_attribute_type(:publish_date).should == :date
|
213
|
+
ArticleWithAliasedFields.timeliness_attribute_type(:publish_time).should == :time
|
214
|
+
ArticleWithAliasedFields.timeliness_attribute_type(:publish_datetime).should == :datetime
|
181
215
|
end
|
182
216
|
end
|
183
217
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ValidatesTimeliness::Validator do
|
4
|
-
NIL = [nil]
|
5
|
-
|
6
4
|
before do
|
7
5
|
Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
|
8
6
|
end
|
@@ -46,13 +44,13 @@ describe ValidatesTimeliness::Validator do
|
|
46
44
|
describe ":allow_nil option" do
|
47
45
|
it 'should not allow nil by default' do
|
48
46
|
Person.validates_date :birth_date
|
49
|
-
invalid!(:birth_date,
|
47
|
+
invalid!(:birth_date, [nil], 'is not a valid date')
|
50
48
|
valid!(:birth_date, Date.today)
|
51
49
|
end
|
52
50
|
|
53
51
|
it 'should allow nil when true' do
|
54
52
|
Person.validates_date :birth_date, :allow_nil => true
|
55
|
-
valid!(:birth_date,
|
53
|
+
valid!(:birth_date, [nil])
|
56
54
|
end
|
57
55
|
|
58
56
|
context "with raw value cache" do
|
@@ -117,6 +115,19 @@ describe ValidatesTimeliness::Validator do
|
|
117
115
|
valid!(:birth_date, on_or_before)
|
118
116
|
end
|
119
117
|
end
|
118
|
+
|
119
|
+
describe "range with excluded end value" do
|
120
|
+
it 'should be split option into :on_or_after and :before values' do
|
121
|
+
on_or_after, before = Date.new(2010,1,1), Date.new(2010,1,3)
|
122
|
+
Person.validates_date :birth_date, :between => on_or_after...before
|
123
|
+
Person.validators.first.options[:on_or_after].should == on_or_after
|
124
|
+
Person.validators.first.options[:before].should == before
|
125
|
+
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
126
|
+
invalid!(:birth_date, before, "must be before 2010-01-03")
|
127
|
+
valid!(:birth_date, on_or_after)
|
128
|
+
valid!(:birth_date, before - 1)
|
129
|
+
end
|
130
|
+
end
|
120
131
|
end
|
121
132
|
|
122
133
|
describe ":ignore_usec option" do
|
@@ -226,7 +237,7 @@ describe ValidatesTimeliness::Validator do
|
|
226
237
|
Person.validates_date :birth_date, :invalid_date_message => 'custom invalid message'
|
227
238
|
invalid!(:birth_date, 'asdf', 'custom invalid message')
|
228
239
|
end
|
229
|
-
|
240
|
+
|
230
241
|
it 'should be used for invalid restriction' do
|
231
242
|
Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message'
|
232
243
|
invalid!(:birth_date, Time.now, 'custom before message')
|
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
17
|
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
|
18
18
|
|
19
|
-
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.
|
19
|
+
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.7"])
|
20
20
|
end
|
metadata
CHANGED
@@ -1,50 +1,39 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_timeliness
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 3
|
8
|
-
- 0
|
9
|
-
- 14
|
10
|
-
version: 3.0.14
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.15
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Adam Meehan
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
22
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
-
none: false
|
24
|
-
requirements:
|
25
|
-
- - ~>
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
hash: 31
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
- 3
|
31
|
-
- 6
|
32
|
-
version: 0.3.6
|
11
|
+
date: 2015-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
33
14
|
name: timeliness
|
34
|
-
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.7
|
35
20
|
type: :runtime
|
36
|
-
|
37
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.7
|
27
|
+
description: Adds validation methods to ActiveModel for validating dates and times.
|
28
|
+
Works with multiple ORMS.
|
38
29
|
email: adam.meehan@gmail.com
|
39
30
|
executables: []
|
40
|
-
|
41
31
|
extensions: []
|
42
|
-
|
43
|
-
extra_rdoc_files:
|
32
|
+
extra_rdoc_files:
|
44
33
|
- README.rdoc
|
45
34
|
- CHANGELOG.rdoc
|
46
35
|
- LICENSE
|
47
|
-
files:
|
36
|
+
files:
|
48
37
|
- CHANGELOG.rdoc
|
49
38
|
- LICENSE
|
50
39
|
- README.rdoc
|
@@ -91,41 +80,30 @@ files:
|
|
91
80
|
- spec/validates_timeliness/validator_spec.rb
|
92
81
|
- spec/validates_timeliness_spec.rb
|
93
82
|
- validates_timeliness.gemspec
|
94
|
-
has_rdoc: true
|
95
83
|
homepage: http://github.com/adzap/validates_timeliness
|
96
84
|
licenses: []
|
97
|
-
|
85
|
+
metadata: {}
|
98
86
|
post_install_message:
|
99
87
|
rdoc_options: []
|
100
|
-
|
101
|
-
require_paths:
|
88
|
+
require_paths:
|
102
89
|
- lib
|
103
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
-
|
105
|
-
requirements:
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
106
92
|
- - ">="
|
107
|
-
- !ruby/object:Gem::Version
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
version: "0"
|
112
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
|
-
requirements:
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
115
97
|
- - ">="
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
|
118
|
-
segments:
|
119
|
-
- 0
|
120
|
-
version: "0"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
121
100
|
requirements: []
|
122
|
-
|
123
101
|
rubyforge_project:
|
124
|
-
rubygems_version:
|
102
|
+
rubygems_version: 2.4.5
|
125
103
|
signing_key:
|
126
|
-
specification_version:
|
104
|
+
specification_version: 4
|
127
105
|
summary: Date and time validation plugin for Rails which allows custom formats
|
128
|
-
test_files:
|
106
|
+
test_files:
|
129
107
|
- spec/spec_helper.rb
|
130
108
|
- spec/support/config_helper.rb
|
131
109
|
- spec/support/model_helpers.rb
|