Skip to content

Commit 4ea0670

Browse files
committed
Merge pull request #29651 from Sayanc93/return-correct-date
Return correct date in ActiveModel for time to date conversions
2 parents 1dfd014 + eb73dfc commit 4ea0670

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

activemodel/CHANGELOG.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
* Fix year value when casting a multiparameter time hash
1+
* Fix date value when casting a multiparameter date hash to not convert
2+
from Gregorian date to Julian date.
3+
4+
Before:
5+
6+
Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
7+
=> #<Day id: nil, day: "0001-01-03", created_at: nil, updated_at: nil>
8+
9+
After:
10+
11+
Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
12+
=> #<Day id: nil, day: "0001-01-01", created_at: nil, updated_at: nil>
13+
14+
Fixes #28521.
15+
16+
*Sayan Chakraborty*
17+
18+
* Fix year value when casting a multiparameter time hash.
219

320
When assigning a hash to a time attribute that's missing a year component
421
(e.g. a `time_select` with `:ignore_date` set to `true`) then the year

activemodel/lib/active_model/type/date.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def new_date(year, mon, mday)
4646

4747
def value_from_multiparameter_assignment(*)
4848
time = super
49-
time && time.to_date
49+
time && new_date(time.year, time.mon, time.mday)
5050
end
5151
end
5252
end

activemodel/test/cases/type/date_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def test_type_cast_date
1818
assert_equal date_string, type.cast(date_string).strftime("%F")
1919
assert_equal date_string, type.cast(values_hash).strftime("%F")
2020
end
21+
22+
def test_returns_correct_year
23+
type = Type::Date.new
24+
25+
time = ::Time.utc(1, 1, 1)
26+
date = ::Date.new(time.year, time.mon, time.mday)
27+
28+
values_hash_for_multiparameter_assignment = { 1 => 1, 2 => 1, 3 => 1 }
29+
30+
assert_equal date, type.cast(values_hash_for_multiparameter_assignment)
31+
end
2132
end
2233
end
2334
end

0 commit comments

Comments
 (0)