Skip to content

Commit a1367da

Browse files
Merge pull request rails#46678 from kryzhovnik/main
Fix Issue rails#46677
2 parents e90bc0d + c20b629 commit a1367da

File tree

8 files changed

+53
-11
lines changed

8 files changed

+53
-11
lines changed

actionview/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
* Datatime form helpers (`time_field`, `date_field`, `datetime_field`, `week_field`, `month_field`) now accept an instance of Time/Date/DateTime as `:value` option.
2+
3+
Before:
4+
```erb
5+
<%= form.datetime_field :written_at, value: Time.current.strftime("%Y-%m-%dT%T") %>
6+
```
7+
8+
After:
9+
```erb
10+
<%= form.datetime_field :written_at, value: Time.current %>
11+
```
12+
13+
*Andrey Samsonov*
14+
115
* Choices of `select` can optionally contain html attributes as the last element
216
of the child arrays when using grouped/nested collections
317

actionview/lib/action_view/helpers/tags/date_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Helpers
55
module Tags # :nodoc:
66
class DateField < DatetimeField # :nodoc:
77
private
8-
def format_date(value)
8+
def format_datetime(value)
99
value&.strftime("%Y-%m-%d")
1010
end
1111
end

actionview/lib/action_view/helpers/tags/datetime_field.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ module Tags # :nodoc:
66
class DatetimeField < TextField # :nodoc:
77
def render
88
options = @options.stringify_keys
9-
options["value"] ||= format_date(value)
10-
options["min"] = format_date(datetime_value(options["min"]))
11-
options["max"] = format_date(datetime_value(options["max"]))
9+
options["value"] = normalize_datetime(options["value"] || value)
10+
options["min"] = normalize_datetime(options["min"])
11+
options["max"] = normalize_datetime(options["max"])
1212
@options = options
1313
super
1414
end
1515

1616
private
17-
def format_date(value)
17+
def format_datetime(value)
1818
raise NotImplementedError
1919
end
2020

21-
def datetime_value(value)
21+
def normalize_datetime(value)
22+
format_datetime(parse_datetime(value))
23+
end
24+
25+
def parse_datetime(value)
2226
if value.is_a? String
2327
DateTime.parse(value) rescue nil
2428
else

actionview/lib/action_view/helpers/tags/datetime_local_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def field_type
1616
end
1717

1818
private
19-
def format_date(value)
19+
def format_datetime(value)
2020
if @include_seconds
2121
value&.strftime("%Y-%m-%dT%T")
2222
else

actionview/lib/action_view/helpers/tags/month_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Helpers
55
module Tags # :nodoc:
66
class MonthField < DatetimeField # :nodoc:
77
private
8-
def format_date(value)
8+
def format_datetime(value)
99
value&.strftime("%Y-%m")
1010
end
1111
end

actionview/lib/action_view/helpers/tags/time_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(object_name, method_name, template_object, options = {})
1010
end
1111

1212
private
13-
def format_date(value)
13+
def format_datetime(value)
1414
if @include_seconds
1515
value&.strftime("%T.%L")
1616
else

actionview/lib/action_view/helpers/tags/week_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Helpers
55
module Tags # :nodoc:
66
class WeekField < DatetimeField # :nodoc:
77
private
8-
def format_date(value)
8+
def format_datetime(value)
99
value&.strftime("%Y-W%V")
1010
end
1111
end

actionview/test/template/form_helper_test.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,12 @@ def test_date_field_with_value_attr
11021102
assert_dom_equal(expected, date_field("post", "written_on", value: value))
11031103
end
11041104

1105+
def test_date_field_with_datetime_value_attr
1106+
expected = %{<input id="post_written_on" name="post[written_on]" type="date" value="2013-06-29" />}
1107+
value = DateTime.new(2013, 6, 29)
1108+
assert_dom_equal(expected, date_field("post", "written_on", value: value))
1109+
end
1110+
11051111
def test_date_field_with_timewithzone_value
11061112
previous_time_zone, Time.zone = Time.zone, "UTC"
11071113
expected = %{<input id="post_written_on" name="post[written_on]" type="date" value="2004-06-15" />}
@@ -1153,6 +1159,12 @@ def test_time_field_with_extra_attrs
11531159
assert_dom_equal(expected, time_field("post", "written_on", min: min_value, max: max_value, step: step))
11541160
end
11551161

1162+
def test_time_field_with_value_attr
1163+
expected = %{<input id="post_written_on" name="post[written_on]" type="time" value="01:02:03.000" />}
1164+
value = DateTime.new(2004, 6, 15, 1, 2, 3)
1165+
assert_dom_equal(expected, time_field("post", "written_on", value: value))
1166+
end
1167+
11561168
def test_time_field_with_timewithzone_value
11571169
previous_time_zone, Time.zone = Time.zone, "UTC"
11581170
expected = %{<input id="post_written_on" name="post[written_on]" type="time" value="01:02:03.000" />}
@@ -1213,7 +1225,7 @@ def test_datetime_field_with_extra_attrs
12131225
end
12141226

12151227
def test_datetime_field_with_value_attr
1216-
expected = %{<input id="post_written_on" name="post[written_on]" type="datetime-local" value="2013-06-29T13:37:00+00:00" />}
1228+
expected = %{<input id="post_written_on" name="post[written_on]" type="datetime-local" value="2013-06-29T13:37:00" />}
12171229
value = DateTime.new(2013, 6, 29, 13, 37)
12181230
assert_dom_equal(expected, datetime_field("post", "written_on", value: value))
12191231
end
@@ -1285,6 +1297,12 @@ def test_month_field_with_extra_attrs
12851297
assert_dom_equal(expected, month_field("post", "written_on", min: min_value, max: max_value, step: step))
12861298
end
12871299

1300+
def test_month_field_with_datetime_value_attr
1301+
expected = %{<input id="post_written_on" name="post[written_on]" type="month" value="2004-06" />}
1302+
value = DateTime.new(2004, 6, 15, 1, 2, 3)
1303+
assert_dom_equal(expected, month_field("post", "written_on", value: value))
1304+
end
1305+
12881306
def test_month_field_with_timewithzone_value
12891307
previous_time_zone, Time.zone = Time.zone, "UTC"
12901308
expected = %{<input id="post_written_on" name="post[written_on]" type="month" value="2004-06" />}
@@ -1320,6 +1338,12 @@ def test_week_field_with_extra_attrs
13201338
assert_dom_equal(expected, week_field("post", "written_on", min: min_value, max: max_value, step: step))
13211339
end
13221340

1341+
def test_week_field_with_datetime_value_attr
1342+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
1343+
value = DateTime.new(2004, 6, 15, 1, 2, 3)
1344+
assert_dom_equal(expected, week_field("post", "written_on", value: value))
1345+
end
1346+
13231347
def test_week_field_with_timewithzone_value
13241348
previous_time_zone, Time.zone = Time.zone, "UTC"
13251349
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}

0 commit comments

Comments
 (0)