From ce58e5cd069ef1341b8a60be1690b2a912a0b85f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 2 Jan 2005 15:32:01 +0000 Subject: Added DateHelper#select_time and DateHelper#select_second #373 [Scott Baron] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@311 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + actionpack/lib/action_view/helpers/date_helper.rb | 20 +++++++++ actionpack/test/template/date_helper_test.rb | 53 +++++++++++++++++++++++ 3 files changed, 75 insertions(+) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index f4170fc84f..cabfe52d15 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added DateHelper#select_time and DateHelper#select_second #373 [Scott Baron] + * Added class declaration for the MissingFile exception #388 [Kent Sibilev] * Added "short hypertext note with a hyperlink to the new URI(s)" to redirects to fulfill compliance with RFC 2616 (HTTP/1.1) section 10.3.3 #397 [Tim Bates] diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 58dab783d5..f8c19d169d 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -76,6 +76,26 @@ module ActionView select_hour(datetime, options) + select_minute(datetime, options) end + # Returns a set of html select-tags (one for hour and minute) + def select_time(datetime = Time.now, options = {}) + h = select_hour(datetime, options) + select_minute(datetime, options) + (options[:include_seconds] ? select_second(datetime, options) : '') + end + + # Returns a select tag with options for each of the seconds 0 through 59 with the current second selected. + # The second can also be substituted for a second number. + def select_second(datetime, options = {}) + second_options = [] + + 0.upto(59) do |second| + second_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.sec) == second ? + "\n" : + "\n" + ) + end + + select_html("second", second_options, options[:prefix], options[:include_blank], options[:discard_type]) + end + # Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected. # The minute can also be substituted for a minute number. def select_minute(datetime, options = {}) diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index fcc2da9655..e30853f42a 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -81,6 +81,30 @@ class DateHelperTest < Test::Unit::TestCase assert_equal expected, select_year( 2003, :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005) end + + def test_select_hour + expected = %(\n" + + assert_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18)) + end + + def test_select_minute + expected = %(\n" + + assert_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18)) + end + + def test_select_second + expected = %(\n" + + assert_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18)) + end def test_select_date @@ -177,6 +201,35 @@ class DateHelperTest < Test::Unit::TestCase ) end + def test_select_time_with_seconds + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), :include_seconds => true) + end + + def test_select_time_without_seconds + expected = %(\n" + + expected << %(\n" + + assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18)) + assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), :include_seconds => false) + end + def test_date_select_with_zero_value expected = %(