Skip to content

Commit

Permalink
Allow a value formatted as HH:MM for time_field
Browse files Browse the repository at this point in the history
#46678 introduced a regression, if you do the following:

```ruby
= time_field(model, attr, value: "01:45")
```

Previously it would render an input with `value="01:45"`, since #46678 it renders a value with seconds (`value="01:45:00.000"`). This is a regression from Rails 7. See #41728 for why you might want the value to be rendered without seconds.

Co-authored-by: jonathanhefner <jonathan@hefner.pro>
  • Loading branch information
ghiculescu and jonathanhefner committed Apr 8, 2023
1 parent a9506eb commit 2d89108
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 12 additions & 8 deletions actionview/lib/action_view/helpers/tags/datetime_field.rb
Expand Up @@ -6,24 +6,28 @@ module Tags # :nodoc:
class DatetimeField < TextField # :nodoc:
def render
options = @options.stringify_keys
options["value"] = normalize_datetime(options["value"] || value)
options["min"] = normalize_datetime(options["min"])
options["max"] = normalize_datetime(options["max"])
options["value"] = datetime_value(options["value"] || value)
options["min"] = format_datetime(parse_datetime(options["min"]))
options["max"] = format_datetime(parse_datetime(options["max"]))
@options = options
super
end

private
def format_datetime(value)
raise NotImplementedError
def datetime_value(value)
if value.is_a?(String)
value
else
format_datetime(value)
end
end

def normalize_datetime(value)
format_datetime(parse_datetime(value))
def format_datetime(value)
raise NotImplementedError
end

def parse_datetime(value)
if value.is_a? String
if value.is_a?(String)
DateTime.parse(value) rescue nil
else
value
Expand Down
10 changes: 10 additions & 0 deletions actionview/test/template/form_helper_test.rb
Expand Up @@ -1165,6 +1165,11 @@ def test_time_field_with_value_attr
assert_dom_equal(expected, time_field("post", "written_on", value: value))
end

def test_time_field_with_value_attr_that_excludes_seconds
expected = %{<input id="post_written_on" name="post[written_on]" type="time" value="01:45" />}
assert_dom_equal(expected, time_field("post", "written_on", value: "01:45"))
end

def test_time_field_with_timewithzone_value
previous_time_zone, Time.zone = Time.zone, "UTC"
expected = %{<input id="post_written_on" name="post[written_on]" type="time" value="01:02:03.000" />}
Expand Down Expand Up @@ -1271,6 +1276,11 @@ def test_datetime_local_field_without_seconds
assert_dom_equal(expected, datetime_local_field("post", "written_on", include_seconds: false))
end

def test_datetime_local_field_with_value_attr_that_excludes_seconds
expected = %{<input id="post_written_on" name="post[written_on]" type="datetime-local" value="2004-06-15T00:00" />}
assert_dom_equal(expected, datetime_local_field("post", "written_on", value: "2004-06-15T00:00"))
end

def test_month_field
expected = %{<input id="post_written_on" name="post[written_on]" type="month" value="2004-06" />}
assert_dom_equal(expected, month_field("post", "written_on"))
Expand Down

0 comments on commit 2d89108

Please sign in to comment.