diff options
author | Alex Soulim <soulim@gmail.com> | 2012-04-03 16:01:00 +0400 |
---|---|---|
committer | Alex Soulim <soulim@gmail.com> | 2012-05-18 08:38:19 +0400 |
commit | 446940c366d588e7e895a64caf325f5f99cba0fb (patch) | |
tree | 413b070e34022ddf4a8889b0045dd4582a8a65bc /actionpack/lib | |
parent | b23ac936a6ef980547666da002681a77d8340573 (diff) | |
download | rails-446940c366d588e7e895a64caf325f5f99cba0fb.tar.gz rails-446940c366d588e7e895a64caf325f5f99cba0fb.tar.bz2 rails-446940c366d588e7e895a64caf325f5f99cba0fb.zip |
Add HTML5 input[type="time"] helper
Diffstat (limited to 'actionpack/lib')
-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 |
4 files changed, 43 insertions, 0 deletions
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 |