From a9a9e8a3cb64faddc347271c00f975e04a92e394 Mon Sep 17 00:00:00 2001 From: Carlos Galdino Date: Mon, 21 May 2012 18:52:56 -0300 Subject: Refactor date related helpers --- actionpack/lib/action_view/helpers/tags/date_field.rb | 13 ++++++------- .../lib/action_view/helpers/tags/datetime_field.rb | 8 ++++---- .../lib/action_view/helpers/tags/datetime_local_field.rb | 16 ++++++---------- actionpack/lib/action_view/helpers/tags/month_field.rb | 13 ++----------- actionpack/lib/action_view/helpers/tags/time_field.rb | 13 ++++++------- actionpack/lib/action_view/helpers/tags/week_field.rb | 13 ++----------- 6 files changed, 26 insertions(+), 50 deletions(-) diff --git a/actionpack/lib/action_view/helpers/tags/date_field.rb b/actionpack/lib/action_view/helpers/tags/date_field.rb index 0e79609d52..64c29dea3d 100644 --- a/actionpack/lib/action_view/helpers/tags/date_field.rb +++ b/actionpack/lib/action_view/helpers/tags/date_field.rb @@ -1,13 +1,12 @@ module ActionView module Helpers module Tags - class DateField < TextField #:nodoc: - def render - options = @options.stringify_keys - options["value"] = @options.fetch("value") { value(object).try(:to_date) } - @options = options - super - end + class DateField < DatetimeField #:nodoc: + private + + def format_date(value) + value.try(:strftime, "%Y-%m-%d") + end end end end diff --git a/actionpack/lib/action_view/helpers/tags/datetime_field.rb b/actionpack/lib/action_view/helpers/tags/datetime_field.rb index 11d58744fd..e407146e96 100644 --- a/actionpack/lib/action_view/helpers/tags/datetime_field.rb +++ b/actionpack/lib/action_view/helpers/tags/datetime_field.rb @@ -4,16 +4,16 @@ module ActionView class DatetimeField < TextField #:nodoc: def render options = @options.stringify_keys - options["value"] = @options.fetch("value") { format_global_date_time_string(value(object)) } - options["min"] = format_global_date_time_string(options["min"]) - options["max"] = format_global_date_time_string(options["max"]) + options["value"] = @options.fetch("value") { format_date(value(object)) } + options["min"] = format_date(options["min"]) + options["max"] = format_date(options["max"]) @options = options super end private - def format_global_date_time_string(value) + def format_date(value) value.try(:strftime, "%Y-%m-%dT%T.%L%z") end end diff --git a/actionpack/lib/action_view/helpers/tags/datetime_local_field.rb b/actionpack/lib/action_view/helpers/tags/datetime_local_field.rb index 7593a3c733..6668d6d718 100644 --- a/actionpack/lib/action_view/helpers/tags/datetime_local_field.rb +++ b/actionpack/lib/action_view/helpers/tags/datetime_local_field.rb @@ -1,20 +1,16 @@ module ActionView module Helpers module Tags - class DatetimeLocalField < TextField #:nodoc: - def render - options = @options.stringify_keys - options["type"] = "datetime-local" - options["value"] = @options.fetch("value") { format_local_date_time_string(value(object)) } - options["min"] = format_local_date_time_string(options["min"]) - options["max"] = format_local_date_time_string(options["max"]) - @options = options - super + class DatetimeLocalField < DatetimeField #:nodoc: + class << self + def field_type + @field_type ||= "datetime-local" + end end private - def format_local_date_time_string(value) + def format_date(value) value.try(:strftime, "%Y-%m-%dT%T") end end diff --git a/actionpack/lib/action_view/helpers/tags/month_field.rb b/actionpack/lib/action_view/helpers/tags/month_field.rb index 56bd85a90b..3d3c32d847 100644 --- a/actionpack/lib/action_view/helpers/tags/month_field.rb +++ b/actionpack/lib/action_view/helpers/tags/month_field.rb @@ -1,19 +1,10 @@ module ActionView module Helpers module Tags - class MonthField < TextField #:nodoc: - def render - options = @options.stringify_keys - options["value"] = @options.fetch("value") { format_month_string(value(object)) } - options["min"] = format_month_string(options["min"]) - options["max"] = format_month_string(options["max"]) - @options = options - super - end - + class MonthField < DatetimeField #:nodoc: private - def format_month_string(value) + def format_date(value) value.try(:strftime, "%Y-%m") end end diff --git a/actionpack/lib/action_view/helpers/tags/time_field.rb b/actionpack/lib/action_view/helpers/tags/time_field.rb index 271dc00c54..a3941860c9 100644 --- a/actionpack/lib/action_view/helpers/tags/time_field.rb +++ b/actionpack/lib/action_view/helpers/tags/time_field.rb @@ -1,13 +1,12 @@ 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 + class TimeField < DatetimeField #:nodoc: + private + + def format_date(value) + value.try(:strftime, "%T.%L") + end end end end diff --git a/actionpack/lib/action_view/helpers/tags/week_field.rb b/actionpack/lib/action_view/helpers/tags/week_field.rb index a1265d9928..1e13939a0a 100644 --- a/actionpack/lib/action_view/helpers/tags/week_field.rb +++ b/actionpack/lib/action_view/helpers/tags/week_field.rb @@ -1,19 +1,10 @@ module ActionView module Helpers module Tags - class WeekField < TextField #:nodoc: - def render - options = @options.stringify_keys - options["value"] = @options.fetch("value") { format_week_string(value(object)) } - options["min"] = format_week_string(options["min"]) - options["max"] = format_week_string(options["max"]) - @options = options - super - end - + class WeekField < DatetimeField #:nodoc: private - def format_week_string(value) + def format_date(value) value.try(:strftime, "%Y-W%W") end end -- cgit v1.2.3 From 30847b499b3c824864c9652f6d12ceb8219b363d Mon Sep 17 00:00:00 2001 From: Carlos Galdino Date: Tue, 22 May 2012 21:04:12 -0300 Subject: Add tests for time_field and date_field helpers These tests check the values of 'min' and 'max' input attrs --- actionpack/test/template/form_helper_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 97c8025db3..c9b39ed18f 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -644,6 +644,15 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal(expected, date_field("post", "written_on")) end + def test_date_field_with_extra_attrs + expected = %{} + @post.written_on = DateTime.new(2004, 6, 15) + min_value = DateTime.new(2000, 6, 15) + max_value = DateTime.new(2010, 8, 15) + step = 2 + assert_dom_equal(expected, date_field("post", "written_on", :min => min_value, :max => max_value, :step => step)) + end + def test_date_field_with_timewithzone_value previous_time_zone, Time.zone = Time.zone, 'UTC' expected = %{} @@ -670,6 +679,15 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal(expected, time_field("post", "written_on")) end + def test_time_field_with_extra_attrs + expected = %{} + @post.written_on = DateTime.new(2004, 6, 15, 1, 2, 3) + min_value = DateTime.new(2000, 6, 15, 20, 45, 30) + max_value = DateTime.new(2010, 8, 15, 10, 25, 00) + step = 60 + assert_dom_equal(expected, time_field("post", "written_on", :min => min_value, :max => max_value, :step => step)) + end + def test_time_field_with_timewithzone_value previous_time_zone, Time.zone = Time.zone, 'UTC' expected = %{} -- cgit v1.2.3