diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2012-05-17 23:13:39 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2012-05-17 23:13:39 -0700 |
commit | d8e8ab1c5351a3b8707143a787fed514b75a4144 (patch) | |
tree | 413b070e34022ddf4a8889b0045dd4582a8a65bc /actionpack | |
parent | b23ac936a6ef980547666da002681a77d8340573 (diff) | |
parent | 446940c366d588e7e895a64caf325f5f99cba0fb (diff) | |
download | rails-d8e8ab1c5351a3b8707143a787fed514b75a4144.tar.gz rails-d8e8ab1c5351a3b8707143a787fed514b75a4144.tar.bz2 rails-d8e8ab1c5351a3b8707143a787fed514b75a4144.zip |
Merge pull request #5715 from soulim/add_time_input_control
Add HTML5 input[type="time"] helper
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG.md | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 17 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_tag_helper.rb | 11 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/tags.rb | 1 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/tags/time_field.rb | 14 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 26 | ||||
-rw-r--r-- | actionpack/test/template/form_tag_helper_test.rb | 5 |
7 files changed, 76 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 3f2593710e..41af29573e 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Add `time_field` and `time_field_tag` helpers which render an `input[type="time"]` tag. *Alex Soulim* + * Removed old text_helper apis for highlight, excerpt and word_wrap *Jeremy Walker* * Templates without a handler extension now raises a deprecation warning but still diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index cc1f133196..6510610034 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -990,6 +990,23 @@ module ActionView Tags::DateField.new(object_name, method, self, options).render end + # Returns a text_field of type "time". + # + # The default value is generated by trying to call +strftime+ with "%T.%L" + # on the objects's value. It is still possible to override that + # by passing the "value" option. + # + # === Options + # * Accepts same options as time_field_tag + # + # === Example + # time_field("task", "started_at") + # # => <input id="task_started_at" name="task[started_at]" type="time" /> + # + def time_field(object_name, method, options = {}) + Tags::TimeField.new(object_name, method, self, options).render + end + # Returns a text_field of type "url". # # url_field("user", "homepage") diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 9e5c66f4a9..e65b4e3e95 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -549,6 +549,17 @@ module ActionView text_field_tag(name, value, options.stringify_keys.update("type" => "date")) end + # Creates a text field of type "time". + # + # === Options + # * <tt>:min</tt> - The minimum acceptable value. + # * <tt>:max</tt> - The maximum acceptable value. + # * <tt>:step</tt> - The acceptable value granularity. + # * Otherwise accepts the same options as text_field_tag. + def time_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "time")) + end + # Creates a text field of type "url". # # ==== Options diff --git a/actionpack/lib/action_view/helpers/tags.rb b/actionpack/lib/action_view/helpers/tags.rb index 3cf762877f..5cd77c8ec3 100644 --- a/actionpack/lib/action_view/helpers/tags.rb +++ b/actionpack/lib/action_view/helpers/tags.rb @@ -25,6 +25,7 @@ module ActionView autoload :TelField autoload :TextArea autoload :TextField + autoload :TimeField autoload :TimeSelect autoload :TimeZoneSelect autoload :UrlField diff --git a/actionpack/lib/action_view/helpers/tags/time_field.rb b/actionpack/lib/action_view/helpers/tags/time_field.rb new file mode 100644 index 0000000000..271dc00c54 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/time_field.rb @@ -0,0 +1,14 @@ +module ActionView + module Helpers + module Tags + class TimeField < TextField #:nodoc: + def render + options = @options.stringify_keys + options["value"] = @options.fetch("value") { value(object).try(:strftime, "%T.%L") } + @options = options + super + end + end + end + end +end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index beb3ea752a..27cc3ad48a 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -646,6 +646,32 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal(expected, date_field("post", "written_on")) end + def test_time_field + expected = %{<input id="post_written_on" name="post[written_on]" type="time" value="00:00:00.000" />} + assert_dom_equal(expected, time_field("post", "written_on")) + end + + def test_time_field_with_datetime_value + expected = %{<input id="post_written_on" name="post[written_on]" type="time" value="01:02:03.000" />} + @post.written_on = DateTime.new(2004, 6, 15, 1, 2, 3) + assert_dom_equal(expected, time_field("post", "written_on")) + 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" />} + @post.written_on = Time.zone.parse('2004-06-15 01:02:03') + assert_dom_equal(expected, time_field("post", "written_on")) + ensure + Time.zone = previous_time_zone + end + + def test_time_field_with_nil_value + expected = %{<input id="post_written_on" name="post[written_on]" type="time" />} + @post.written_on = nil + assert_dom_equal(expected, time_field("post", "written_on")) + end + def test_url_field expected = %{<input id="user_homepage" name="user[homepage]" type="url" />} assert_dom_equal(expected, url_field("user", "homepage")) diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 7a645217b8..6574e13558 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -459,6 +459,11 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal(expected, date_field_tag("cell")) end + def test_time_field_tag + expected = %{<input id="cell" name="cell" type="time" />} + assert_dom_equal(expected, time_field_tag("cell")) + end + def test_url_field_tag expected = %{<input id="homepage" name="homepage" type="url" />} assert_dom_equal(expected, url_field_tag("homepage")) |