validates_timeliness 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -0
- data/README.rdoc +24 -6
- data/Rakefile +2 -1
- data/lib/validates_timeliness/active_record/attribute_methods.rb +5 -6
- data/lib/validates_timeliness/active_record/multiparameter_attributes.rb +28 -23
- data/lib/validates_timeliness/formats.rb +15 -1
- data/lib/validates_timeliness/matcher.rb +1 -0
- data/lib/validates_timeliness/parser.rb +13 -15
- data/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +12 -8
- data/lib/validates_timeliness/validator.rb +16 -20
- data/lib/validates_timeliness/version.rb +3 -0
- data/lib/validates_timeliness.rb +0 -1
- data/spec/action_view/instance_tag_spec.rb +5 -1
- data/spec/active_record/multiparameter_attributes_spec.rb +7 -3
- data/spec/formats_spec.rb +24 -8
- data/spec/ginger_scenarios.rb +1 -1
- data/spec/spec/rails/matchers/validate_timeliness_spec.rb +1 -0
- data/spec/spec_helper.rb +1 -2
- data/spec/validator_spec.rb +25 -16
- metadata +4 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
= 2.2.0 [2009-09-12]
|
2
|
+
- Ruby 1.9 support!
|
3
|
+
- Customise dummy date values for time types. See DUMMY DATE FOR TIME TYPES.
|
4
|
+
- Fixed matcher conflict with Shoulda. Load plugin matcher manually now see matcher section in README
|
5
|
+
- Fixed :ignore_usec when used with :with_time or :with_date
|
6
|
+
- Some clean up and refactoring
|
7
|
+
|
1
8
|
= 2.1.0 [2009-06-20]
|
2
9
|
- Added ambiguous year threshold setting in Formats class to customize the threshold for 2 digit years (See README)
|
3
10
|
- Fixed interpolation values in custom error message for Rails 2.2+
|
data/README.rdoc
CHANGED
@@ -22,9 +22,9 @@ think should be a valid date or time string.
|
|
22
22
|
* Restores ability to see raw value entered for date/time attributes with
|
23
23
|
_before_type_cast modifier, which was lost in Rails 2.1.
|
24
24
|
|
25
|
-
*
|
25
|
+
* Supports Rails timezone handling
|
26
26
|
|
27
|
-
* Supports Rails
|
27
|
+
* Supports Rails I18n for the error messages
|
28
28
|
|
29
29
|
* Rspec matcher for testing model validation of dates and times
|
30
30
|
|
@@ -107,7 +107,7 @@ temporal options.
|
|
107
107
|
|
108
108
|
== EXAMPLES:
|
109
109
|
|
110
|
-
validates_date :date_of_birth :before =>
|
110
|
+
validates_date :date_of_birth :before => lambda { 18.years.ago },
|
111
111
|
:before_message => "must be at least 18 years old"
|
112
112
|
|
113
113
|
validates_time :breakfast_time, :on_or_after => '6:00am',
|
@@ -115,7 +115,7 @@ temporal options.
|
|
115
115
|
:before => :second_breakfast_time,
|
116
116
|
:allow_nil => true
|
117
117
|
|
118
|
-
validates_datetime :appointment_date, :before =>
|
118
|
+
validates_datetime :appointment_date, :before => lambda { 1.week.from_now }
|
119
119
|
|
120
120
|
validates_date :entry_date, :with_time => '17:00', :on_or_before => :competition_closing
|
121
121
|
|
@@ -263,6 +263,18 @@ Now you get:
|
|
263
263
|
year of 20 is considered 1920
|
264
264
|
|
265
265
|
|
266
|
+
=== DUMMY DATE FOR TIME TYPES
|
267
|
+
|
268
|
+
Given that Ruby has no support for a time-only type, all time type columns are evaluated
|
269
|
+
as a regular Time class objects with a dummy date value set. Rails defines the dummy date as
|
270
|
+
2000-01-01. So a time of '12:30' is evaluated as a Time value of '2000-01-01 12:30'. If you
|
271
|
+
need to customize this for some reason you can do so as follows
|
272
|
+
|
273
|
+
ValidatesTimeliness::Formats.dummy_date_for_time_type = [2009, 1, 1]
|
274
|
+
|
275
|
+
The value should be an array of 3 values being year, month and day in that order.
|
276
|
+
|
277
|
+
|
266
278
|
=== TEMPORAL RESTRICTION ERRORS:
|
267
279
|
|
268
280
|
When using the validation temporal restrictions there are times when the restriction
|
@@ -355,9 +367,15 @@ To sweeten the deal that little bit more, you have an Rspec matcher available fo
|
|
355
367
|
you model specs. Now you can easily test the validations you have just written
|
356
368
|
with the plugin or better yet *before* you write them! You just use the
|
357
369
|
validation options you want as you would with the validation method. Those
|
358
|
-
options are then verified and reported if they fail.
|
370
|
+
options are then verified and reported if they fail.
|
371
|
+
|
372
|
+
First require it in your spec_helper.rb
|
373
|
+
|
374
|
+
require 'validates_timeliness/matcher'
|
375
|
+
|
376
|
+
Use it like so:
|
359
377
|
|
360
|
-
|
378
|
+
@person.should validate_date(:birth_date, :before => Time.now, :before_message => 'should be before today')
|
361
379
|
|
362
380
|
|
363
381
|
The matcher names are just the singular of the validation methods.
|
data/Rakefile
CHANGED
@@ -3,9 +3,10 @@ require 'rake/gempackagetask'
|
|
3
3
|
require 'rubygems/specification'
|
4
4
|
require 'date'
|
5
5
|
require 'spec/rake/spectask'
|
6
|
+
require 'lib/validates_timeliness/version'
|
6
7
|
|
7
8
|
GEM = "validates_timeliness"
|
8
|
-
GEM_VERSION =
|
9
|
+
GEM_VERSION = ValidatesTimeliness::VERSION
|
9
10
|
AUTHOR = "Adam Meehan"
|
10
11
|
EMAIL = "adam.meehan@gmail.com"
|
11
12
|
HOMEPAGE = "http://github.com/adzap/validates_timeliness"
|
@@ -36,7 +36,8 @@ module ValidatesTimeliness
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def read_attribute_before_type_cast_with_timeliness(attr_name)
|
39
|
-
|
39
|
+
cached_attr = "_#{attr_name}_before_type_cast"
|
40
|
+
return @attributes_cache[cached_attr] if @attributes_cache.has_key?(cached_attr)
|
40
41
|
read_attribute_before_type_cast_without_timeliness(attr_name)
|
41
42
|
end
|
42
43
|
|
@@ -50,11 +51,9 @@ module ValidatesTimeliness
|
|
50
51
|
if [:date, :time, :datetime].include?(column.type)
|
51
52
|
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
EOV
|
54
|
+
define_method("#{name}=") do |value|
|
55
|
+
write_date_time_attribute(name, value, column.type, time_zone_aware)
|
56
|
+
end
|
58
57
|
timeliness_methods << name
|
59
58
|
end
|
60
59
|
end
|
@@ -5,6 +5,33 @@ module ValidatesTimeliness
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module ActiveRecord
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def time_array_to_string(values, type)
|
12
|
+
values.collect! {|v| v.to_s }
|
13
|
+
|
14
|
+
case type
|
15
|
+
when :date
|
16
|
+
extract_date_from_multiparameter_attributes(values)
|
17
|
+
when :time
|
18
|
+
extract_time_from_multiparameter_attributes(values)
|
19
|
+
when :datetime
|
20
|
+
extract_date_from_multiparameter_attributes(values) + " " + extract_time_from_multiparameter_attributes(values)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def extract_date_from_multiparameter_attributes(values)
|
25
|
+
year = ValidatesTimeliness::Formats.unambiguous_year(values[0].rjust(2, "0"))
|
26
|
+
[year, *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
27
|
+
end
|
28
|
+
|
29
|
+
def extract_time_from_multiparameter_attributes(values)
|
30
|
+
values[3..5].map { |s| s.rjust(2, "0") }.join(":")
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
8
35
|
module MultiparameterAttributes
|
9
36
|
|
10
37
|
def self.included(base)
|
@@ -23,7 +50,7 @@ module ValidatesTimeliness
|
|
23
50
|
if values.empty?
|
24
51
|
send("#{name}=", nil)
|
25
52
|
else
|
26
|
-
value = time_array_to_string(values, column.type)
|
53
|
+
value = ValidatesTimeliness::ActiveRecord.time_array_to_string(values, column.type)
|
27
54
|
send("#{name}=", value)
|
28
55
|
end
|
29
56
|
rescue => ex
|
@@ -37,28 +64,6 @@ module ValidatesTimeliness
|
|
37
64
|
execute_callstack_for_multiparameter_attributes_without_timeliness(callstack)
|
38
65
|
end
|
39
66
|
|
40
|
-
def time_array_to_string(values, type)
|
41
|
-
values.collect! {|v| v.to_s }
|
42
|
-
|
43
|
-
case type
|
44
|
-
when :date
|
45
|
-
extract_date_from_multiparameter_attributes(values)
|
46
|
-
when :time
|
47
|
-
extract_time_from_multiparameter_attributes(values)
|
48
|
-
when :datetime
|
49
|
-
extract_date_from_multiparameter_attributes(values) + " " + extract_time_from_multiparameter_attributes(values)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def extract_date_from_multiparameter_attributes(values)
|
54
|
-
year = ValidatesTimeliness::Formats.unambiguous_year(values[0].rjust(2, "0"))
|
55
|
-
[year, *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
56
|
-
end
|
57
|
-
|
58
|
-
def extract_time_from_multiparameter_attributes(values)
|
59
|
-
values[3..5].map { |s| s.rjust(2, "0") }.join(":")
|
60
|
-
end
|
61
|
-
|
62
67
|
end
|
63
68
|
|
64
69
|
end
|
@@ -23,6 +23,7 @@ module ValidatesTimeliness
|
|
23
23
|
|
24
24
|
|
25
25
|
# Set the threshold value for a two digit year to be considered last century
|
26
|
+
#
|
26
27
|
# Default: 30
|
27
28
|
#
|
28
29
|
# Example:
|
@@ -32,6 +33,14 @@ module ValidatesTimeliness
|
|
32
33
|
cattr_accessor :ambiguous_year_threshold
|
33
34
|
self.ambiguous_year_threshold = 30
|
34
35
|
|
36
|
+
# Set the dummy date part for a time type value. Should be an array of 3 values
|
37
|
+
# being year, month and day in that order.
|
38
|
+
#
|
39
|
+
# Default: [ 2000, 1, 1 ] same as ActiveRecord
|
40
|
+
#
|
41
|
+
cattr_accessor :dummy_date_for_time_type
|
42
|
+
self.dummy_date_for_time_type = [ 2000, 1, 1 ]
|
43
|
+
|
35
44
|
# Format tokens:
|
36
45
|
# y = year
|
37
46
|
# m = month
|
@@ -179,6 +188,7 @@ module ValidatesTimeliness
|
|
179
188
|
options.reverse_merge!(:strict => true)
|
180
189
|
|
181
190
|
sets = if options[:format]
|
191
|
+
options[:strict] = true
|
182
192
|
[ send("#{type}_expressions").assoc(options[:format]) ]
|
183
193
|
else
|
184
194
|
expression_set(type, string)
|
@@ -195,7 +205,11 @@ module ValidatesTimeliness
|
|
195
205
|
break(proc) if matches = full.match(string.strip)
|
196
206
|
end
|
197
207
|
last = options[:include_offset] ? 8 : 7
|
198
|
-
|
208
|
+
if matches
|
209
|
+
values = processor.call(*matches[1..last])
|
210
|
+
values[0..2] = dummy_date_for_time_type if type == :time
|
211
|
+
return values
|
212
|
+
end
|
199
213
|
end
|
200
214
|
|
201
215
|
# Delete formats of specified type. Error raised if format not found.
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_timeliness/spec/rails/matchers/validate_timeliness'
|
@@ -7,28 +7,24 @@ module ValidatesTimeliness
|
|
7
7
|
return nil if raw_value.blank?
|
8
8
|
return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
9
9
|
|
10
|
-
options.reverse_merge
|
11
|
-
|
12
|
-
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, options)
|
13
|
-
raise if time_array.nil?
|
14
|
-
|
15
|
-
# Rails dummy time date part is defined as 2000-01-01
|
16
|
-
time_array[0..2] = 2000, 1, 1 if type == :time
|
17
|
-
|
18
|
-
# Date.new enforces days per month, unlike Time
|
19
|
-
date = Date.new(*time_array[0..2]) unless type == :time
|
10
|
+
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, options.reverse_merge(:strict => true))
|
11
|
+
return nil if time_array.nil?
|
20
12
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
13
|
+
if type == :date
|
14
|
+
Date.new(*time_array[0..2]) rescue nil
|
15
|
+
else
|
16
|
+
make_time(time_array[0..7])
|
17
|
+
end
|
26
18
|
end
|
27
19
|
|
28
20
|
def make_time(time_array)
|
21
|
+
# Enforce date part validity which Time class does not
|
22
|
+
return nil unless Date.valid_civil?(*time_array[0..2])
|
23
|
+
|
29
24
|
if Time.respond_to?(:zone) && ValidatesTimeliness.use_time_zones
|
30
25
|
Time.zone.local(*time_array)
|
31
26
|
else
|
27
|
+
# Older AR way of handling times with datetime fallback
|
32
28
|
begin
|
33
29
|
time_zone = ValidatesTimeliness.default_timezone
|
34
30
|
Time.send(time_zone, *time_array)
|
@@ -38,6 +34,8 @@ module ValidatesTimeliness
|
|
38
34
|
DateTime.civil(*(time_array << zone_offset))
|
39
35
|
end
|
40
36
|
end
|
37
|
+
rescue ArgumentError, TypeError
|
38
|
+
nil
|
41
39
|
end
|
42
40
|
|
43
41
|
end
|
@@ -28,13 +28,12 @@ module Spec
|
|
28
28
|
|
29
29
|
valid = test_validity
|
30
30
|
|
31
|
-
valid = test_option(:equal_to)
|
32
|
-
valid = test_option(:before)
|
33
|
-
valid = test_option(:after)
|
34
|
-
valid = test_option(:on_or_before) if @options[:on_or_before]
|
35
|
-
valid = test_option(:on_or_after)
|
36
|
-
|
37
|
-
valid = test_between if @options[:between] && valid
|
31
|
+
valid = test_option(:equal_to) if valid && @options[:equal_to]
|
32
|
+
valid = test_option(:before) if valid && @options[:before]
|
33
|
+
valid = test_option(:after) if valid && @options[:after]
|
34
|
+
valid = test_option(:on_or_before) if valid && @options[:on_or_before]
|
35
|
+
valid = test_option(:on_or_after) if valid && @options[:on_or_after]
|
36
|
+
valid = test_between if valid && @options[:between]
|
38
37
|
|
39
38
|
return valid
|
40
39
|
end
|
@@ -124,9 +123,14 @@ module Spec
|
|
124
123
|
restriction = [restriction] unless restriction.is_a?(Array)
|
125
124
|
restriction.map! {|r| @validator.class.send(:type_cast_value, r, @type) }
|
126
125
|
interpolate = @validator.send(:interpolation_values, option, restriction )
|
126
|
+
|
127
127
|
# get I18n message if defined and has interpolation keys in msg
|
128
128
|
if defined?(I18n) && !@validator.send(:custom_error_messages).include?(option)
|
129
|
-
msg =
|
129
|
+
msg = if defined?(ActiveRecord::Error)
|
130
|
+
ActiveRecord::Error.new(@record, @expected, option, interpolate).message
|
131
|
+
else
|
132
|
+
@record.errors.generate_message(@expected, option, interpolate)
|
133
|
+
end
|
130
134
|
else
|
131
135
|
msg = msg % interpolate
|
132
136
|
end
|
@@ -32,21 +32,13 @@ module ValidatesTimeliness
|
|
32
32
|
raw_value = raw_value(record, attr_name) || value
|
33
33
|
|
34
34
|
if value.is_a?(String) || configuration[:format]
|
35
|
-
|
36
|
-
value = ValidatesTimeliness::Parser.parse(raw_value, type, :strict => strict, :format => configuration[:format])
|
35
|
+
value = ValidatesTimeliness::Parser.parse(raw_value, type, :strict => false, :format => configuration[:format])
|
37
36
|
end
|
38
37
|
|
39
38
|
return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank])
|
40
39
|
|
41
|
-
if raw_value.blank?
|
42
|
-
|
43
|
-
return
|
44
|
-
end
|
45
|
-
|
46
|
-
if value.nil?
|
47
|
-
add_error(record, attr_name, "invalid_#{type}".to_sym)
|
48
|
-
return
|
49
|
-
end
|
40
|
+
return add_error(record, attr_name, :blank) if raw_value.blank?
|
41
|
+
return add_error(record, attr_name, "invalid_#{type}".to_sym) if value.nil?
|
50
42
|
|
51
43
|
validate_restrictions(record, attr_name, value)
|
52
44
|
end
|
@@ -62,21 +54,20 @@ module ValidatesTimeliness
|
|
62
54
|
end
|
63
55
|
|
64
56
|
def validate_restrictions(record, attr_name, value)
|
65
|
-
|
66
|
-
|
67
|
-
combine_date_and_time(value, record)
|
68
|
-
else
|
69
|
-
restriction_type = type
|
70
|
-
self.class.type_cast_value(value, type, configuration[:ignore_usec])
|
57
|
+
if configuration[:with_time] || configuration[:with_date]
|
58
|
+
value = combine_date_and_time(value, record)
|
71
59
|
end
|
60
|
+
|
61
|
+
value = self.class.type_cast_value(value, implied_type, configuration[:ignore_usec])
|
62
|
+
|
72
63
|
return if value.nil?
|
73
64
|
|
74
65
|
RESTRICTION_METHODS.each do |option, method|
|
75
66
|
next unless restriction = configuration[option]
|
76
67
|
begin
|
77
|
-
restriction = self.class.evaluate_option_value(restriction,
|
68
|
+
restriction = self.class.evaluate_option_value(restriction, implied_type, record)
|
78
69
|
next if restriction.nil?
|
79
|
-
restriction = self.class.type_cast_value(restriction,
|
70
|
+
restriction = self.class.type_cast_value(restriction, implied_type, configuration[:ignore_usec])
|
80
71
|
|
81
72
|
unless evaluate_restriction(restriction, value, method)
|
82
73
|
add_error(record, attr_name, option, interpolation_values(option, restriction))
|
@@ -155,6 +146,10 @@ module ValidatesTimeliness
|
|
155
146
|
options.assert_valid_keys(VALID_OPTIONS - invalid_for_type)
|
156
147
|
end
|
157
148
|
|
149
|
+
def implied_type
|
150
|
+
@implied_type ||= configuration[:with_date] || configuration[:with_time] ? :datetime : type
|
151
|
+
end
|
152
|
+
|
158
153
|
# class methods
|
159
154
|
class << self
|
160
155
|
|
@@ -185,7 +180,8 @@ module ValidatesTimeliness
|
|
185
180
|
when Symbol
|
186
181
|
evaluate_option_value(record.send(value), type, record)
|
187
182
|
when Proc
|
188
|
-
|
183
|
+
result = value.arity > 0 ? value.call(record) : value.call
|
184
|
+
evaluate_option_value(result, type, record)
|
189
185
|
when Array
|
190
186
|
value.map {|r| evaluate_option_value(r, type, record) }.sort
|
191
187
|
when Range
|
data/lib/validates_timeliness.rb
CHANGED
@@ -2,7 +2,6 @@ require 'validates_timeliness/formats'
|
|
2
2
|
require 'validates_timeliness/parser'
|
3
3
|
require 'validates_timeliness/validator'
|
4
4
|
require 'validates_timeliness/validation_methods'
|
5
|
-
require 'validates_timeliness/spec/rails/matchers/validate_timeliness' if ENV['RAILS_ENV'] == 'test'
|
6
5
|
|
7
6
|
require 'validates_timeliness/active_record/attribute_methods'
|
8
7
|
require 'validates_timeliness/active_record/multiparameter_attributes'
|
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
|
3
|
+
ValidatesTimeliness.enable_datetime_select_extension!
|
4
|
+
|
5
|
+
describe 'ValidatesTimeliness::ActionView::InstanceTag' do
|
6
|
+
include ActionView::Helpers::DateHelper
|
7
|
+
include ActionController::Assertions::SelectorAssertions
|
4
8
|
|
5
9
|
before do
|
6
10
|
@person = Person.new
|
@@ -6,17 +6,17 @@ describe ValidatesTimeliness::ActiveRecord::MultiparameterAttributes do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should convert array for datetime type into datetime string" do
|
9
|
-
time_string =
|
9
|
+
time_string = time_array_to_string([2000,2,1,9,10,11], :datetime)
|
10
10
|
time_string.should == "2000-02-01 09:10:11"
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should convert array for date type into date string" do
|
14
|
-
time_string =
|
14
|
+
time_string = time_array_to_string([2000,2,1], :date)
|
15
15
|
time_string.should == "2000-02-01"
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should convert array for time type into time string" do
|
19
|
-
time_string =
|
19
|
+
time_string = time_array_to_string([2000,1,1,9,10,11], :time)
|
20
20
|
time_string.should == "09:10:11"
|
21
21
|
end
|
22
22
|
|
@@ -44,5 +44,9 @@ describe ValidatesTimeliness::ActiveRecord::MultiparameterAttributes do
|
|
44
44
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
45
45
|
end
|
46
46
|
end
|
47
|
+
|
48
|
+
def time_array_to_string(*args)
|
49
|
+
ValidatesTimeliness::ActiveRecord.time_array_to_string(*args)
|
50
|
+
end
|
47
51
|
|
48
52
|
end
|
data/spec/formats_spec.rb
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe ValidatesTimeliness::Formats do
|
4
|
-
attr_reader :formats
|
5
|
-
|
6
|
-
before do
|
7
|
-
@formats = ValidatesTimeliness::Formats
|
8
|
-
end
|
9
4
|
|
10
5
|
describe "format proc generator" do
|
11
6
|
it "should generate proc which outputs date array with values in correct order" do
|
@@ -104,7 +99,7 @@ describe ValidatesTimeliness::Formats do
|
|
104
99
|
|
105
100
|
it "should return time array from date string" do
|
106
101
|
time_array = formats.parse('12:13:14', :time, :strict => true)
|
107
|
-
time_array.should == [
|
102
|
+
time_array.should == [2000,1,1,12,13,14,0]
|
108
103
|
end
|
109
104
|
|
110
105
|
it "should return date array from time string" do
|
@@ -134,7 +129,7 @@ describe ValidatesTimeliness::Formats do
|
|
134
129
|
|
135
130
|
it "should ignore date when extracting time and strict is false" do
|
136
131
|
time_array = formats.parse('2000-02-01 12:13', :time, :strict => false)
|
137
|
-
time_array.should == [
|
132
|
+
time_array.should == [2000,1,1,12,13,0,0]
|
138
133
|
end
|
139
134
|
|
140
135
|
it "should return zone offset when :include_offset options is true" do
|
@@ -177,6 +172,22 @@ describe ValidatesTimeliness::Formats do
|
|
177
172
|
end
|
178
173
|
end
|
179
174
|
|
175
|
+
describe "parse with custom dummy date values" do
|
176
|
+
before(:all) do
|
177
|
+
@old_dummy_date = formats.dummy_date_for_time_type
|
178
|
+
formats.dummy_date_for_time_type = [2009,1,1]
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should return time array with custom dummy date" do
|
182
|
+
time_array = formats.parse('12:13:14', :time, :strict => true)
|
183
|
+
time_array.should == [2009,1,1,12,13,14,0]
|
184
|
+
end
|
185
|
+
|
186
|
+
after(:all) do
|
187
|
+
formats.dummy_date_for_time_type = @old_dummy_date
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
180
191
|
describe "removing formats" do
|
181
192
|
it "should remove format from format array" do
|
182
193
|
formats.remove_formats(:time, 'h.nn_ampm')
|
@@ -219,7 +230,7 @@ describe ValidatesTimeliness::Formats do
|
|
219
230
|
formats.add_formats(:time, "ss:hh:nn", :before => 'hh:nn:ss')
|
220
231
|
validate("59:23:58", :time).should be_true
|
221
232
|
time_array = formats.parse('59:23:58', :time)
|
222
|
-
time_array.should == [
|
233
|
+
time_array.should == [2000,1,1,23,58,59,0]
|
223
234
|
end
|
224
235
|
|
225
236
|
it "should raise error if format exists" do
|
@@ -251,6 +262,11 @@ describe ValidatesTimeliness::Formats do
|
|
251
262
|
end
|
252
263
|
end
|
253
264
|
|
265
|
+
|
266
|
+
def formats
|
267
|
+
ValidatesTimeliness::Formats
|
268
|
+
end
|
269
|
+
|
254
270
|
def validate(time_string, type)
|
255
271
|
valid = false
|
256
272
|
formats.send("#{type}_expressions").each do |format, regexp, processor|
|
data/spec/ginger_scenarios.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -45,8 +45,7 @@ if RAILS_VER >= '2.1'
|
|
45
45
|
end
|
46
46
|
|
47
47
|
require 'validates_timeliness'
|
48
|
-
|
49
|
-
ValidatesTimeliness.enable_datetime_select_extension!
|
48
|
+
require 'validates_timeliness/matcher'
|
50
49
|
|
51
50
|
ActiveRecord::Migration.verbose = false
|
52
51
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
data/spec/validator_spec.rb
CHANGED
@@ -358,18 +358,6 @@ describe ValidatesTimeliness::Validator do
|
|
358
358
|
should_have_error(:birth_date_and_time, :equal_to)
|
359
359
|
end
|
360
360
|
|
361
|
-
it "should have error when value is equal to :equal_to restriction for all values except microscond, and microsecond is not ignored" do
|
362
|
-
configure_validator(:equal_to => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => false)
|
363
|
-
validate_with(:birth_date_and_time, Time.utc(2000, 1, 1, 0, 0, 0, 500))
|
364
|
-
should_have_error(:birth_date_and_time, :equal_to)
|
365
|
-
end
|
366
|
-
|
367
|
-
it "should be valid when value is equal to :equal_to restriction for all values except microscond, and microsecond is ignored" do
|
368
|
-
configure_validator(:equal_to => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => true)
|
369
|
-
validate_with(:birth_date_and_time, Time.utc(2000, 1, 1, 0, 0, 0, 500))
|
370
|
-
should_have_no_error(:birth_date_and_time, :equal_to)
|
371
|
-
end
|
372
|
-
|
373
361
|
it "should be valid when value is equal to :equal_to restriction" do
|
374
362
|
validate_with(:birth_date_and_time, Time.now)
|
375
363
|
should_have_no_error(:birth_date_and_time, :equal_to)
|
@@ -409,6 +397,16 @@ describe ValidatesTimeliness::Validator do
|
|
409
397
|
end
|
410
398
|
end
|
411
399
|
|
400
|
+
describe "instance with :ignore_usec option" do
|
401
|
+
|
402
|
+
it "should ignore usec on time values when evaluated" do
|
403
|
+
configure_validator(:equal_to => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => true)
|
404
|
+
validate_with(:birth_date_and_time, Time.utc(2000, 1, 1, 0, 0, 0, 500))
|
405
|
+
should_have_no_error(:birth_date_and_time, :equal_to)
|
406
|
+
end
|
407
|
+
|
408
|
+
end
|
409
|
+
|
412
410
|
describe "instance with :with_time option" do
|
413
411
|
|
414
412
|
it "should validate date attribute as datetime combining value of :with_time against restrictions " do
|
@@ -423,20 +421,31 @@ describe ValidatesTimeliness::Validator do
|
|
423
421
|
should_have_no_error(:birth_date, :on_or_before)
|
424
422
|
end
|
425
423
|
|
424
|
+
it "should should ignore usec value on combined value if :ignore_usec option is true" do
|
425
|
+
configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :equal_to => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
|
426
|
+
validate_with(:birth_date, "2000-01-01")
|
427
|
+
should_have_no_error(:birth_date, :equal_to)
|
428
|
+
end
|
426
429
|
end
|
427
430
|
|
428
431
|
describe "instance with :with_date option" do
|
429
432
|
|
430
433
|
it "should validate time attribute as datetime combining value of :with_date against restrictions " do
|
431
434
|
configure_validator(:type => :time, :with_date => '2009-01-01', :on_or_before => Time.mktime(2000,1,1,12,30))
|
432
|
-
validate_with(:
|
433
|
-
should_have_error(:
|
435
|
+
validate_with(:birth_time, "12:30")
|
436
|
+
should_have_error(:birth_time, :on_or_before)
|
434
437
|
end
|
435
438
|
|
436
439
|
it "should skip restriction validation if :with_date value is nil" do
|
437
440
|
configure_validator(:type => :time, :with_date => nil, :on_or_before => Time.mktime(2000,1,1,12,30))
|
438
|
-
validate_with(:
|
439
|
-
should_have_no_error(:
|
441
|
+
validate_with(:birth_time, "12:30")
|
442
|
+
should_have_no_error(:birth_time, :on_or_before)
|
443
|
+
end
|
444
|
+
|
445
|
+
it "should should ignore usec value on combined value if :ignore_usec option is true" do
|
446
|
+
configure_validator(:type => :time, :with_date => Date.new(2000,1,1), :on_or_before => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
|
447
|
+
validate_with(:birth_time, Time.mktime(2000,1,1,12,30,0,50))
|
448
|
+
should_have_no_error(:birth_time, :on_or_before)
|
440
449
|
end
|
441
450
|
end
|
442
451
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_timeliness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
@@ -9,7 +9,7 @@ autorequire: validates_timeliness
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-12 00:00:00 +10:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/validates_timeliness/active_record/multiparameter_attributes.rb
|
34
34
|
- lib/validates_timeliness/active_record/attribute_methods.rb
|
35
35
|
- lib/validates_timeliness/parser.rb
|
36
|
+
- lib/validates_timeliness/version.rb
|
36
37
|
- lib/validates_timeliness/core_ext/date.rb
|
37
38
|
- lib/validates_timeliness/core_ext/time.rb
|
38
39
|
- lib/validates_timeliness/core_ext/date_time.rb
|
@@ -40,6 +41,7 @@ files:
|
|
40
41
|
- lib/validates_timeliness/validation_methods.rb
|
41
42
|
- lib/validates_timeliness/locale/en.yml
|
42
43
|
- lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb
|
44
|
+
- lib/validates_timeliness/matcher.rb
|
43
45
|
- lib/validates_timeliness/action_view/instance_tag.rb
|
44
46
|
- lib/validates_timeliness/formats.rb
|
45
47
|
- lib/validates_timeliness.rb
|