Use output_buffer reader and writer methods exclusively instead of hitting the instance variable so others can override the methods.

This commit is contained in:
Jeremy Kemper 2008-06-08 20:05:39 -07:00
parent d9fb021845
commit ff5f155f8d
15 changed files with 159 additions and 242 deletions

View File

@ -31,7 +31,7 @@ module ActionView
# </body></html>
#
def capture(*args, &block)
if @output_buffer
if output_buffer
with_output_buffer { block.call(*args) }
else
block.call(*args)
@ -121,10 +121,10 @@ module ActionView
private
def with_output_buffer(buf = '')
@output_buffer, old_buffer = buf, @output_buffer
self.output_buffer, old_buffer = buf, output_buffer
yield
ensure
@output_buffer = old_buffer
self.output_buffer = old_buffer
end
end
end

View File

@ -202,11 +202,6 @@ module ActionView
end
js_option
end
private
def block_is_within_action_view?(block)
!@output_buffer.nil?
end
end
JavascriptHelper = JavaScriptHelper unless const_defined? :JavascriptHelper

View File

@ -122,10 +122,6 @@ module ActionView
" #{attrs.sort * ' '}" unless attrs.empty?
end
end
def block_is_within_action_view?(block)
!@output_buffer.nil?
end
end
end
end

View File

@ -26,8 +26,8 @@ module ActionView
# # will either display "Logged in!" or a login link
# %>
def concat(string)
if @output_buffer && string
@output_buffer << string
if output_buffer && string
output_buffer << string
else
string
end

View File

@ -106,7 +106,7 @@ module ActionView
locals_code << "#{key} = local_assigns[:#{key}]\n"
end
"def #{render_symbol}(local_assigns)\nold_output_buffer = @output_buffer;#{locals_code}#{body}\nensure\n@output_buffer = old_output_buffer\nend"
"def #{render_symbol}(local_assigns)\nold_output_buffer = output_buffer;#{locals_code}#{body}\nensure\nself.output_buffer = old_output_buffer\nend"
end
# Return true if the given template was compiled for a superset of the keys in local_assigns

View File

@ -37,6 +37,8 @@ module ActionView
if helper_class && !self.class.ancestors.include?(helper_class)
self.class.send(:include, helper_class)
end
self.output_buffer = ''
end
class TestController < ActionController::Base
@ -48,6 +50,9 @@ module ActionView
end
end
protected
attr_accessor :output_buffer
private
def method_missing(selector, *args)
controller = TestController.new

View File

@ -1002,17 +1002,15 @@ class DateHelperTest < ActionView::TestCase
@post = Post.new
@post.written_on = Date.new(2004, 6, 15)
@output_buffer = ''
fields_for :post, @post do |f|
@output_buffer.concat f.date_select(:written_on)
concat f.date_select(:written_on)
end
expected = "<select id='post_written_on_1i' name='post[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_written_on_2i' name='post[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_written_on_3i' name='post[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)
assert_dom_equal(expected, output_buffer)
end
def test_date_select_with_index
@ -1287,10 +1285,8 @@ class DateHelperTest < ActionView::TestCase
@post = Post.new
@post.updated_at = Time.local(2004, 6, 15, 16, 35)
@output_buffer = ''
fields_for :post, @post do |f|
@output_buffer.concat f.datetime_select(:updated_at)
concat f.datetime_select(:updated_at)
end
expected = "<select id='post_updated_at_1i' name='post[updated_at(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"
@ -1299,7 +1295,7 @@ class DateHelperTest < ActionView::TestCase
expected << " &mdash; <select id='post_updated_at_4i' name='post[updated_at(4i)]'>\n<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 selected='selected' 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</select>\n"
expected << " : <select id='post_updated_at_5i' name='post[updated_at(5i)]'>\n<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 selected='selected' value='35'>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</select>\n"
assert_dom_equal(expected, @output_buffer)
assert_dom_equal(expected, output_buffer)
end
def test_date_select_with_zero_value_and_no_start_year

View File

