parse_date_time method returns for date column to return Date object when assigned string as per normal Rails

parse_date_time method returns same object when assigned Date or Time object
This commit is contained in:
Adam Meehan 2008-08-22 15:12:38 +10:00
parent aecaa7baca
commit 0ef34c7d28
3 changed files with 23 additions and 4 deletions

View File

@ -1,5 +1,7 @@
[2008-08-22] [2008-08-22]
- fixed bug with attribute cache not clearing on write for date and time columns - fixed bug with attribute cache not clearing on write for date and time columns [reported by Sylvestre Mergulhão]
- return Date object for date column attribute reader when assigned string as per normal Rails behaviour
- attribute reader returns same object type when assigned Date or Time object as per normal Rails behaviour
[2008-08-07] [2008-08-07]
- modified matcher option value parsing to allow same value types as validation method - modified matcher option value parsing to allow same value types as validation method

View File

@ -34,7 +34,7 @@ module ValidatesTimeliness
def parse_date_time(raw_value, type, strict=true) def parse_date_time(raw_value, type, strict=true)
return nil if raw_value.blank? return nil if raw_value.blank?
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date) return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
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?
@ -43,7 +43,9 @@ module ValidatesTimeliness
time_array[0..2] = 2000, 1, 1 if type == :time time_array[0..2] = 2000, 1, 1 if type == :time
# Date.new enforces days per month, unlike Time # Date.new enforces days per month, unlike Time
Date.new(*time_array[0..2]) unless type == :time date = Date.new(*time_array[0..2]) unless type == :time
return date if type == :date
# Create time object which checks time part, and return time object # Create time object which checks time part, and return time object
make_time(time_array) make_time(time_array)

View File

@ -34,11 +34,26 @@ describe ValidatesTimeliness::AttributeMethods do
@person.birth_date_and_time_before_type_cast.should be_kind_of(Time) @person.birth_date_and_time_before_type_cast.should be_kind_of(Time)
end end
it "should return Time object using attribute read method when written with string" do it "should return Time object for datetime attribute read method when assigned Time object" do
@person.birth_date_and_time = Time.now
@person.birth_date_and_time.should be_kind_of(Time)
end
it "should return Time object for datetime attribute read method when assigned string" do
@person.birth_date_and_time = "2000-01-01 02:03:04" @person.birth_date_and_time = "2000-01-01 02:03:04"
@person.birth_date_and_time.should be_kind_of(Time) @person.birth_date_and_time.should be_kind_of(Time)
end end
it "should return Date object for date attribute read method when assigned Date object" do
@person.birth_date = Date.today
@person.birth_date.should be_kind_of(Date)
end
it "should return Date object for date attribute read method when assigned string" do
@person.birth_date = '2000-01-01'
@person.birth_date.should be_kind_of(Date)
end
it "should return nil when time is invalid" do it "should return nil when time is invalid" do
@person.birth_date_and_time = "2000-01-32 02:03:04" @person.birth_date_and_time = "2000-01-32 02:03:04"
@person.birth_date_and_time.should be_nil @person.birth_date_and_time.should be_nil