mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 14:56:43 +00:00
fixed fallback for out of range dates which Rails does not handle automatically
aliased timeliness_date_time_parse to parse_date_time for nicer interface
This commit is contained in:
parent
70ba75a4ae
commit
9cf994564e
4
README
4
README
@ -231,7 +231,7 @@ an example of using Chronis instead. Put this into a file in the lib directory.
|
|||||||
|
|
||||||
class ActiveRecord::Base
|
class ActiveRecord::Base
|
||||||
|
|
||||||
def self.timeliness_date_time_parse(raw_value, type)
|
def self.parse_date_time(raw_value, type)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -239,7 +239,7 @@ an example of using Chronis instead. Put this into a file in the lib directory.
|
|||||||
|
|
||||||
== CREDITS:
|
== CREDITS:
|
||||||
|
|
||||||
* Adam Meehan (http://duckpunching.com/)
|
* Adam Meehan (adam.meehan@gmail.com, http://duckpunching.com/)
|
||||||
|
|
||||||
* Jonathan Viney (http://workingwithrails.com/person/4985-jonathan-viney)
|
* Jonathan Viney (http://workingwithrails.com/person/4985-jonathan-viney)
|
||||||
For his validates_date_time plugin which I have used before this plugin and
|
For his validates_date_time plugin which I have used before this plugin and
|
||||||
|
|||||||
@ -24,8 +24,9 @@ module ValidatesTimeliness
|
|||||||
# Chronic. Just return nil for an invalid value and a Time object for a
|
# Chronic. Just return nil for an invalid value and a Time object for a
|
||||||
# valid parsed value.
|
# valid parsed value.
|
||||||
#
|
#
|
||||||
# Remember Rails, since version 2, will automatically handle the fallback
|
# Remember to attempt fallback to back to a DateTime if the Time object is
|
||||||
# to a DateTime when you create a time which is out of range.
|
# out of range. Range for Ruby Time class on 32-bit *nix is down to about
|
||||||
|
# 1902-01-01 and 1970-01-01 for Windows.
|
||||||
def timeliness_date_time_parse(raw_value, type, strict=true)
|
def timeliness_date_time_parse(raw_value, type, strict=true)
|
||||||
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
||||||
|
|
||||||
@ -44,11 +45,11 @@ module ValidatesTimeliness
|
|||||||
Date.new(*time_array[0..2]) unless type == :time
|
Date.new(*time_array[0..2]) unless type == :time
|
||||||
|
|
||||||
# Check time part, and return time object
|
# Check time part, and return time object
|
||||||
Time.local(*time_array)
|
Time.local(*time_array) rescue DateTime.new(*time_array[0..5])
|
||||||
rescue
|
rescue
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
alias_method :parse_date_time, :timeliness_date_time_parse
|
||||||
|
|
||||||
# The main validation method which can be used directly or called through
|
# The main validation method which can be used directly or called through
|
||||||
# the other specific type validation methods.
|
# the other specific type validation methods.
|
||||||
|
|||||||
@ -186,6 +186,7 @@ describe ValidatesTimeliness::Formats do
|
|||||||
|
|
||||||
after do
|
after do
|
||||||
formats.time_formats << 'h.nn_ampm'
|
formats.time_formats << 'h.nn_ampm'
|
||||||
|
# reload class instead
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -223,6 +224,7 @@ describe ValidatesTimeliness::Formats do
|
|||||||
after do
|
after do
|
||||||
formats.time_formats.delete("h o'clock")
|
formats.time_formats.delete("h o'clock")
|
||||||
formats.time_formats.delete("ss:hh:nn")
|
formats.time_formats.delete("ss:hh:nn")
|
||||||
|
# reload class instead
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -231,13 +233,12 @@ describe ValidatesTimeliness::Formats do
|
|||||||
time_array = formats.extract_date_time_values('01/02/2000', :date)
|
time_array = formats.extract_date_time_values('01/02/2000', :date)
|
||||||
time_array.should == [2000, 1, 2,nil,nil,nil,nil]
|
time_array.should == [2000, 1, 2,nil,nil,nil,nil]
|
||||||
formats.remove_us_formats
|
formats.remove_us_formats
|
||||||
puts formats.datetime_formats.inspect
|
|
||||||
time_array = formats.extract_date_time_values('01/02/2000', :date)
|
time_array = formats.extract_date_time_values('01/02/2000', :date)
|
||||||
time_array.should == [2000, 2, 1,nil,nil,nil,nil]
|
time_array.should == [2000, 2, 1,nil,nil,nil,nil]
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
# reload class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -53,12 +53,10 @@ describe ValidatesTimeliness::Validations do
|
|||||||
@person.should be_valid
|
@person.should be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
# What is going on? No fall back.
|
|
||||||
it "should be valid with values before out of Time range" do
|
it "should be valid with values before out of Time range" do
|
||||||
@person.birth_date_and_time = "1890-01-31 12:12:12"
|
@person.birth_date_and_time = "1890-01-31 12:12:12"
|
||||||
@person.birth_date = "1890-01-31"
|
@person.birth_date = "1890-01-31"
|
||||||
@person.birth_time = "23:59:59"
|
@person.birth_time = "23:59:59"
|
||||||
puts @person.errors.inspect
|
|
||||||
@person.should be_valid
|
@person.should be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user