From 44266fe11ed71edf9c18674eacdba80094f5942f Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Sun, 12 Dec 2010 02:29:37 +0700 Subject: [PATCH] Add field and tag helpers for HTML5 date input types Complementing the already-existing helpers for HTML5 input fields like email, url, telephone, range, etc., this adds simple helpers for date, month, week, time, datetime and datetime-local input types. By virtue of the usual helper support for passing HTML options, you may specify attributes for these inputs: <%= date_field :todo, :due_date, :min => Date.today.rfc3339 %> --- actionpack/lib/action_view/helpers/form_helper.rb | 30 ++++++++++++ .../lib/action_view/helpers/form_tag_helper.rb | 48 ++++++++++++++++++++ actionpack/test/template/form_helper_test.rb | 30 ++++++++++++ actionpack/test/template/form_tag_helper_test.rb | 30 ++++++++++++ 4 files changed, 138 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index ef5bbd8..e492a8a 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -822,6 +822,36 @@ module ActionView InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("email", options) end + # Returns a text_field of type "date". + def date_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("date", options) + end + + # Returns a text_field of type "month". + def month_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("month", options) + end + + # Returns a text_field of type "week". + def week_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("week", options) + end + + # Returns a text_field of type "time". + def time_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("time", options) + end + + # Returns a text_field of type "datetime". + def datetime_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("datetime", options) + end + + # Returns a text_field of type "datetime-local". + def datetime_local_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("datetime-local", options) + end + # Returns an input tag of type "number". # # ==== Options diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 50f065f..fad3728 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -491,6 +491,54 @@ module ActionView text_field_tag(name, value, options.stringify_keys.update("type" => "email")) end + # Creates a text field of type "date". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def date_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "date")) + end + + # Creates a text field of type "month". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def month_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "month")) + end + + # Creates a text field of type "week". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def week_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "week")) + end + + # Creates a text field of type "time". + # + # ==== Options + # * 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 "datetime". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def datetime_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "datetime")) + end + + # Creates a text field of type "datetime-local". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def datetime_local_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "datetime-local")) + end + # Creates a number field. # # ==== Options diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 2c60096..a040d60 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -426,6 +426,36 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal(expected, email_field("user", "address")) end + def test_date_field + expected = %{} + assert_dom_equal(expected, date_field("user", "birthday")) + end + + def test_month_field + expected = %{} + assert_dom_equal(expected, month_field("promotion", "period")) + end + + def test_week_field + expected = %{} + assert_dom_equal(expected, week_field("weekly_report", "term")) + end + + def test_time_field + expected = %{} + assert_dom_equal(expected, time_field("meal", "served_at")) + end + + def test_datetime_field + expected = %{} + assert_dom_equal(expected, datetime_field("todo", "due_at")) + end + + def test_datetime_local_field + expected = %{} + assert_dom_equal(expected, datetime_local_field("event", "when")) + end + def test_number_field expected = %{} assert_dom_equal(expected, number_field("order", "quantity", :in => 1...10)) diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index f3933a2..7eccc3f 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -412,6 +412,36 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal(expected, email_field_tag("address")) end + def test_date_field_tag + expected = %{} + assert_dom_equal(expected, date_field_tag("birthday")) + end + + def test_month_field_tag + expected = %{} + assert_dom_equal(expected, month_field_tag("promotion_period")) + end + + def test_week_field_tag + expected = %{} + assert_dom_equal(expected, week_field_tag("report_term")) + end + + def test_time_field_tag + expected = %{} + assert_dom_equal(expected, time_field_tag("served_at")) + end + + def test_datetime_field_tag + expected = %{} + assert_dom_equal(expected, datetime_field_tag("due_at")) + end + + def test_datetime_local_field_tag + expected = %{} + assert_dom_equal(expected, datetime_local_field_tag("when")) + end + def test_number_field_tag expected = %{} assert_dom_equal(expected, number_field_tag("quantity", nil, :in => 1...10)) -- 1.7.1