@ -337,14 +337,12 @@ class FormHelperTest < ActionView::TestCase
end
def test_form_for
@output_buffer = ''
form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
@output_buffer.concat f.label(:title)
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
@output_buffer.concat f.submit('Create post')
concat f.label(:title)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
concat f.submit('Create post')
end
expected =
@ -357,16 +355,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='commit' id='post_submit' type='submit' value='Create post' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_method
@output_buffer = ''
form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -378,16 +374,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' type='hidden' value='0' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_without_object
@output_buffer = ''
form_for(:post, :html => { :id => 'create-post' }) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -398,17 +392,15 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' type='hidden' value='0' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_index
@output_buffer = ''
form_for("post[]", @post) do |f|
@output_buffer.concat f.label(:title)
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.label(:title)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -420,16 +412,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[123][secret]' type='hidden' value='0' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_nil_index_option_override
@output_buffer = ''
form_for("post[]", @post, :index => nil) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -440,14 +430,13 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[][secret]' type='hidden' value='0' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_nested_fields_for
@output_buffer = ''
form_for(:post, @post) do |f|
f.fields_for(:comment, @post) do |c|
@output_buffer.concat c.text_field(:title)
concat c.text_field(:title)
end
end
@ -455,16 +444,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for
@output_buffer = ''
fields_for(:post, @post) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -473,16 +460,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for_with_index
@output_buffer = ''
fields_for("post[]", @post) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -491,16 +476,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
"<input name='post[123][secret]' type='hidden' value='0' />"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for_with_nil_index_option_override
@output_buffer = ''
fields_for("post[]", @post, :index => nil) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -509,16 +492,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
"<input name='post[][secret]' type='hidden' value='0' />"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for_with_index_option_override
@output_buffer = ''
fields_for("post[]", @post, :index => "abc") do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -527,15 +508,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />" +
"<input name='post[abc][secret]' type='hidden' value='0' />"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for_without_object
@output_buffer = ''
fields_for(:post) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -544,15 +524,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for_with_only_object
@output_buffer = ''
fields_for(@post) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -561,31 +540,29 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for_object_with_bracketed_name
@output_buffer = ''
fields_for("author[post]", @post) do |f|
@output_buffer.concat f.label(:title)
@output_buffer.concat f.text_field(:title)
concat f.label(:title)
concat f.text_field(:title)
end
assert_dom_equal "<label for=\"author_post_title\">Title</label>" +
"<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
@output_buffer
output_buffer
end
def test_fields_for_object_with_bracketed_name_and_index
@output_buffer = ''
fields_for("author[post]", @post, :index => 1) do |f|
@output_buffer.concat f.label(:title)
@output_buffer.concat f.text_field(:title)
concat f.label(:title)
concat f.text_field(:title)
end
assert_dom_equal "<label for=\"author_post_1_title\">Title</label>" +
"<input name='author[post][1][title]' size='30' type='text' id='author_post_1_title' value='Hello World' />",
@output_buffer
output_buffer
end
def test_form_builder_does_not_have_form_for_method
@ -593,14 +570,12 @@ class FormHelperTest < ActionView::TestCase
end
def test_form_for_and_fields_for
@output_buffer = ''
form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
@output_buffer.concat post_form.text_field(:title)
@output_buffer.concat post_form.text_area(:body)
concat post_form.text_field(:title)
concat post_form.text_area(:body)
fields_for(:parent_post, @post) do |parent_fields|
@output_buffer.concat parent_fields.check_box(:secret)
concat parent_fields.check_box(:secret)
end
end
@ -612,18 +587,16 @@ class FormHelperTest < ActionView::TestCase
"<input name='parent_post[secret]' type='hidden' value='0' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_and_fields_for_with_object
@output_buffer = ''
form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
@output_buffer.concat post_form.text_field(:title)
@output_buffer.concat post_form.text_area(:body)
concat post_form.text_field(:title)
concat post_form.text_area(:body)
post_form.fields_for(@comment) do |comment_fields|
@output_buffer.concat comment_fields.text_field(:name)
concat comment_fields.text_field(:name)
end
end
@ -634,7 +607,7 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[comment][name]' type='text' id='post_comment_name' value='new comment' size='30' />" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
class LabelledFormBuilder < ActionView::Helpers::FormBuilder
@ -649,12 +622,10 @@ class FormHelperTest < ActionView::TestCase
end
def test_form_for_with_labelled_builder
@output_buffer = ''
form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -665,18 +636,17 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' type='hidden' value='0' /><br/>" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_default_form_builder
old_default_form_builder, ActionView::Base.default_form_builder =
ActionView::Base.default_form_builder, LabelledFormBuilder
@output_buffer = ''
form_for(:post, @post) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -687,17 +657,15 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' type='hidden' value='0' /><br/>" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
ensure
ActionView::Base.default_form_builder = old_default_form_builder
end
def test_default_form_builder_with_active_record_helpers
@output_buffer = ''
form_for(:post, @post) do |f|
@output_buffer.concat f.error_message_on('author_name')
@output_buffer.concat f.error_messages
concat f.error_message_on('author_name')
concat f.error_messages
end
expected = %(<form action='http://www.example.com' method='post'>) +
@ -705,7 +673,7 @@ class FormHelperTest < ActionView::TestCase
%(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
%(</form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
@ -713,10 +681,9 @@ class FormHelperTest < ActionView::TestCase
post = @post
@post = nil
@output_buffer = ''
form_for(:post, post) do |f|
@output_buffer.concat f.error_message_on('author_name')
@output_buffer.concat f.error_messages
concat f.error_message_on('author_name')
concat f.error_messages
end
expected = %(<form action='http://www.example.com' method='post'>) +
@ -724,19 +691,18 @@ class FormHelperTest < ActionView::TestCase
%(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
%(</form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
# Perhaps this test should be moved to prototype helper tests.
def test_remote_form_for_with_labelled_builder
self.extend ActionView::Helpers::PrototypeHelper
@output_buffer = ''
remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -747,16 +713,14 @@ class FormHelperTest < ActionView::TestCase
"<input name='post[secret]' type='hidden' value='0' /><br/>" +
"</form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_fields_for_with_labelled_builder
@output_buffer = ''
fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
@output_buffer.concat f.text_field(:title)
@output_buffer.concat f.text_area(:body)
@output_buffer.concat f.check_box(:secret)
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected =
@ -765,29 +729,23 @@ class FormHelperTest < ActionView::TestCase
"<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' /><br/>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_html_options_adds_options_to_form_tag
@output_buffer = ''
form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_string_url_option
@output_buffer = ''
form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end
assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', @output_buffer
assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', output_buffer
end
def test_form_for_with_hash_url_option
@output_buffer = ''
form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end
assert_equal 'controller', @controller.url_for_options[:controller]
@ -795,26 +753,20 @@ class FormHelperTest < ActionView::TestCase
end
def test_form_for_with_record_url_option
@output_buffer = ''
form_for(:post, @post, :url => @post) do |f| end
expected = "<form action=\"/posts/123\" method=\"post\"></form>"
assert_equal expected, @output_buffer
assert_equal expected, output_buffer
end
def test_form_for_with_existing_object
@output_buffer = ''
form_for(@post) do |f| end
expected = "<form action=\"/posts/123\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>"
assert_equal expected, @output_buffer
assert_equal expected, output_buffer
end
def test_form_for_with_new_object
@output_buffer = ''
post = Post.new
post.new_record = true
def post.id() nil end
@ -822,64 +774,61 @@ class FormHelperTest < ActionView::TestCase
form_for(post) do |f| end
expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>"
assert_equal expected, @output_buffer
assert_equal expected, output_buffer
end
def test_form_for_with_existing_object_in_list
@post.new_record = false
@comment.save
@output_buffer = ''
form_for([@post, @comment]) {}
expected = %(<form action="#{comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_new_object_in_list
@post.new_record = false
@output_buffer = ''
form_for([@post, @comment]) {}
expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_existing_object_and_namespace_in_list
@post.new_record = false
@comment.save
@output_buffer = ''
form_for([:admin, @post, @comment]) {}
expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_new_object_and_namespace_in_list
@post.new_record = false
@output_buffer = ''
form_for([:admin, @post, @comment]) {}
expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_for_with_existing_object_and_custom_url
@output_buffer = ''
form_for(@post, :url => "/super_posts") do |f| end
expected = "<form action=\"/super_posts\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>"
assert_equal expected, @output_buffer
assert_equal expected, output_buffer
end
def test_remote_form_for_with_html_options_adds_options_to_form_tag
self.extend ActionView::Helpers::PrototypeHelper
@output_buffer = ''
remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\" onsubmit=\"new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"></form>"
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end

View File

@ -230,16 +230,14 @@ class FormOptionsHelperTest < ActionView::TestCase
def test_select_under_fields_for
@post = Post.new
@post.category = "<mus>"
@output_buffer = ''
fields_for :post, @post do |f|
@output_buffer.concat f.select(:category, %w( abe <mus> hest))
concat f.select(:category, %w( abe <mus> hest))
end
assert_dom_equal(
"<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
@output_buffer
output_buffer
)
end
@ -352,16 +350,14 @@ class FormOptionsHelperTest < ActionView::TestCase
@post = Post.new
@post.author_name = "Babe"
@output_buffer = ''
fields_for :post, @post do |f|
@output_buffer.concat f.collection_select(:author_name, @posts, :author_name, :author_name)
concat f.collection_select(:author_name, @posts, :author_name, :author_name)
end
assert_dom_equal(
"<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
@output_buffer
output_buffer
)
end
@ -1194,11 +1190,9 @@ COUNTRIES
def test_time_zone_select_under_fields_for
@firm = Firm.new("D")
@output_buffer = ''
fields_for :firm, @firm do |f|
@output_buffer.concat f.time_zone_select(:time_zone)
concat f.time_zone_select(:time_zone)
end
assert_dom_equal(
@ -1209,7 +1203,7 @@ COUNTRIES
"<option value=\"D\" selected=\"selected\">D</option>\n" +
"<option value=\"E\">E</option>" +
"</select>",
@output_buffer
output_buffer
)
end

View File

@ -43,19 +43,17 @@ class FormTagHelperTest < ActionView::TestCase
end
def test_form_tag_with_block
@output_buffer = ''
form_tag("http://example.com") { @output_buffer.concat "Hello world!" }
form_tag("http://example.com") { concat "Hello world!" }
expected = %(<form action="http://example.com" method="post">Hello world!</form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_form_tag_with_block_and_method
@output_buffer = ''
form_tag("http://example.com", :method => :put) { @output_buffer.concat "Hello world!" }
form_tag("http://example.com", :method => :put) { concat "Hello world!" }
expected = %(<form action="http://example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>Hello world!</form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_hidden_field_tag
@ -234,23 +232,22 @@ class FormTagHelperTest < ActionView::TestCase
end
def test_field_set_tag
@output_buffer = ''
field_set_tag("Your details") { @output_buffer.concat "Hello world!" }
field_set_tag("Your details") { concat "Hello world!" }
expected = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
@output_buffer = ''
field_set_tag { @output_buffer.concat "Hello world!" }
self.output_buffer = ''
field_set_tag { concat "Hello world!" }
expected = %(<fieldset>Hello world!</fieldset>)
assert_dom_equal expected, @output_buffer
@output_buffer = ''
field_set_tag('') { @output_buffer.concat "Hello world!" }
assert_dom_equal expected, output_buffer
self.output_buffer = ''
field_set_tag('') { concat "Hello world!" }
expected = %(<fieldset>Hello world!</fieldset>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def protect_against_forgery?

View File

@ -82,12 +82,12 @@ class JavaScriptHelperTest < ActionView::TestCase
end
def test_javascript_tag
@output_buffer = 'foo'
self.output_buffer = 'foo'
assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
javascript_tag("alert('hello')")
assert_equal 'foo', @output_buffer, 'javascript_tag without a block should not concat to @output_buffer'
assert_equal 'foo', output_buffer, 'javascript_tag without a block should not concat to output_buffer'
end
def test_javascript_tag_with_options
@ -96,15 +96,13 @@ class JavaScriptHelperTest < ActionView::TestCase
end
def test_javascript_tag_with_block
@output_buffer = ''
javascript_tag { @output_buffer.concat "alert('hello')" }
assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", @output_buffer
javascript_tag { concat "alert('hello')" }
assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", output_buffer
end
def test_javascript_tag_with_block_and_options
@output_buffer = ''
javascript_tag(:id => "the_js_tag") { @output_buffer.concat "alert('hello')" }
assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", @output_buffer
javascript_tag(:id => "the_js_tag") { concat "alert('hello')" }
assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", output_buffer
end
def test_javascript_cdata_section

View File

@ -118,52 +118,46 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
end
def test_form_remote_tag_with_block
@output_buffer = ''
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { @output_buffer.concat "Hello world!" }
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">Hello world!</form>), @output_buffer
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { concat "Hello world!" }
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">Hello world!</form>), output_buffer
end
def test_remote_form_for_with_record_identification_with_new_record
@output_buffer = ''
remote_form_for(@record, {:html => { :id => 'create-author' }}) {}
expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' id='create-author' method='post'></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_remote_form_for_with_record_identification_without_html_options
@output_buffer = ''
remote_form_for(@record) {}
expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' method='post' id='new_author'></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_remote_form_for_with_record_identification_with_existing_record
@record.save
@output_buffer = ''
remote_form_for(@record) {}
expected = %(<form action='#{author_path(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_path(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_remote_form_for_with_new_object_in_list
@output_buffer = ''
remote_form_for([@author, @article]) {}
expected = %(<form action='#{author_articles_path(@author)}' onsubmit="new Ajax.Request('#{author_articles_path(@author)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_article' method='post' id='new_article'></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_remote_form_for_with_existing_object_in_list
@author.save
@article.save
@output_buffer = ''
remote_form_for([@author, @article]) {}
expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_1' method='post' onsubmit="new Ajax.Request('#{author_article_path(@author, @article)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_article'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>)
assert_dom_equal expected, @output_buffer
assert_dom_equal expected, output_buffer
end
def test_on_callbacks

View File

@ -17,37 +17,32 @@ class RecordTagHelperTest < ActionView::TestCase
end
def test_content_tag_for
@output_buffer = ''
expected = %(<li class="post bar" id="post_45"></li>)
actual = content_tag_for(:li, @post, :class => 'bar') { }
assert_dom_equal expected, actual
end
def test_content_tag_for_prefix
@output_buffer = ''
expected = %(<ul class="post" id="archived_post_45"></ul>)
actual = content_tag_for(:ul, @post, :archived) { }
assert_dom_equal expected, actual
end
def test_content_tag_for_with_extra_html_tags
@output_buffer = ''
expected = %(<tr class="post bar" id="post_45" style='background-color: #f0f0f0'></tr>)
actual = content_tag_for(:tr, @post, {:class => "bar", :style => "background-color: #f0f0f0"}) { }
assert_dom_equal expected, actual
end
def test_block_works_with_content_tag_for
@output_buffer = ''
expected = %(<tr class="post" id="post_45">#{@post.body}</tr>)
actual = content_tag_for(:tr, @post) { @output_buffer.concat @post.body }
actual = content_tag_for(:tr, @post) { concat @post.body }
assert_dom_equal expected, actual
end
def test_div_for
@output_buffer = ''
expected = %(<div class="post bar" id="post_45">#{@post.body}</div>)
actual = div_for(@post, :class => "bar") { @output_buffer.concat @post.body }
actual = div_for(@post, :class => "bar") { concat @post.body }
assert_dom_equal expected, actual
end

View File

@ -35,15 +35,13 @@ class TagHelperTest < ActionView::TestCase
end
def test_content_tag_with_block
@output_buffer = ''
content_tag(:div) { @output_buffer.concat "Hello world!" }
assert_dom_equal "<div>Hello world!</div>", @output_buffer
content_tag(:div) { concat "Hello world!" }
assert_dom_equal "<div>Hello world!</div>", output_buffer
end
def test_content_tag_with_block_and_options
@output_buffer = ''
content_tag(:div, :class => "green") { @output_buffer.concat "Hello world!" }
assert_dom_equal %(<div class="green">Hello world!</div>), @output_buffer
content_tag(:div, :class => "green") { concat "Hello world!" }
assert_dom_equal %(<div class="green">Hello world!</div>), output_buffer
end
def test_content_tag_with_block_and_options_outside_of_action_view

View File

@ -12,11 +12,11 @@ class TextHelperTest < ActionView::TestCase
end
def test_concat
@output_buffer = 'foo'
self.output_buffer = 'foo'
concat 'bar'
assert_equal 'foobar', @output_buffer
assert_equal 'foobar', output_buffer
assert_nothing_raised { concat nil }
assert_equal 'foobar', @output_buffer
assert_equal 'foobar', output_buffer
end
def test_simple_format