From 31dad37c22f23b30e00ff0fda6b71093d7178ea6 Mon Sep 17 00:00:00 2001 From: miloops Date: Thu, 17 Jul 2008 14:01:46 -0300 Subject: [PATCH] Ensure all documented options work in date helpers. --- actionpack/lib/action_view/helpers/date_helper.rb | 23 ++-- actionpack/test/template/date_helper_test.rb | 132 +++++++++++++++++++++ 2 files changed, 145 insertions(+), 10 deletions(-) diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 0735ed0..dbecdf3 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -223,8 +223,8 @@ module ActionView # Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the +datetime+. # It's also possible to explicitly set the order of the tags using the :order option with an array of # symbols :year, :month and :day in the desired order. If you do not supply a Symbol, it - # will be appended onto the :order passed in. You can also add :date_separator and :time_separator - # keys to the +options+ to control visual display of the elements. + # will be appended onto the :order passed in. You can also add :date_separator, + # :datetime_separator and :time_separator keys to the +options+ to control visual display of the elements. # # If anything is passed in the html_options hash it will be applied to every select tag in the set. # @@ -245,6 +245,10 @@ module ActionView # # with a '/' between each date field. # select_datetime(my_date_time, :date_separator => '/') # + # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today) + # # with a '-' between the date fields and the time fields. + # select_datetime(my_date_time, :datetime_separator => '-') + # # # Generates a datetime select that discards the type of the field and defaults to the datetime in # # my_date_time (four days after today) # select_datetime(my_date_time, :discard_type => true) @@ -290,11 +294,9 @@ module ActionView options[:order] ||= [] [:year, :month, :day].each { |o| options[:order].push(o) unless options[:order].include?(o) } - select_date = '' - options[:order].each do |o| - select_date << self.send("select_#{o}", date, options, html_options) - end - select_date + options[:order].inject([]) do |select_date, o| + select_date << self.send("select_#{o}", date, options, html_options) + end.join(options[:date_separator]) end # Returns a set of html select-tags (one for hour and minute) @@ -663,9 +665,10 @@ module ActionView date_or_time_select.insert(0, self.send("select_#{param}", datetime, options_with_prefix(position[param], options.merge(:use_hidden => discard[param])), html_options)) date_or_time_select.insert(0, case param - when :hour then (discard[:year] && discard[:day] ? "" : " — ") - when :minute then " : " - when :second then options[:include_seconds] ? " : " : "" + when :hour then (discard[:year] && discard[:day] ? "" : (options[:datetime_separator] || " — ")) + when :minute then options[:time_separator] || " : " + when :second then options[:include_seconds] ? (options[:time_separator] || " : ") : "" + when :month, :day then options[:date_separator] || "" else "" end) diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 8b4e94c..db29c8f 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -934,6 +934,138 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), {:include_seconds => false}, :class => 'selector') end + def test_select_date_with_date_separator + expected = %(\n" + + expected << " / " + + expected << %(\n" + + expected << " / " + + expected << %(\n" + + assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { :date_separator => ' / ', :start_year => 2003, :end_year => 2005, :prefix => "date[first]"}) + end + + def test_select_datetime_with_all_separators + expected = %(\n" + + expected << " / " + + expected << %(\n" + + expected << " / " + + expected << %(\n" + + expected << " , " + + expected << %(\n" + + expected << " - " + + expected << %(\n" + + assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :date_separator => ' / ', :datetime_separator => ' , ', :time_separator => ' - ') + end + + def test_date_select_with_separator + @post = Post.new + @post.written_on = Date.new(2004, 6, 15) + + expected = %{\n" + + expected << " / " + + expected << %{\n" + + expected << " / " + + expected << %{\n" + + assert_dom_equal expected, date_select("post", "written_on", { :date_separator => " / " }) + end + + def test_time_select_with_separator + @post = Post.new + @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) + + expected = %{\n} + expected << %{\n} + expected << %{\n} + + expected << %(\n" + + expected << " - " + + expected << %(\n" + + assert_dom_equal expected, time_select("post", "written_on", { :time_separator => " - "}) + end + + def test_datetime_select_with_separators + @post = Post.new + @post.updated_at = Time.local(2004, 6, 15, 16, 35) + + expected = %{\n" + + expected << " / " + + expected << %{\n" + + expected << " / " + + expected << %{\n" + + expected << " , " + + expected << %{\n" + + expected << " - " + + expected << %{\n" + + assert_dom_equal expected, datetime_select("post", "updated_at", { :date_separator => " / ", :datetime_separator => " , ", :time_separator => " - "}) + end + uses_mocha 'TestDatetimeAndTimeSelectUseTimeCurrentAsDefault' do def test_select_datetime_uses_time_current_as_default time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) -- 1.5.5.1