mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +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
6
README
6
README
@ -231,15 +231,15 @@ an example of using Chronis instead. Put this into a file in the lib directory.
|
||||
|
||||
class ActiveRecord::Base
|
||||
|
||||
def self.timeliness_date_time_parse(raw_value, type)
|
||||
|
||||
def self.parse_date_time(raw_value, type)
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
== CREDITS:
|
||||
|
||||
* Adam Meehan (http://duckpunching.com/)
|
||||
* Adam Meehan (adam.meehan@gmail.com, http://duckpunching.com/)
|
||||
|
||||
* Jonathan Viney (http://workingwithrails.com/person/4985-jonathan-viney)
|
||||
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
|
||||
# valid parsed value.
|
||||
#
|
||||
# Remember Rails, since version 2, will automatically handle the fallback
|
||||
# to a DateTime when you create a time which is out of range.
|
||||
# Remember to attempt fallback to back to a DateTime if the Time object is
|
||||
# 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)
|
||||
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
|
||||
|
||||
# Check time part, and return time object
|
||||
Time.local(*time_array)
|
||||
Time.local(*time_array) rescue DateTime.new(*time_array[0..5])
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
alias_method :parse_date_time, :timeliness_date_time_parse
|
||||
|
||||
# The main validation method which can be used directly or called through
|
||||
# the other specific type validation methods.
|
||||
|
||||
@ -186,6 +186,7 @@ describe ValidatesTimeliness::Formats do
|
||||
|
||||
after do
|
||||
formats.time_formats << 'h.nn_ampm'
|
||||
# reload class instead
|
||||
end
|
||||
end
|
||||
|
||||
@ -223,6 +224,7 @@ describe ValidatesTimeliness::Formats do
|
||||
after do
|
||||
formats.time_formats.delete("h o'clock")
|
||||
formats.time_formats.delete("ss:hh:nn")
|
||||
# reload class instead
|
||||
end
|
||||
end
|
||||
|
||||
@ -231,13 +233,12 @@ describe ValidatesTimeliness::Formats do
|
||||
time_array = formats.extract_date_time_values('01/02/2000', :date)
|
||||
time_array.should == [2000, 1, 2,nil,nil,nil,nil]
|
||||
formats.remove_us_formats
|
||||
puts formats.datetime_formats.inspect
|
||||
time_array = formats.extract_date_time_values('01/02/2000', :date)
|
||||
time_array.should == [2000, 2, 1,nil,nil,nil,nil]
|
||||
end
|
||||
|
||||
after do
|
||||
|
||||
# reload class
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -53,12 +53,10 @@ describe ValidatesTimeliness::Validations do
|
||||
@person.should be_valid
|
||||
end
|
||||
|
||||
# What is going on? No fall back.
|
||||
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 = "1890-01-31"
|
||||
@person.birth_time = "23:59:59"
|
||||
puts @person.errors.inspect
|
||||
@person.should be_valid
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user