mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix date_select within fields_for with an index [#1666 state:resolved] [Josh, Frederick Cheung]
Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
This commit is contained in:
parent
452cd74d81
commit
7260852187
2 changed files with 85 additions and 20 deletions
|
@ -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
|
||||
|
|
|
@ -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 = "<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"
|
||||
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"
|
||||
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"
|
||||
|
||||
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 = "<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"
|
||||
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"
|
||||
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"
|
||||
|
||||
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 << %{<select id="post_456_written_on_3i" name="post[#{id}][written_on(3i)]">\n}
|
||||
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}
|
||||
|
||||
expected << "</select>\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 = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n"
|
||||
expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
|
||||
expected << "<option value=\"\"></option>\n"
|
||||
|
@ -1966,6 +1997,40 @@ class DateHelperTest < ActionView::TestCase
|
|||
assert_dom_equal expected, datetime_select("post", "updated_at", :index => id)
|
||||
end
|
||||
|
||||
def test_datetime_select_within_fields_for_with_options_index
|
||||
@post = Post.new
|
||||
@post.updated_at = Time.local(2004, 6, 15, 16, 35)
|
||||
id = 456
|
||||
|
||||
fields_for :post, @post, :index => id do |f|
|
||||
concat f.datetime_select(:updated_at)
|
||||
end
|
||||
|
||||
expected = %{<select id="post_456_updated_at_1i" name="post[#{id}][updated_at(1i)]">\n}
|
||||
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}
|
||||
expected << "</select>\n"
|
||||
|
||||
expected << %{<select id="post_456_updated_at_2i" name="post[#{id}][updated_at(2i)]">\n}
|
||||
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}
|
||||
expected << "</select>\n"
|
||||
|
||||
expected << %{<select id="post_456_updated_at_3i" name="post[#{id}][updated_at(3i)]">\n}
|
||||
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}
|
||||
expected << "</select>\n"
|
||||
|
||||
expected << " — "
|
||||
|
||||
expected << %{<select id="post_456_updated_at_4i" name="post[#{id}][updated_at(4i)]">\n}
|
||||
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}
|
||||
expected << "</select>\n"
|
||||
expected << " : "
|
||||
expected << %{<select id="post_456_updated_at_5i" name="post[#{id}][updated_at(5i)]">\n}
|
||||
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}
|
||||
expected << "</select>\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,
|
||||
|
|
Loading…
Reference in a new issue