From 4ef72e63711602e5a474b64d08c9aefca97a0f37 Mon Sep 17 00:00:00 2001 From: Colin Law Date: Thu, 15 Jan 2009 12:34:54 +0000 Subject: [PATCH] Added leading_zeros_month_and_day option to date_select and datetime_select --- actionpack/lib/action_view/helpers/date_helper.rb | 29 ++++- actionpack/test/template/date_helper_test.rb | 153 +++++++++++++++++++++ 2 files changed, 178 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 4305617..3f9178f 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -258,6 +258,16 @@ module ActionView # datetime_select("post", "written_on", :prompt => {:hour => true}) # generic prompt for hours # datetime_select("post", "written_on", :prompt => true) # generic prompts for all # + # # Generates a datetime select showing months as numbers that, when POSTed, will be stored in the + # # post variable in the written_on attribute. + # datetime_select("post", "written_on", :use_month_numbers => true) + # + # # Generates a datetime select showing months as numbers and with days and months with leading zeros and + # ordering the date as day, month, year that, when POSTed, will be stored in the post variable in the + # written_on attribute. + # datetime_select("post", "written_on", {:use_month_numbers => true,:leading_zeros_month_and_day =>true, + # :order=>[:day,:month,:year]}) + # # The selects are prepared for multi-parameter assignment to an Active Record object. def datetime_select(object_name, method, options = {}, html_options = {}) InstanceTag.new(object_name, method, self, options.delete(:object)).to_datetime_select_tag(options, html_options) @@ -680,7 +690,9 @@ module ActionView if @options[:use_hidden] || @options[:discard_day] build_hidden(:day, day) else - build_options_and_select(:day, day, :start => 1, :end => 31, :leading_zeros => false) + # :leading_zeros_month_and_day must default to false if it is nil + @options[:leading_zeros_month_and_day] ||= false + build_options_and_select(:day, day, :start => 1, :end => 31, :leading_zeros => @options[:leading_zeros_month_and_day]) end end @@ -757,13 +769,20 @@ module ActionView # If :use_month_numbers option is passed # month_name(1) => 1 # + # if :use_month_numbers and :leading_zeros_month_and_day options are passed + # month_name(1) => 01 + # # If :add_month_numbers option is passed # month_name(1) => "1 - January" + # + # If :add_month_numbers and :leading_zeros_month_and_day options are passed + # month_name(1) => "01 - January" def month_name(number) + number_s = @options[:leading_zeros_month_and_day] ? "%02d" % number : number.to_s if @options[:use_month_numbers] - number + number_s elsif @options[:add_month_numbers] - "#{number} - #{month_names[number]}" + "#{number_s} - #{month_names[number]}" else month_names[number] end @@ -794,7 +813,9 @@ module ActionView start = options.delete(:start) || 0 stop = options.delete(:end) || 59 step = options.delete(:step) || 1 - leading_zeros = options.delete(:leading_zeros).nil? ? true : false + # option leading_zeros nil or true imply leading zeros required + leading_zeros = options.delete(:leading_zeros) + leading_zeros = true if leading_zeros.nil? select_options = [] start.step(stop, step) do |i| diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 6ec01b7..3908141 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -127,6 +127,15 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, select_day(16) end + def test_select_day_with_leading_zeros + expected = %(\n" + + assert_dom_equal expected, select_day(Time.mktime(2003, 8, 2), :leading_zeros_month_and_day => true) + assert_dom_equal expected, select_day(2, :leading_zeros_month_and_day => true) + end + def test_select_day_with_blank expected = %(\n) + expected << %(\n\n\n\n\n\n\n\n\n\n\n\n) + expected << "\n" + + assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :leading_zeros_month_and_day => true) + assert_dom_equal expected, select_month(8, :leading_zeros_month_and_day => true) + end + def test_select_month_with_numbers expected = %(\n) + expected << %(\n\n\n\n\n\n\n\n\n\n\n\n) + expected << "\n" + + assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_numbers => true, :leading_zeros_month_and_day => true) + assert_dom_equal expected, select_month(8, :use_month_numbers => true, :leading_zeros_month_and_day => true) + end + def test_select_month_with_numbers_and_names expected = %(\n) + expected << %(\n\n\n\n\n\n\n\n\n\n\n\n) + expected << "\n" + + assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true, :leading_zeros_month_and_day => true) + assert_dom_equal expected, select_month(8, :add_month_numbers => true, :leading_zeros_month_and_day => true) + end + def test_select_month_with_numbers_and_names_with_abbv expected = %(\n) + expected << %(\n\n\n\n\n\n\n\n\n\n\n\n) + expected << "\n" + + assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true, :use_short_month => true, :leading_zeros_month_and_day => true) + assert_dom_equal expected, select_month(8, :add_month_numbers => true, :use_short_month => true, :leading_zeros_month_and_day => true) + end + def test_select_month_with_abbv expected = %(\n) + expected << %(\n\n\n) + expected << "\n" + + assert_dom_equal expected, select_year(13, :start_year => 13, :end_year => 15, :leading_zeros_month_and_day => true) + end + def test_select_year_with_disabled expected = %(\n} + expected << %{\n\n\n\n\n\n\n\n\n\n\n} + expected << "\n" + + expected << %{\n" + + expected << %{\n" + + assert_dom_equal expected, date_select("post", "written_on", :use_month_numbers => true) + end + + def test_date_select_with_numeric_months_and_leading_zeros_months_and_days + @post = Post.new + @post.written_on = Date.new(2004, 6, 5) + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + assert_dom_equal expected, date_select("post", "written_on", :use_month_numbers => true, :leading_zeros_month_and_day => true) + end + def test_date_select_without_day @post = Post.new @post.written_on = Date.new(2004, 6, 15) @@ -1695,6 +1790,64 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, datetime_select("post", "updated_at") end + def test_datetime_select_with_numeric_months + @post = Post.new + @post.updated_at = Time.local(2004, 6, 5, 16, 35) + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_dom_equal expected, datetime_select("post", "updated_at", :use_month_numbers => true) + end + + def test_datetime_select_with_numeric_months_and_leading_zeros_months_and_days + @post = Post.new + @post.updated_at = Time.local(2004, 6, 5, 16, 35) + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_dom_equal expected, datetime_select("post", "updated_at", :use_month_numbers => true, :leading_zeros_month_and_day => true) + end + uses_mocha 'TestDatetimeSelectDefaultsToTimeZoneNowWhenConfigTimeZoneIsSet' do def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) -- 1.6.0.2.1172.ga5ed0