Skip to content
This repository
Browse code

Fix date_select within fields_for with an index [#1666 state:resolved…

…] [Josh, Frederick Cheung]

Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
  • Loading branch information...
commit 72608521871f73d6b07afa5f6f36e0dedcf1d079 1 parent 452cd74
Josh authored lifo committed
10 actionpack/lib/action_view/helpers/date_helper.rb
@@ -860,7 +860,7 @@ def build_hidden(type, value)
860 860 # => post[written_on(1i)]
861 861 def input_name_from_type(type)
862 862 prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX
863   - prefix += "[#{@options[:index]}]" if @options[:index]
  863 + prefix += "[#{@options[:index]}]" if @options.has_key?(:index)
864 864
865 865 field_name = @options[:field_name] || type
866 866 if @options[:include_position]
@@ -923,7 +923,7 @@ def datetime_selector(options, html_options)
923 923 options[:field_name] = @method_name
924 924 options[:include_position] = true
925 925 options[:prefix] ||= @object_name
926   - options[:index] ||= @auto_index
  926 + options[:index] = @auto_index if @auto_index && !options.has_key?(:index)
927 927 options[:datetime_separator] ||= ' &mdash; '
928 928 options[:time_separator] ||= ' : '
929 929
@@ -961,15 +961,15 @@ def default_datetime(options)
961 961
962 962 class FormBuilder
963 963 def date_select(method, options = {}, html_options = {})
964   - @template.date_select(@object_name, method, options.merge(:object => @object), html_options)
  964 + @template.date_select(@object_name, method, objectify_options(options), html_options)
965 965 end
966 966
967 967 def time_select(method, options = {}, html_options = {})
968   - @template.time_select(@object_name, method, options.merge(:object => @object), html_options)
  968 + @template.time_select(@object_name, method, objectify_options(options), html_options)
969 969 end
970 970
971 971 def datetime_select(method, options = {}, html_options = {})
972   - @template.datetime_select(@object_name, method, options.merge(:object => @object), html_options)
  972 + @template.datetime_select(@object_name, method, objectify_options(options), html_options)
973 973 end
974 974 end
975 975 end
95 actionpack/test/template/date_helper_test.rb
@@ -1228,6 +1228,38 @@ def test_date_select_within_fields_for
1228 1228 assert_dom_equal(expected, output_buffer)
1229 1229 end
1230 1230
  1231 + def test_date_select_within_fields_for_with_index
  1232 + @post = Post.new
  1233 + @post.written_on = Date.new(2004, 6, 15)
  1234 + id = 27
  1235 +
  1236 + fields_for :post, @post, :index => id do |f|
  1237 + concat f.date_select(:written_on)
  1238 + end
  1239 +
  1240 + expected = "<select id='post_#{id}_written_on_1i' name='post[#{id}][written_on(1i)]'>\n<option value='1999'>1999</option>\n<option value='2000'>2000</option>\n<option value='2001'>2001</option>\n<option value='2002'>2002</option>\n<option value='2003'>2003</option>\n<option selected='selected' value='2004'>2004</option>\n<option value='2005'>2005</option>\n<option value='2006'>2006</option>\n<option value='2007'>2007</option>\n<option value='2008'>2008</option>\n<option value='2009'>2009</option>\n</select>\n"
  1241 + expected << "<select id='post_#{id}_written_on_2i' name='post[#{id}][written_on(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option selected='selected' value='6'>June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n"
  1242 + expected << "<select id='post_#{id}_written_on_3i' name='post[#{id}][written_on(3i)]'>\n<option value='1'>1</option>\n<option value='2'>2</option>\n<option value='3'>3</option>\n<option value='4'>4</option>\n<option value='5'>5</option>\n<option value='6'>6</option>\n<option value='7'>7</option>\n<option value='8'>8</option>\n<option value='9'>9</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option selected='selected' value='15'>15</option>\n<option value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n<option value='24'>24</option>\n<option value='25'>25</option>\n<option value='26'>26</option>\n<option value='27'>27</option>\n<option value='28'>28</option>\n<option value='29'>29</option>\n<option value='30'>30</option>\n<option value='31'>31</option>\n</select>\n"
  1243 +
  1244 + assert_dom_equal(expected, output_buffer)
  1245 + end
  1246 +
  1247 + def test_date_select_within_fields_for_with_blank_index
  1248 + @post = Post.new
  1249 + @post.written_on = Date.new(2004, 6, 15)
  1250 + id = nil
  1251 +
  1252 + fields_for :post, @post, :index => id do |f|
  1253 + concat f.date_select(:written_on)
  1254 + end
  1255 +
  1256 + expected = "<select id='post_#{id}_written_on_1i' name='post[#{id}][written_on(1i)]'>\n<option value='1999'>1999</option>\n<option value='2000'>2000</option>\n<option value='2001'>2001</option>\n<option value='2002'>2002</option>\n<option value='2003'>2003</option>\n<option selected='selected' value='2004'>2004</option>\n<option value='2005'>2005</option>\n<option value='2006'>2006</option>\n<option value='2007'>2007</option>\n<option value='2008'>2008</option>\n<option value='2009'>2009</option>\n</select>\n"
  1257 + expected << "<select id='post_#{id}_written_on_2i' name='post[#{id}][written_on(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option selected='selected' value='6'>June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n"
  1258 + expected << "<select id='post_#{id}_written_on_3i' name='post[#{id}][written_on(3i)]'>\n<option value='1'>1</option>\n<option value='2'>2</option>\n<option value='3'>3</option>\n<option value='4'>4</option>\n<option value='5'>5</option>\n<option value='6'>6</option>\n<option value='7'>7</option>\n<option value='8'>8</option>\n<option value='9'>9</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option selected='selected' value='15'>15</option>\n<option value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n<option value='24'>24</option>\n<option value='25'>25</option>\n<option value='26'>26</option>\n<option value='27'>27</option>\n<option value='28'>28</option>\n<option value='29'>29</option>\n<option value='30'>30</option>\n<option value='31'>31</option>\n</select>\n"
  1259 +
  1260 + assert_dom_equal(expected, output_buffer)
  1261 + end
  1262 +
