From 72608521871f73d6b07afa5f6f36e0dedcf1d079 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 4 Jan 2009 14:54:35 +0000 Subject: [PATCH] Fix date_select within fields_for with an index [#1666 state:resolved] [Josh, Frederick Cheung] Signed-off-by: Frederick Cheung --- .../lib/action_view/helpers/date_helper.rb | 10 +- actionpack/test/template/date_helper_test.rb | 95 ++++++++++++++++--- 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 4305617ac8..b4c1adbe76 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -860,7 +860,7 @@ module ActionView # => post[written_on(1i)] def input_name_from_type(type) prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX - prefix += "[#{@options[:index]}]" if @options[:index] + prefix += "[#{@options[:index]}]" if @options.has_key?(:index) field_name = @options[:field_name] || type if @options[:include_position] @@ -923,7 +923,7 @@ module ActionView options[:field_name] = @method_name options[:include_position] = true options[:prefix] ||= @object_name - options[:index] ||= @auto_index + options[:index] = @auto_index if @auto_index && !options.has_key?(:index) options[:datetime_separator] ||= ' — ' options[:time_separator] ||= ' : ' @@ -961,15 +961,15 @@ module ActionView class FormBuilder def date_select(method, options = {}, html_options = {}) - @template.date_select(@object_name, method, options.merge(:object => @object), html_options) + @template.date_select(@object_name, method, objectify_options(options), html_options) end def time_select(method, options = {}, html_options = {}) - @template.time_select(@object_name, method, options.merge(:object => @object), html_options) + @template.time_select(@object_name, method, objectify_options(options), html_options) end def datetime_select(method, options = {}, html_options = {}) - @template.datetime_select(@object_name, method, options.merge(:object => @object), html_options) + @template.datetime_select(@object_name, method, objectify_options(options), html_options) end end end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 6ec01b7a8f..92cdce2e45 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1228,6 +1228,38 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal(expected, output_buffer) end + def test_date_select_within_fields_for_with_index + @post = Post.new + @post.written_on = Date.new(2004, 6, 15) + id = 27 + + fields_for :post, @post, :index => id do |f| + concat f.date_select(:written_on) + end + + expected = "\n" + expected << "\n" + expected << "\n" + + assert_dom_equal(expected, output_buffer) + end + + def test_date_select_within_fields_for_with_blank_index + @post = Post.new + @post.written_on = Date.new(2004, 6, 15) + id = nil + + fields_for :post, @post, :index => id do |f| + concat f.date_select(:written_on) + end + + expected = "\n" + expected << "\n" + expected << "\n" + + assert_dom_equal(expected, output_buffer) + end + def test_date_select_with_index @post = Post.new @post.written_on = Date.new(2004, 6, 15) @@ -1243,7 +1275,6 @@ class DateHelperTest < ActionView::TestCase expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", :index => id) @@ -1330,13 +1361,13 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, date_select("post", "written_on", :include_blank => true) end - + def test_date_select_with_nil_and_blank_and_order @post = Post.new start_year = Time.now.year-5 end_year = Time.now.year+5 - + expected = '' + "\n" expected << %{\n} + expected << %{\n\n\n\n\n\n\n\n\n\n\n} + expected << "\n" + + expected << %{\n" + + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_dom_equal expected, output_buffer + end + def test_datetime_select_with_auto_index @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) @@ -2253,7 +2318,7 @@ class DateHelperTest < ActionView::TestCase @post = Post.new @post.updated_at = Time.local(2008, 7, 16, 23, 30) - options = { + options = { :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2265,7 +2330,7 @@ class DateHelperTest < ActionView::TestCase # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! - assert_equal({ + assert_equal({ :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2279,7 +2344,7 @@ class DateHelperTest < ActionView::TestCase @post = Post.new @post.updated_at = Time.local(2008, 7, 16, 23, 30) - options = { + options = { :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2291,7 +2356,7 @@ class DateHelperTest < ActionView::TestCase # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! - assert_equal({ + assert_equal({ :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2305,7 +2370,7 @@ class DateHelperTest < ActionView::TestCase @post = Post.new @post.updated_at = Time.local(2008, 7, 16, 23, 30) - options = { + options = { :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2317,7 +2382,7 @@ class DateHelperTest < ActionView::TestCase # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! - assert_equal({ + assert_equal({ :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2328,7 +2393,7 @@ class DateHelperTest < ActionView::TestCase end def test_select_date_should_not_change_passed_options_hash - options = { + options = { :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2340,7 +2405,7 @@ class DateHelperTest < ActionView::TestCase # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! - assert_equal({ + assert_equal({ :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2351,7 +2416,7 @@ class DateHelperTest < ActionView::TestCase end def test_select_datetime_should_not_change_passed_options_hash - options = { + options = { :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2363,7 +2428,7 @@ class DateHelperTest < ActionView::TestCase # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! - assert_equal({ + assert_equal({ :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2374,7 +2439,7 @@ class DateHelperTest < ActionView::TestCase end def test_select_time_should_not_change_passed_options_hash - options = { + options = { :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false, @@ -2386,7 +2451,7 @@ class DateHelperTest < ActionView::TestCase # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! - assert_equal({ + assert_equal({ :order => [ :year, :month, :day ], :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 }, :discard_type => false,