mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 23:06:42 +00:00
added make_time method to do time object creation with correct timezone handling for Rails 2.1 and 2.0
This commit is contained in:
parent
8082b5ce1c
commit
727f3dc8e3
@ -21,7 +21,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
||||||
# Override this method to use any date parsing algorithm you like such as
|
# Override this method to use any date parsing algorithm you like such as
|
||||||
# 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.
|
||||||
@ -31,17 +31,14 @@ module ValidatesTimeliness
|
|||||||
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, strict)
|
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, strict)
|
||||||
raise if time_array.nil?
|
raise if time_array.nil?
|
||||||
|
|
||||||
if type == :time
|
# Rails dummy time date part is defined as 2000-01-01
|
||||||
# Rails dummy time date part is defined as 2000-01-01
|
time_array[0..2] = 2000, 1, 1 if type == :time
|
||||||
time_array[0..2] = 2000, 1, 1
|
|
||||||
end
|
|
||||||
|
|
||||||
# Date.new enforces days per month, unlike Time
|
# Date.new enforces days per month, unlike Time
|
||||||
date = Date.new(*time_array[0..2]) unless type == :time
|
Date.new(*time_array[0..2]) unless type == :time
|
||||||
return date if type == :date
|
|
||||||
|
|
||||||
# Check time part, and return time object
|
# Create time object which checks time part, and return time object
|
||||||
Time.local(*time_array) rescue DateTime.new(*time_array[0..5])
|
make_time(time_array)
|
||||||
rescue
|
rescue
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
@ -107,7 +104,7 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Validate value against the temoSpral restrictions. Restriction values
|
# Validate value against the temopral restrictions. Restriction values
|
||||||
# maybe of mixed type, so the are evaluated as a common type, which may
|
# maybe of mixed type, so the are evaluated as a common type, which may
|
||||||
# require conversion. The type used is defined by validation type.
|
# require conversion. The type used is defined by validation type.
|
||||||
def validate_timeliness_restrictions(record, attr_name, value, configuration)
|
def validate_timeliness_restrictions(record, attr_name, value, configuration)
|
||||||
@ -152,7 +149,23 @@ module ValidatesTimeliness
|
|||||||
defaults.each {|k, v| messages["#{k}_message".to_sym] = v }
|
defaults.each {|k, v| messages["#{k}_message".to_sym] = v }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Create time in correct timezone. For Rails 2.1 that is value in
|
||||||
|
# Time.zone. Rails 2.0 should be default_timezone.
|
||||||
|
def make_time(time_array)
|
||||||
|
if Time.respond_to?(:zone)
|
||||||
|
Time.zone.local(*time_array)
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
Time.send(ActiveRecord::Base.default_timezone, *time_array)
|
||||||
|
rescue ArgumentError, TypeError
|
||||||
|
zone_offset = ActiveRecord::Base.default_timezone == :local ? DateTime.local_offset : 0
|
||||||
|
time_array.pop # remove microseconds
|
||||||
|
DateTime.civil(*(time_array << zone_offset))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user