1231 1263 def test_date_select_with_index
1232 1264 @post = Post.new
1233 1265 @post.written_on = Date.new(2004, 6, 15)
@@ -1243,7 +1275,6 @@ def test_date_select_with_index
1243 1275
1244 1276 expected << %{<select id="post_456_written_on_3i" name="post[#{id}][written_on(3i)]">\n}
1245 1277 expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
1246   -
1247 1278 expected << "</select>\n"
1248 1279
1249 1280 assert_dom_equal expected, date_select("post", "written_on", :index => id)
@@ -1330,13 +1361,13 @@ def test_date_select_with_nil_and_blank
1330 1361
1331 1362 assert_dom_equal expected, date_select("post", "written_on", :include_blank => true)
1332 1363 end
1333   -
  1364 +
1334 1365 def test_date_select_with_nil_and_blank_and_order
1335 1366 @post = Post.new
1336 1367
1337 1368 start_year = Time.now.year-5
1338 1369 end_year = Time.now.year+5
1339   -
  1370 +
1340 1371 expected = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n"
1341 1372 expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
1342 1373 expected << "<option value=\"\"></option>\n"
@@ -1966,6 +1997,40 @@ def test_datetime_select_with_options_index
1966 1997 assert_dom_equal expected, datetime_select("post", "updated_at", :index => id)
1967 1998 end
1968 1999
  2000 + def test_datetime_select_within_fields_for_with_options_index
  2001 + @post = Post.new
  2002 + @post.updated_at = Time.local(2004, 6, 15, 16, 35)
  2003 + id = 456
  2004 +
  2005 + fields_for :post, @post, :index => id do |f|
  2006 + concat f.datetime_select(:updated_at)
  2007 + end
  2008 +
  2009 + expected = %{<select id="post_456_updated_at_1i" name="post[#{id}][updated_at(1i)]">\n}
  2010 + expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
  2011 + expected << "</select>\n"
  2012 +
  2013 + expected << %{<select id="post_456_updated_at_2i" name="post[#{id}][updated_at(2i)]">\n}
  2014 + expected << %{<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
  2015 + expected << "</select>\n"
  2016 +
  2017 + expected << %{<select id="post_456_updated_at_3i" name="post[#{id}][updated_at(3i)]">\n}
  2018 + expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
  2019 + expected << "</select>\n"
  2020 +
  2021 + expected << " &mdash; "
  2022 +
  2023 + expected << %{<select id="post_456_updated_at_4i" name="post[#{id}][updated_at(4i)]">\n}
  2024 + expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
  2025 + expected << "</select>\n"
  2026 + expected << " : "
  2027 + expected << %{<select id="post_456_updated_at_5i" name="post[#{id}][updated_at(5i)]">\n}
  2028 + expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35" selected="selected">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
  2029 + expected << "</select>\n"
  2030 +
  2031 + assert_dom_equal expected, output_buffer
  2032 + end
  2033 +
1969 2034 def test_datetime_select_with_auto_index
1970 2035 @post = Post.new
1971 2036 @post.updated_at = Time.local(2004, 6, 15, 16, 35)
@@ -2253,7 +2318,7 @@ def test_date_select_should_not_change_passed_options_hash
2253 2318 @post = Post.new
2254 2319 @post.updated_at = Time.local(2008, 7, 16, 23, 30)
2255 2320
2256   - options = {
  2321 + options = {
2257 2322 :order => [ :year, :month, :day ],
2258 2323 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2259 2324 :discard_type => false,
@@ -2265,7 +2330,7 @@ def test_date_select_should_not_change_passed_options_hash
2265 2330
2266 2331 # note: the literal hash is intentional to show that the actual options hash isn't modified
2267 2332 # don't change this!
2268   - assert_equal({
  2333 + assert_equal({
2269 2334 :order => [ :year, :month, :day ],
2270 2335 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2271 2336 :discard_type => false,
@@ -2279,7 +2344,7 @@ def test_datetime_select_should_not_change_passed_options_hash
2279 2344 @post = Post.new
2280 2345 @post.updated_at = Time.local(2008, 7, 16, 23, 30)
2281 2346
2282   - options = {
  2347 + options = {
2283 2348 :order => [ :year, :month, :day ],
2284 2349 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2285 2350 :discard_type => false,
@@ -2291,7 +2356,7 @@ def test_datetime_select_should_not_change_passed_options_hash
2291 2356
2292 2357 # note: the literal hash is intentional to show that the actual options hash isn't modified
2293 2358 # don't change this!
2294   - assert_equal({
  2359 + assert_equal({
2295 2360 :order => [ :year, :month, :day ],
2296 2361 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2297 2362 :discard_type => false,
@@ -2305,7 +2370,7 @@ def test_time_select_should_not_change_passed_options_hash
2305 2370 @post = Post.new
2306 2371 @post.updated_at = Time.local(2008, 7, 16, 23, 30)
2307 2372
2308   - options = {
  2373 + options = {
2309 2374 :order => [ :year, :month, :day ],
2310 2375 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2311 2376 :discard_type => false,
@@ -2317,7 +2382,7 @@ def test_time_select_should_not_change_passed_options_hash
2317 2382
2318 2383 # note: the literal hash is intentional to show that the actual options hash isn't modified
2319 2384 # don't change this!
2320   - assert_equal({
  2385 + assert_equal({
2321 2386 :order => [ :year, :month, :day ],
2322 2387 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2323 2388 :discard_type => false,
@@ -2328,7 +2393,7 @@ def test_time_select_should_not_change_passed_options_hash
2328 2393 end
2329 2394
2330 2395 def test_select_date_should_not_change_passed_options_hash
2331   - options = {
  2396 + options = {
2332 2397 :order => [ :year, :month, :day ],
2333 2398 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2334 2399 :discard_type => false,
@@ -2340,7 +2405,7 @@ def test_select_date_should_not_change_passed_options_hash
2340 2405
2341 2406 # note: the literal hash is intentional to show that the actual options hash isn't modified
2342 2407 # don't change this!
2343   - assert_equal({
  2408 + assert_equal({
2344 2409 :order => [ :year, :month, :day ],
2345 2410 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2346 2411 :discard_type => false,
@@ -2351,7 +2416,7 @@ def test_select_date_should_not_change_passed_options_hash
2351 2416 end
2352 2417
2353 2418 def test_select_datetime_should_not_change_passed_options_hash
2354   - options = {
  2419 + options = {
2355 2420 :order => [ :year, :month, :day ],
2356 2421 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2357 2422 :discard_type => false,
@@ -2363,7 +2428,7 @@ def test_select_datetime_should_not_change_passed_options_hash
2363 2428
2364 2429 # note: the literal hash is intentional to show that the actual options hash isn't modified
2365 2430 # don't change this!
2366   - assert_equal({
  2431 + assert_equal({
2367 2432 :order => [ :year, :month, :day ],
2368 2433 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2369 2434 :discard_type => false,
@@ -2374,7 +2439,7 @@ def test_select_datetime_should_not_change_passed_options_hash
2374 2439 end
2375 2440
2376 2441 def test_select_time_should_not_change_passed_options_hash
2377   - options = {
  2442 + options = {
2378 2443 :order => [ :year, :month, :day ],
2379 2444 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2380 2445 :discard_type => false,
@@ -2386,7 +2451,7 @@ def test_select_time_should_not_change_passed_options_hash
2386 2451
2387 2452 # note: the literal hash is intentional to show that the actual options hash isn't modified
2388 2453 # don't change this!
2389   - assert_equal({
  2454 + assert_equal({
2390 2455 :order => [ :year, :month, :day ],
2391 2456 :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
2392 2457 :discard_type => false,

0 comments on commit 7260852

Please sign in to comment.
Something went wrong with that request. Please try again.