From f3f9abe9bce2896291a2db7ee31478198a270fb1 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Thu, 17 Jul 2008 01:25:55 +0200 Subject: [PATCH] Fixed DateHelper#date_or_time_select and DateHelper#default_time_from_options to prevent accidental changes of options hash --- actionpack/lib/action_view/helpers/date_helper.rb | 17 +++++++------- actionpack/test/template/date_helper_test.rb | 24 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index d018034..48c62af 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -631,7 +631,7 @@ module ActionView position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 } - order = (options[:order] ||= [:year, :month, :day]) + order = (options[:order] ||= [:year, :month, :day]).dup # Discard explicit and implicit by not being included in the :order discard = {} @@ -684,21 +684,22 @@ module ActionView options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}i)]") end - def default_time_from_options(default) - case default + def default_time_from_options(options) + case options when nil Time.current when Date, Time - default + options else + default = {} # Rename :minute and :second to :min and :sec - default[:min] ||= default[:minute] - default[:sec] ||= default[:second] - + default[:sec] = options[:sec] || options[:second] || nil + default[:min] = options[:min] || options[:minute] || nil + time = Time.current [:year, :month, :day, :hour, :min, :sec].each do |key| - default[key] ||= time.send(key) + default[key] = default[key] || options[key] || time.send(key) end Time.utc_time(default[:year], default[:month], default[:day], default[:hour], default[:min], default[:sec]) diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 8b4e94c..86f352b 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1428,6 +1428,30 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, select_date(nil, :prefix => "date[first]") end + + def test_date_select_should_not_change_passed_options_hash + @post = Post.new + @post.updated_at = Time.local(2008, 7, 16, 23, 30) + + options = { + :order => [ :year, :month, :day ], + :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, + :discard_type => false, + :include_blank => false, + :ignore_date => false, + :include_seconds => true + } + date_select(@post, :updated_at, options) + + assert_equal({ + :order => [ :year, :month, :day ], + :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, + :discard_type => false, + :include_blank => false, + :ignore_date => false, + :include_seconds => true + }, options) + end def test_datetime_select_with_nil_value_and_no_start_and_end_year expected = %(