mirror of https://github.com/rails/rails
commit
a1367da11d
|
@ -1,3 +1,17 @@
|
|||
* Datatime form helpers (`time_field`, `date_field`, `datetime_field`, `week_field`, `month_field`) now accept an instance of Time/Date/DateTime as `:value` option.
|
||||
|
||||
Before:
|
||||
```erb
|
||||
<%= form.datetime_field :written_at, value: Time.current.strftime("%Y-%m-%dT%T") %>
|
||||
```
|
||||
|
||||
After:
|
||||
```erb
|
||||
<%= form.datetime_field :written_at, value: Time.current %>
|
||||
```
|
||||
|
||||
*Andrey Samsonov*
|
||||
|
||||
* Choices of `select` can optionally contain html attributes as the last element
|
||||
of the child arrays when using grouped/nested collections
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ module ActionView
|
|||
module Tags # :nodoc:
|
||||
class DateField < DatetimeField # :nodoc:
|
||||
private
|
||||
def format_date(value)
|
||||
def format_datetime(value)
|
||||
value&.strftime("%Y-%m-%d")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,19 +6,23 @@ module ActionView
|
|||
class DatetimeField < TextField # :nodoc:
|
||||
def render
|
||||
options = @options.stringify_keys
|
||||
options["value"] ||= format_date(value)
|
||||
options["min"] = format_date(datetime_value(options["min"]))
|
||||
options["max"] = format_date(datetime_value(options["max"]))
|
||||
options["value"] = normalize_datetime(options["value"] || value)
|
||||
options["min"] = normalize_datetime(options["min"])
|
||||
options["max"] = normalize_datetime(options["max"])
|
||||
@options = options
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
def format_date(value)
|
||||
def format_datetime(value)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def datetime_value(value)
|
||||
def normalize_datetime(value)
|
||||
format_datetime(parse_datetime(value))
|
||||
end
|
||||
|
||||
def parse_datetime(value)
|
||||
if value.is_a? String
|
||||
DateTime.parse(value) rescue nil
|
||||
else
|
||||
|
|
|
@ -16,7 +16,7 @@ module ActionView
|
|||
end
|
||||
|
||||
private
|
||||
def format_date(value)
|
||||
def format_datetime(value)
|
||||
if @include_seconds
|
||||
value&.strftime("%Y-%m-%dT%T")
|
||||
else
|
||||
|
|
|
@ -5,7 +5,7 @@ module ActionView
|
|||
module Tags # :nodoc:
|
||||
class MonthField < DatetimeField # :nodoc:
|
||||
private
|
||||
def format_date(value)
|
||||
def format_datetime(value)
|
||||
value&.strftime("%Y-%m")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,7 +10,7 @@ module ActionView
|
|||
end
|
||||
|
||||
private
|
||||
def format_date(value)
|
||||
def format_datetime(value)
|
||||
if @include_seconds
|
||||
value&.strftime("%T.%L")
|
||||
else
|
||||
|
|
|
@ -5,7 +5,7 @@ module ActionView
|
|||
module Tags # :nodoc:
|
||||
class WeekField < DatetimeField # :nodoc:
|
||||
private
|
||||
def format_date(value)
|
||||
def format_datetime(value)
|
||||
value&.strftime("%Y-W%V")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1102,6 +1102,12 @@ class FormHelperTest < ActionView::TestCase
|
|||
assert_dom_equal(expected, date_field("post", "written_on", value: value))
|
||||
end
|
||||
|
||||
def test_date_field_with_datetime_value_attr
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="date" value="2013-06-29" />}
|
||||
value = DateTime.new(2013, 6, 29)
|
||||
assert_dom_equal(expected, date_field("post", "written_on", value: value))
|
||||
end
|
||||
|
||||
def test_date_field_with_timewithzone_value
|
||||
previous_time_zone, Time.zone = Time.zone, "UTC"
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="date" value="2004-06-15" />}
|
||||
|
@ -1153,6 +1159,12 @@ class FormHelperTest < ActionView::TestCase
|
|||
assert_dom_equal(expected, time_field("post", "written_on", min: min_value, max: max_value, step: step))
|
||||
end
|
||||
|
||||
def test_time_field_with_value_attr
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="time" value="01:02:03.000" />}
|
||||
value = DateTime.new(2004, 6, 15, 1, 2, 3)
|
||||
assert_dom_equal(expected, time_field("post", "written_on", value: value))
|
||||
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" />}
|
||||
|
@ -1213,7 +1225,7 @@ class FormHelperTest < ActionView::TestCase
|
|||
end
|
||||
|
||||
def test_datetime_field_with_value_attr
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="datetime-local" value="2013-06-29T13:37:00+00:00" />}
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="datetime-local" value="2013-06-29T13:37:00" />}
|
||||
value = DateTime.new(2013, 6, 29, 13, 37)
|
||||
assert_dom_equal(expected, datetime_field("post", "written_on", value: value))
|
||||
end
|
||||
|
@ -1285,6 +1297,12 @@ class FormHelperTest < ActionView::TestCase
|
|||
assert_dom_equal(expected, month_field("post", "written_on", min: min_value, max: max_value, step: step))
|
||||
end
|
||||
|
||||
def test_month_field_with_datetime_value_attr
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="month" value="2004-06" />}
|
||||
value = DateTime.new(2004, 6, 15, 1, 2, 3)
|
||||
assert_dom_equal(expected, month_field("post", "written_on", value: value))
|
||||
end
|
||||
|
||||
def test_month_field_with_timewithzone_value
|
||||
previous_time_zone, Time.zone = Time.zone, "UTC"
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="month" value="2004-06" />}
|
||||
|
@ -1320,6 +1338,12 @@ class FormHelperTest < ActionView::TestCase
|
|||
assert_dom_equal(expected, week_field("post", "written_on", min: min_value, max: max_value, step: step))
|
||||
end
|
||||
|
||||
def test_week_field_with_datetime_value_attr
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
|
||||
value = DateTime.new(2004, 6, 15, 1, 2, 3)
|
||||
assert_dom_equal(expected, week_field("post", "written_on", value: value))
|
||||
end
|
||||
|
||||
def test_week_field_with_timewithzone_value
|
||||
previous_time_zone, Time.zone = Time.zone, "UTC"
|
||||
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
|
||||
|
|
Loading…
Reference in New Issue