mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Check whether blocks are called from erb using a special __in_erb_template variable visible in block binding.
This commit is contained in:
@@ -31,10 +31,13 @@ module ActionView
|
||||
# </body></html>
|
||||
#
|
||||
def capture(*args, &block)
|
||||
if output_buffer
|
||||
# Return captured buffer in erb.
|
||||
if block_called_from_erb?(block)
|
||||
with_output_buffer { block.call(*args) }
|
||||
|
||||
# Return block result otherwise, but protect buffer also.
|
||||
else
|
||||
block.call(*args)
|
||||
with_output_buffer { return block.call(*args) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -117,6 +120,7 @@ module ActionView
|
||||
ivar = "@content_for_#{name}"
|
||||
content = capture(&block) if block_given?
|
||||
instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{content}")
|
||||
nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -6,7 +6,7 @@ module ActionView
|
||||
# Provides a number of methods for creating form tags that doesn't rely on an Active Record object assigned to the template like
|
||||
# FormHelper does. Instead, you provide the names and values manually.
|
||||
#
|
||||
# NOTE: The HTML options <tt>disabled</tt>, <tt>readonly</tt>, and <tt>multiple</tt> can all be treated as booleans. So specifying
|
||||
# NOTE: The HTML options <tt>disabled</tt>, <tt>readonly</tt>, and <tt>multiple</tt> can all be treated as booleans. So specifying
|
||||
# <tt>:disabled => true</tt> will give <tt>disabled="disabled"</tt>.
|
||||
module FormTagHelper
|
||||
# Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like
|
||||
@@ -20,15 +20,15 @@ module ActionView
|
||||
# * A list of parameters to feed to the URL the form will be posted to.
|
||||
#
|
||||
# ==== Examples
|
||||
# form_tag('/posts')
|
||||
# form_tag('/posts')
|
||||
# # => <form action="/posts" method="post">
|
||||
#
|
||||
# form_tag('/posts/1', :method => :put)
|
||||
# form_tag('/posts/1', :method => :put)
|
||||
# # => <form action="/posts/1" method="put">
|
||||
#
|
||||
# form_tag('/upload', :multipart => true)
|
||||
# form_tag('/upload', :multipart => true)
|
||||
# # => <form action="/upload" method="post" enctype="multipart/form-data">
|
||||
#
|
||||
#
|
||||
# <% form_tag '/posts' do -%>
|
||||
# <div><%= submit_tag 'Save' %></div>
|
||||
# <% end -%>
|
||||
@@ -88,7 +88,7 @@ module ActionView
|
||||
# * <tt>:size</tt> - The number of visible characters that will fit in the input.
|
||||
# * <tt>:maxlength</tt> - The maximum number of characters that the browser will allow the user to enter.
|
||||
# * Any other key creates standard HTML attributes for the tag.
|
||||
#
|
||||
#
|
||||
# ==== Examples
|
||||
# text_field_tag 'name'
|
||||
# # => <input id="name" name="name" type="text" />
|
||||
@@ -146,13 +146,13 @@ module ActionView
|
||||
# # => <input id="token" name="token" type="hidden" value="VUBJKB23UIVI1UU1VOBVI@" />
|
||||
#
|
||||
# hidden_field_tag 'collected_input', '', :onchange => "alert('Input collected!')"
|
||||
# # => <input id="collected_input" name="collected_input" onchange="alert('Input collected!')"
|
||||
# # => <input id="collected_input" name="collected_input" onchange="alert('Input collected!')"
|
||||
# # type="hidden" value="" />
|
||||
def hidden_field_tag(name, value = nil, options = {})
|
||||
text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
|
||||
end
|
||||
|
||||
# Creates a file upload field. If you are using file uploads then you will also need
|
||||
# Creates a file upload field. If you are using file uploads then you will also need
|
||||
# to set the multipart option for the form tag:
|
||||
#
|
||||
# <%= form_tag { :action => "post" }, { :multipart => true } %>
|
||||
@@ -160,7 +160,7 @@ module ActionView
|
||||
# <%= submit_tag %>
|
||||
# <%= end_form_tag %>
|
||||
#
|
||||
# The specified URL will then be passed a File object containing the selected file, or if the field
|
||||
# The specified URL will then be passed a File object containing the selected file, or if the field
|
||||
# was left blank, a StringIO object.
|
||||
#
|
||||
# ==== Options
|
||||
@@ -181,7 +181,7 @@ module ActionView
|
||||
# # => <input id="resume" name="resume" type="file" value="~/resume.doc" />
|
||||
#
|
||||
# file_field_tag 'user_pic', :accept => 'image/png,image/gif,image/jpeg'
|
||||
# # => <input accept="image/png,image/gif,image/jpeg" id="user_pic" name="user_pic" type="file" />
|
||||
# # => <input accept="image/png,image/gif,image/jpeg" id="user_pic" name="user_pic" type="file" />
|
||||
#
|
||||
# file_field_tag 'file', :accept => 'text/html', :class => 'upload', :value => 'index.html'
|
||||
# # => <input accept="text/html" class="upload" id="file" name="file" type="file" value="index.html" />
|
||||
@@ -286,7 +286,7 @@ module ActionView
|
||||
tag :input, html_options
|
||||
end
|
||||
|
||||
# Creates a radio button; use groups of radio buttons named the same to allow users to
|
||||
# Creates a radio button; use groups of radio buttons named the same to allow users to
|
||||
# select from a group of options.
|
||||
#
|
||||
# ==== Options
|
||||
@@ -313,14 +313,14 @@ module ActionView
|
||||
tag :input, html_options
|
||||
end
|
||||
|
||||
# Creates a submit button with the text <tt>value</tt> as the caption.
|
||||
# Creates a submit button with the text <tt>value</tt> as the caption.
|
||||
#
|
||||
# ==== Options
|
||||
# * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
|
||||
# prompt with the question specified. If the user accepts, the form is
|
||||
# processed normally, otherwise no action is taken.
|
||||
# * <tt>:disabled</tt> - If true, the user will not be able to use this input.
|
||||
# * <tt>:disable_with</tt> - Value of this parameter will be used as the value for a disabled version
|
||||
# * <tt>:disable_with</tt> - Value of this parameter will be used as the value for a disabled version
|
||||
# of the submit button when the form is submitted.
|
||||
# * Any other key creates standard HTML options for the tag.
|
||||
#
|
||||
@@ -335,7 +335,7 @@ module ActionView
|
||||
# # => <input disabled="disabled" name="commit" type="submit" value="Save edits" />
|
||||
#
|
||||
# submit_tag "Complete sale", :disable_with => "Please wait..."
|
||||
# # => <input name="commit" onclick="this.disabled=true;this.value='Please wait...';this.form.submit();"
|
||||
# # => <input name="commit" onclick="this.disabled=true;this.value='Please wait...';this.form.submit();"
|
||||
# # type="submit" value="Complete sale" />
|
||||
#
|
||||
# submit_tag nil, :class => "form_submit"
|
||||
@@ -346,7 +346,7 @@ module ActionView
|
||||
# # name="commit" type="submit" value="Edit" />
|
||||
def submit_tag(value = "Save changes", options = {})
|
||||
options.stringify_keys!
|
||||
|
||||
|
||||
if disable_with = options.delete("disable_with")
|
||||
options["onclick"] = [
|
||||
"this.setAttribute('originalValue', this.value)",
|
||||
@@ -358,15 +358,15 @@ module ActionView
|
||||
"return result;",
|
||||
].join(";")
|
||||
end
|
||||
|
||||
|
||||
if confirm = options.delete("confirm")
|
||||
options["onclick"] ||= ''
|
||||
options["onclick"] += "return #{confirm_javascript_function(confirm)};"
|
||||
end
|
||||
|
||||
|
||||
tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys)
|
||||
end
|
||||
|
||||
|
||||
# Displays an image which when clicked will submit the form.
|
||||
#
|
||||
# <tt>source</tt> is passed to AssetTagHelper#image_path
|
||||
@@ -412,7 +412,7 @@ module ActionView
|
||||
concat(content)
|
||||
concat("</fieldset>")
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def html_options_for_form(url_for_options, options, *parameters_for_url)
|
||||
returning options.stringify_keys do |html_options|
|
||||
@@ -420,7 +420,7 @@ module ActionView
|
||||
html_options["action"] = url_for(url_for_options, *parameters_for_url)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def extra_tags_for_form(html_options)
|
||||
case method = html_options.delete("method").to_s
|
||||
when /^get$/i # must be case-insentive, but can't use downcase as might be nil
|
||||
@@ -434,12 +434,12 @@ module ActionView
|
||||
content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag, :style => 'margin:0;padding:0')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def form_tag_html(html_options)
|
||||
extra_tags = extra_tags_for_form(html_options)
|
||||
tag(:form, html_options, true) + extra_tags
|
||||
end
|
||||
|
||||
|
||||
def form_tag_in_block(html_options, &block)
|
||||
content = capture(&block)
|
||||
concat(form_tag_html(html_options))
|
||||
|
||||
@@ -12,14 +12,14 @@ module ActionView
|
||||
BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple).to_set
|
||||
BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map(&:to_sym))
|
||||
|
||||
# Returns an empty HTML tag of type +name+ which by default is XHTML
|
||||
# compliant. Set +open+ to true to create an open tag compatible
|
||||
# with HTML 4.0 and below. Add HTML attributes by passing an attributes
|
||||
# Returns an empty HTML tag of type +name+ which by default is XHTML
|
||||
# compliant. Set +open+ to true to create an open tag compatible
|
||||
# with HTML 4.0 and below. Add HTML attributes by passing an attributes
|
||||
# hash to +options+. Set +escape+ to false to disable attribute value
|
||||
# escaping.
|
||||
#
|
||||
# ==== Options
|
||||
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
|
||||
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
|
||||
# <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
|
||||
# symbols or strings for the attribute names.
|
||||
#
|
||||
@@ -30,7 +30,7 @@ module ActionView
|
||||
# tag("br", nil, true)
|
||||
# # => <br>
|
||||
#
|
||||
# tag("input", { :type => 'text', :disabled => true })
|
||||
# tag("input", { :type => 'text', :disabled => true })
|
||||
# # => <input type="text" disabled="disabled" />
|
||||
#
|
||||
# tag("img", { :src => "open & shut.png" })
|
||||
@@ -43,13 +43,13 @@ module ActionView
|
||||
end
|
||||
|
||||
# Returns an HTML block tag of type +name+ surrounding the +content+. Add
|
||||
# HTML attributes by passing an attributes hash to +options+.
|
||||
# HTML attributes by passing an attributes hash to +options+.
|
||||
# Instead of passing the content as an argument, you can also use a block
|
||||
# in which case, you pass your +options+ as the second parameter.
|
||||
# Set escape to false to disable attribute value escaping.
|
||||
#
|
||||
# ==== Options
|
||||
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
|
||||
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
|
||||
# <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
|
||||
# symbols or strings for the attribute names.
|
||||
#
|
||||
@@ -68,7 +68,13 @@ module ActionView
|
||||
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
|
||||
if block_given?
|
||||
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
|
||||
concat(content_tag_string(name, capture(&block), options, escape))
|
||||
content_tag = content_tag_string(name, capture(&block), options, escape)
|
||||
|
||||
if block_called_from_erb?(block)
|
||||
concat(content_tag)
|
||||
else
|
||||
content_tag
|
||||
end
|
||||
else
|
||||
content_tag_string(name, content_or_options_with_block, options, escape)
|
||||
end
|
||||
@@ -102,6 +108,16 @@ module ActionView
|
||||
end
|
||||
|
||||
private
|
||||
BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
|
||||
|
||||
# Check whether we're called from an erb template.
|
||||
# We'd return a string in any other case, but erb <%= ... %>
|
||||
# can't take an <% end %> later on, so we have to use <% ... %>
|
||||
# and implicitly concat.
|
||||
def block_called_from_erb?(block)
|
||||
eval(BLOCK_CALLED_FROM_ERB, block)
|
||||
end
|
||||
|
||||
def content_tag_string(name, content, options, escape = true)
|
||||
tag_options = tag_options(options, escape) if options
|
||||
"<#{name}#{tag_options}>#{content}</#{name}>"
|
||||
|
||||
@@ -30,11 +30,7 @@ module ActionView
|
||||
ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.")
|
||||
end
|
||||
|
||||
if output_buffer && string
|
||||
output_buffer << string
|
||||
else
|
||||
string
|
||||
end
|
||||
output_buffer << string
|
||||
end
|
||||
|
||||
if RUBY_VERSION < '1.9'
|
||||
|
||||
@@ -106,7 +106,13 @@ module ActionView
|
||||
locals_code << "#{key} = local_assigns[:#{key}]\n"
|
||||
end
|
||||
|
||||
"def #{render_symbol}(local_assigns)\nold_output_buffer = output_buffer;#{locals_code}#{body}\nensure\nself.output_buffer = old_output_buffer\nend"
|
||||
<<-end_src
|
||||
def #{render_symbol}(local_assigns)
|
||||
old_output_buffer = output_buffer;#{locals_code}#{body}
|
||||
ensure
|
||||
self.output_buffer = old_output_buffer
|
||||
end
|
||||
end_src
|
||||
end
|
||||
|
||||
# Return true if the given template was compiled for a superset of the keys in local_assigns
|
||||
|
||||
@@ -48,7 +48,8 @@ module ActionView
|
||||
self.erb_trim_mode = '-'
|
||||
|
||||
def compile(template)
|
||||
::ERB.new(template.source, nil, erb_trim_mode, '@output_buffer').src
|
||||
src = ::ERB.new(template.source, nil, erb_trim_mode, '@output_buffer').src
|
||||
"__in_erb_template=true;#{src}"
|
||||
end
|
||||
|
||||
def cache_fragment(block, name = {}, options = nil) #:nodoc:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
content_for :title do
|
||||
'Putting stuff in the title!'
|
||||
end
|
||||
xml << "\nGreat stuff!"
|
||||
xml << "\nGreat stuff!"
|
||||
|
||||
@@ -35,21 +35,23 @@ class FormTagHelperTest < ActionView::TestCase
|
||||
expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_form_tag_with_method_delete
|
||||
actual = form_tag({}, { :method => :delete })
|
||||
expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="delete" /></div>)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
def test_form_tag_with_block
|
||||
def test_form_tag_with_block_in_erb
|
||||
__in_erb_template = ''
|
||||
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
|
||||
end
|
||||
|
||||
def test_form_tag_with_block_and_method
|
||||
def test_form_tag_with_block_and_method_in_erb
|
||||
__in_erb_template = ''
|
||||
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>)
|
||||
@@ -88,11 +90,11 @@ class FormTagHelperTest < ActionView::TestCase
|
||||
actual = radio_button_tag("gender", "m") + radio_button_tag("gender", "f")
|
||||
expected = %(<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />)
|
||||
assert_dom_equal expected, actual
|
||||
|
||||
|
||||
actual = radio_button_tag("opinion", "-1") + radio_button_tag("opinion", "1")
|
||||
expected = %(<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />)
|
||||
assert_dom_equal expected, actual
|
||||
|
||||
|
||||
actual = radio_button_tag("person[gender]", "m")
|
||||
expected = %(<input id="person_gender_m" name="person[gender]" type="radio" value="m" />)
|
||||
assert_dom_equal expected, actual
|
||||
@@ -103,13 +105,13 @@ class FormTagHelperTest < ActionView::TestCase
|
||||
expected = %(<select id="people" name="people"><option>david</option></select>)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_select_tag_with_multiple
|
||||
actual = select_tag "colors", "<option>Red</option><option>Blue</option><option>Green</option>", :multiple => :true
|
||||
expected = %(<select id="colors" multiple="multiple" name="colors"><option>Red</option><option>Blue</option><option>Green</option></select>)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_select_tag_disabled
|
||||
actual = select_tag "places", "<option>Home</option><option>Work</option><option>Pub</option>", :disabled => :true
|
||||
expected = %(<select id="places" disabled="disabled" name="places"><option>Home</option><option>Work</option><option>Pub</option></select>)
|
||||
@@ -145,37 +147,37 @@ class FormTagHelperTest < ActionView::TestCase
|
||||
expected = %(<input class="admin" id="title" name="title" type="text" value="Hello!" />)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_text_field_tag_size_symbol
|
||||
actual = text_field_tag "title", "Hello!", :size => 75
|
||||
expected = %(<input id="title" name="title" size="75" type="text" value="Hello!" />)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_text_field_tag_size_string
|
||||
actual = text_field_tag "title", "Hello!", "size" => "75"
|
||||
expected = %(<input id="title" name="title" size="75" type="text" value="Hello!" />)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_text_field_tag_maxlength_symbol
|
||||
actual = text_field_tag "title", "Hello!", :maxlength => 75
|
||||
expected = %(<input id="title" name="title" maxlength="75" type="text" value="Hello!" />)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_text_field_tag_maxlength_string
|
||||
actual = text_field_tag "title", "Hello!", "maxlength" => "75"
|
||||
expected = %(<input id="title" name="title" maxlength="75" type="text" value="Hello!" />)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_text_field_disabled
|
||||
actual = text_field_tag "title", "Hello!", :disabled => :true
|
||||
expected = %(<input id="title" name="title" disabled="disabled" type="text" value="Hello!" />)
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_text_field_tag_with_multiple_options
|
||||
actual = text_field_tag "title", "Hello!", :size => 70, :maxlength => 80
|
||||
expected = %(<input id="title" name="title" size="70" maxlength="80" type="text" value="Hello!" />)
|
||||
@@ -226,12 +228,13 @@ class FormTagHelperTest < ActionView::TestCase
|
||||
submit_tag("Save", :confirm => "Are you sure?")
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
def test_pass
|
||||
assert_equal 1, 1
|
||||
end
|
||||
|
||||
def test_field_set_tag
|
||||
def test_field_set_tag_in_erb
|
||||
__in_erb_template = ''
|
||||
field_set_tag("Your details") { concat "Hello world!" }
|
||||
|
||||
expected = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
|
||||
|
||||
@@ -48,7 +48,7 @@ class JavaScriptHelperTest < ActionView::TestCase
|
||||
end
|
||||
|
||||
def test_link_to_function_with_href
|
||||
assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
|
||||
assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
|
||||
link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
|
||||
end
|
||||
|
||||
@@ -95,12 +95,14 @@ class JavaScriptHelperTest < ActionView::TestCase
|
||||
javascript_tag("alert('hello')", :id => "the_js_tag")
|
||||
end
|
||||
|
||||
def test_javascript_tag_with_block
|
||||
def test_javascript_tag_with_block_in_erb
|
||||
__in_erb_template = ''
|
||||
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
|
||||
def test_javascript_tag_with_block_and_options_in_erb
|
||||
__in_erb_template = ''
|
||||
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
|
||||
|
||||
@@ -54,7 +54,7 @@ class PrototypeHelperBaseTest < ActionView::TestCase
|
||||
end
|
||||
|
||||
def create_generator
|
||||
block = Proc.new { |*args| yield *args if block_given? }
|
||||
block = Proc.new { |*args| yield *args if block_given? }
|
||||
JavaScriptGenerator.new self, &block
|
||||
end
|
||||
end
|
||||
@@ -70,7 +70,7 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
assert_dom_equal %(<a class=\"fine\" href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true}); return false;\">Remote outauthor</a>),
|
||||
link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" })
|
||||
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onComplete:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
|
||||
link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" })
|
||||
link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" })
|
||||
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onSuccess:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
|
||||
link_to_remote("Remote outauthor", :success => "alert(request.responseText)", :url => { :action => "whatnot" })
|
||||
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
|
||||
@@ -78,12 +78,12 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot?a=10&b=20', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
|
||||
link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' })
|
||||
end
|
||||
|
||||
|
||||
def test_link_to_remote_html_options
|
||||
assert_dom_equal %(<a class=\"fine\" href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true}); return false;\">Remote outauthor</a>),
|
||||
link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } })
|
||||
end
|
||||
|
||||
|
||||
def test_link_to_remote_url_quote_escaping
|
||||
assert_dom_equal %(<a href="#" onclick="new Ajax.Request('http://www.example.com/whatnot\\\'s', {asynchronous:true, evalScripts:true}); return false;">Remote</a>),
|
||||
link_to_remote("Remote", { :url => { :action => "whatnot's" } })
|
||||
@@ -93,14 +93,14 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
assert_dom_equal %(<script type="text/javascript">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Updater('schremser_bier', 'http://www.example.com/mehr_bier', {asynchronous:true, evalScripts:true})}, 10)\n//]]>\n</script>),
|
||||
periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" })
|
||||
end
|
||||
|
||||
|
||||
def test_periodically_call_remote_with_frequency
|
||||
assert_dom_equal(
|
||||
"<script type=\"text/javascript\">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true})}, 2)\n//]]>\n</script>",
|
||||
periodically_call_remote(:frequency => 2)
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
def test_form_remote_tag
|
||||
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;\">),
|
||||
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast })
|
||||
@@ -117,21 +117,22 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put })
|
||||
end
|
||||
|
||||
def test_form_remote_tag_with_block
|
||||
def test_form_remote_tag_with_block_in_erb
|
||||
__in_erb_template = ''
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
def test_remote_form_for_with_record_identification_without_html_options
|
||||
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
|
||||
end
|
||||
@@ -139,23 +140,23 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
def test_remote_form_for_with_record_identification_with_existing_record
|
||||
@record.save
|
||||
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
|
||||
end
|
||||
|
||||
def test_remote_form_for_with_new_object_in_list
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
def test_remote_form_for_with_existing_object_in_list
|
||||
@author.save
|
||||
@article.save
|
||||
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
|
||||
end
|
||||
@@ -172,18 +173,18 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
|
||||
form_remote_tag(:update => { :success => "glass_of_beer", :failure => "glass_of_water" }, :url => { :action => :fast }, callback=>"monkeys();")
|
||||
end
|
||||
|
||||
|
||||
#HTTP status codes 200 up to 599 have callbacks
|
||||
#these should work
|
||||
100.upto(599) do |callback|
|
||||
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, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
|
||||
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
|
||||
end
|
||||
|
||||
|
||||
#test 200 and 404
|
||||
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, on200:function(request){monkeys();}, on404:function(request){bananas();}, parameters:Form.serialize(this)}); return false;">),
|
||||
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, 200=>"monkeys();", 404=>"bananas();")
|
||||
|
||||
|
||||
#these shouldn't
|
||||
1.upto(99) do |callback|
|
||||
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;">),
|
||||
@@ -193,44 +194,44 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
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;">),
|
||||
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
|
||||
end
|
||||
|
||||
|
||||
#test ultimate combo
|
||||
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, on200:function(request){monkeys();}, on404:function(request){bananas();}, onComplete:function(request){c();}, onFailure:function(request){f();}, onLoading:function(request){c1()}, onSuccess:function(request){s()}, parameters:Form.serialize(this)}); return false;\">),
|
||||
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :loading => "c1()", :success => "s()", :failure => "f();", :complete => "c();", 200=>"monkeys();", 404=>"bananas();")
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
def test_submit_to_remote
|
||||
assert_dom_equal %(<input name=\"More beer!\" onclick=\"new Ajax.Updater('empty_bottle', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form)}); return false;\" type=\"button\" value=\"1000000\" />),
|
||||
submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle")
|
||||
end
|
||||
|
||||
|
||||
def test_observe_field
|
||||
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/reorder_if_empty', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
|
||||
observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" })
|
||||
end
|
||||
|
||||
|
||||
def test_observe_field_using_with_option
|
||||
expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(value)})})\n//]]>\n</script>)
|
||||
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id')
|
||||
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + encodeURIComponent(value)")
|
||||
end
|
||||
|
||||
|
||||
def test_observe_field_using_json_in_with_option
|
||||
expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:{'id':value}})})\n//]]>\n</script>)
|
||||
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}")
|
||||
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}")
|
||||
end
|
||||
|
||||
|
||||
def test_observe_field_using_function_for_callback
|
||||
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {alert('Element changed')})\n//]]>\n</script>),
|
||||
observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')")
|
||||
end
|
||||
|
||||
|
||||
def test_observe_form
|
||||
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {new Ajax.Request('http://www.example.com/cart_changed', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
|
||||
observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" })
|
||||
end
|
||||
|
||||
|
||||
def test_observe_form_using_function_for_callback
|
||||
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {alert('Form changed')})\n//]]>\n</script>),
|
||||
observe_form("cart", :frequency => 2, :function => "alert('Form changed')")
|
||||
@@ -245,7 +246,7 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
block = Proc.new { |page| page.replace_html('foo', 'bar') }
|
||||
assert_equal create_generator(&block).to_s, update_page(&block)
|
||||
end
|
||||
|
||||
|
||||
def test_update_page_tag
|
||||
block = Proc.new { |page| page.replace_html('foo', 'bar') }
|
||||
assert_equal javascript_tag(create_generator(&block).to_s), update_page_tag(&block)
|
||||
@@ -261,15 +262,15 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
|
||||
def author_path(record)
|
||||
"/authors/#{record.id}"
|
||||
end
|
||||
|
||||
|
||||
def authors_path
|
||||
"/authors"
|
||||
end
|
||||
|
||||
|
||||
def author_articles_path(author)
|
||||
"/authors/#{author.id}/articles"
|
||||
end
|
||||
|
||||
|
||||
def author_article_path(author, article)
|
||||
"/authors/#{author.id}/articles/#{article.id}"
|
||||
end
|
||||
@@ -280,7 +281,7 @@ class JavaScriptGeneratorTest < PrototypeHelperBaseTest
|
||||
super
|
||||
@generator = create_generator
|
||||
end
|
||||
|
||||
|
||||
def test_insert_html_with_string
|
||||
assert_equal 'new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");',
|
||||
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
|
||||
@@ -291,56 +292,56 @@ class JavaScriptGeneratorTest < PrototypeHelperBaseTest
|
||||
assert_equal 'new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");',
|
||||
@generator.insert_html(:after, 'element', '<p>This is a test</p>')
|
||||
end
|
||||
|
||||
|
||||
def test_replace_html_with_string
|
||||
assert_equal 'Element.update("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");',
|
||||
@generator.replace_html('element', '<p>This is a test</p>')
|
||||
end
|
||||
|
||||
|
||||
def test_replace_element_with_string
|
||||
assert_equal 'Element.replace("element", "\\u003Cdiv id=\"element\"\\u003E\\u003Cp\\u003EThis is a test\\u003C/p\\u003E\\u003C/div\\u003E");',
|
||||
@generator.replace('element', '<div id="element"><p>This is a test</p></div>')
|
||||
end
|
||||
|
||||
|
||||
def test_remove
|
||||
assert_equal 'Element.remove("foo");',
|
||||
@generator.remove('foo')
|
||||
assert_equal '["foo", "bar", "baz"].each(Element.remove);',
|
||||
@generator.remove('foo', 'bar', 'baz')
|
||||
end
|
||||
|
||||
|
||||
def test_show
|
||||
assert_equal 'Element.show("foo");',
|
||||
@generator.show('foo')
|
||||
assert_equal '["foo", "bar", "baz"].each(Element.show);',
|
||||
@generator.show('foo', 'bar', 'baz')
|
||||
@generator.show('foo', 'bar', 'baz')
|
||||
end
|
||||
|
||||
|
||||
def test_hide
|
||||
assert_equal 'Element.hide("foo");',
|
||||
@generator.hide('foo')
|
||||
assert_equal '["foo", "bar", "baz"].each(Element.hide);',
|
||||
@generator.hide('foo', 'bar', 'baz')
|
||||
@generator.hide('foo', 'bar', 'baz')
|
||||
end
|
||||
|
||||
|
||||
def test_toggle
|
||||
assert_equal 'Element.toggle("foo");',
|
||||
@generator.toggle('foo')
|
||||
assert_equal '["foo", "bar", "baz"].each(Element.toggle);',
|
||||
@generator.toggle('foo', 'bar', 'baz')
|
||||
@generator.toggle('foo', 'bar', 'baz')
|
||||
end
|
||||
|
||||
|
||||
def test_alert
|
||||
assert_equal 'alert("hello");', @generator.alert('hello')
|
||||
end
|
||||
|
||||
|
||||
def test_redirect_to
|
||||
assert_equal 'window.location.href = "http://www.example.com/welcome";',
|
||||
@generator.redirect_to(:action => 'welcome')
|
||||
assert_equal 'window.location.href = "http://www.example.com/welcome?a=b&c=d";',
|
||||
@generator.redirect_to("http://www.example.com/welcome?a=b&c=d")
|
||||
end
|
||||
|
||||
|
||||
def test_reload
|
||||
assert_equal 'window.location.reload();',
|
||||
@generator.reload
|
||||
@@ -350,16 +351,16 @@ class JavaScriptGeneratorTest < PrototypeHelperBaseTest
|
||||
@generator.delay(20) do
|
||||
@generator.hide('foo')
|
||||
end
|
||||
|
||||
|
||||
assert_equal "setTimeout(function() {\n;\nElement.hide(\"foo\");\n}, 20000);", @generator.to_s
|
||||
end
|
||||
|
||||
|
||||
def test_to_s
|
||||
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
|
||||
@generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
|
||||
@generator.remove('foo', 'bar')
|
||||
@generator.replace_html('baz', '<p>This is a test</p>')
|
||||
|
||||
|
||||
assert_equal <<-EOS.chomp, @generator.to_s
|
||||
new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
|
||||
new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
|
||||
@@ -381,12 +382,12 @@ Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
|
||||
@generator['hello'].hide
|
||||
assert_equal %($("hello").hide();), @generator.to_s
|
||||
end
|
||||
|
||||
|
||||
def test_element_proxy_variable_access
|
||||
@generator['hello']['style']
|
||||
assert_equal %($("hello").style;), @generator.to_s
|
||||
end
|
||||
|
||||
|
||||
def test_element_proxy_variable_access_with_assignment
|
||||
@generator['hello']['style']['color'] = 'red'
|
||||
assert_equal %($("hello").style.color = "red";), @generator.to_s
|
||||
@@ -401,7 +402,7 @@ Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
|
||||
@generator['hello'].hide("first").clean_whitespace
|
||||
assert_equal %($("hello").hide("first").cleanWhitespace();), @generator.to_s
|
||||
end
|
||||
|
||||
|
||||
def test_select_access
|
||||
assert_equal %($$("div.hello");), @generator.select('div.hello')
|
||||
end
|
||||
@@ -410,29 +411,29 @@ Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
|
||||
@generator.select('p.welcome b').first.hide
|
||||
assert_equal %($$("p.welcome b").first().hide();), @generator.to_s
|
||||
end
|
||||
|
||||
|
||||
def test_visual_effect
|
||||
assert_equal %(new Effect.Puff("blah",{});),
|
||||
assert_equal %(new Effect.Puff("blah",{});),
|
||||
@generator.visual_effect(:puff,'blah')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_visual_effect_toggle
|
||||
assert_equal %(Effect.toggle("blah",'appear',{});),
|
||||
assert_equal %(Effect.toggle("blah",'appear',{});),
|
||||
@generator.visual_effect(:toggle_appear,'blah')
|
||||
end
|
||||
|
||||
|
||||
def test_sortable
|
||||
assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
|
||||
assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
|
||||
@generator.sortable('blah', :url => { :action => "order" })
|
||||
end
|
||||
|
||||
|
||||
def test_draggable
|
||||
assert_equal %(new Draggable("blah", {});),
|
||||
assert_equal %(new Draggable("blah", {});),
|
||||
@generator.draggable('blah')
|
||||
end
|
||||
|
||||
|
||||
def test_drop_receiving
|
||||
assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
|
||||
assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
|
||||
@generator.drop_receiving('blah', :url => { :action => "order" })
|
||||
end
|
||||
|
||||
@@ -534,7 +535,7 @@ return array.reverse();
|
||||
});
|
||||
EOS
|
||||
end
|
||||
|
||||
|
||||
def test_collection_proxy_with_find_all
|
||||
@generator.select('p').find_all 'a' do |value, index|
|
||||
@generator << '(value.className == "welcome")'
|
||||
@@ -546,7 +547,7 @@ return (value.className == "welcome");
|
||||
});
|
||||
EOS
|
||||
end
|
||||
|
||||
|
||||
def test_collection_proxy_with_in_groups_of
|
||||
@generator.select('p').in_groups_of('a', 3)
|
||||
@generator.select('p').in_groups_of('a', 3, 'x')
|
||||
@@ -555,13 +556,13 @@ var a = $$("p").inGroupsOf(3);
|
||||
var a = $$("p").inGroupsOf(3, "x");
|
||||
EOS
|
||||
end
|
||||
|
||||
|
||||
def test_collection_proxy_with_each_slice
|
||||
@generator.select('p').each_slice('a', 3)
|
||||
@generator.select('p').each_slice('a', 3) do |group, index|
|
||||
group.reverse
|
||||
end
|
||||
|
||||
|
||||
assert_equal <<-EOS.strip, @generator.to_s
|
||||
var a = $$("p").eachSlice(3);
|
||||
var a = $$("p").eachSlice(3, function(value, index) {
|
||||
@@ -569,7 +570,7 @@ return value.reverse();
|
||||
});
|
||||
EOS
|
||||
end
|
||||
|
||||
|
||||
def test_debug_rjs
|
||||
ActionView::Base.debug_rjs = true
|
||||
@generator['welcome'].replace_html 'Welcome'
|
||||
@@ -577,7 +578,7 @@ return value.reverse();
|
||||
ensure
|
||||
ActionView::Base.debug_rjs = false
|
||||
end
|
||||
|
||||
|
||||
def test_literal
|
||||
literal = @generator.literal("function() {}")
|
||||
assert_equal "function() {}", literal.to_json
|
||||
@@ -588,7 +589,7 @@ return value.reverse();
|
||||
@generator.form.focus('my_field')
|
||||
assert_equal "Form.focus(\"my_field\");", @generator.to_s
|
||||
end
|
||||
|
||||
|
||||
def test_call_with_block
|
||||
@generator.call(:before)
|
||||
@generator.call(:my_method) do |p|
|
||||
@@ -601,7 +602,7 @@ return value.reverse();
|
||||
end
|
||||
assert_equal "before();\nmy_method(function() { $(\"one\").show();\n$(\"two\").hide(); });\nin_between();\nmy_method_with_arguments(true, \"hello\", function() { $(\"three\").visualEffect(\"highlight\"); });", @generator.to_s
|
||||
end
|
||||
|
||||
|
||||
def test_class_proxy_call_with_block
|
||||
@generator.my_object.my_method do |p|
|
||||
p[:one].show
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'abstract_unit'
|
||||
|
||||
class Post
|
||||
def id
|
||||
45
|
||||
45
|
||||
end
|
||||
def body
|
||||
"What a wonderful world!"
|
||||
@@ -15,35 +15,36 @@ class RecordTagHelperTest < ActionView::TestCase
|
||||
def setup
|
||||
@post = Post.new
|
||||
end
|
||||
|
||||
|
||||
def test_content_tag_for
|
||||
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
|
||||
expected = %(<ul class="post" id="archived_post_45"></ul>)
|
||||
actual = content_tag_for(:ul, @post, :archived) { }
|
||||
assert_dom_equal expected, actual
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
|
||||
def test_content_tag_for_with_extra_html_tags
|
||||
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
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
def test_block_works_with_content_tag_for
|
||||
|
||||
def test_block_works_with_content_tag_for_in_erb
|
||||
__in_erb_template = ''
|
||||
expected = %(<tr class="post" id="post_45">#{@post.body}</tr>)
|
||||
actual = content_tag_for(:tr, @post) { concat @post.body }
|
||||
assert_dom_equal expected, actual
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
def test_div_for
|
||||
|
||||
def test_div_for_in_erb
|
||||
__in_erb_template = ''
|
||||
expected = %(<div class="post bar" id="post_45">#{@post.body}</div>)
|
||||
actual = div_for(@post, :class => "bar") { concat @post.body }
|
||||
assert_dom_equal expected, actual
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -33,23 +33,40 @@ class TagHelperTest < ActionView::TestCase
|
||||
assert_equal content_tag("a", "Create", "href" => "create"),
|
||||
content_tag("a", "Create", :href => "create")
|
||||
end
|
||||
|
||||
def test_content_tag_with_block
|
||||
|
||||
def test_content_tag_with_block_in_erb
|
||||
__in_erb_template = ''
|
||||
content_tag(:div) { concat "Hello world!" }
|
||||
assert_dom_equal "<div>Hello world!</div>", output_buffer
|
||||
end
|
||||
|
||||
def test_content_tag_with_block_and_options
|
||||
|
||||
def test_content_tag_with_block_and_options_in_erb
|
||||
__in_erb_template = ''
|
||||
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
|
||||
self.output_buffer = nil
|
||||
assert_equal content_tag("a", "Create", :href => "create"),
|
||||
content_tag("a", "href" => "create") { "Create" }
|
||||
|
||||
def test_content_tag_with_block_and_options_out_of_erb
|
||||
assert_dom_equal %(<div class="green">Hello world!</div>), content_tag(:div, :class => "green") { "Hello world!" }
|
||||
end
|
||||
|
||||
|
||||
def test_content_tag_with_block_and_options_outside_out_of_erb
|
||||
assert_equal content_tag("a", "Create", :href => "create"),
|
||||
content_tag("a", "href" => "create") { "Create" }
|
||||
end
|
||||
|
||||
def test_content_tag_nested_in_content_tag_out_of_erb
|
||||
assert_equal content_tag("p", content_tag("b", "Hello")),
|
||||
content_tag("p") { content_tag("b", "Hello") },
|
||||
output_buffer
|
||||
end
|
||||
|
||||
def test_content_tag_nested_in_content_tag_in_erb
|
||||
__in_erb_template = true
|
||||
content_tag("p") { concat content_tag("b", "Hello") }
|
||||
assert_equal '<p><b>Hello</b></p>', output_buffer
|
||||
end
|
||||
|
||||
def test_cdata_section
|
||||
assert_equal "<![CDATA[<hello world>]]>", cdata_section("<hello world>")
|
||||
end
|
||||
|
||||
@@ -13,9 +13,7 @@ class TextHelperTest < ActionView::TestCase
|
||||
|
||||
def test_concat
|
||||
self.output_buffer = 'foo'
|
||||
concat 'bar'
|
||||
assert_equal 'foobar', output_buffer
|
||||
assert_nothing_raised { concat nil }
|
||||
assert_equal 'foobar', concat('bar')
|
||||
assert_equal 'foobar', output_buffer
|
||||
end
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ class UrlHelperTest < ActionView::TestCase
|
||||
def test_link_tag_with_straight_url
|
||||
assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com")
|
||||
end
|
||||
|
||||
|
||||
def test_link_tag_without_host_option
|
||||
ActionController::Base.class_eval { attr_accessor :url }
|
||||
url = {:controller => 'weblog', :action => 'show'}
|
||||
@@ -121,7 +121,7 @@ class UrlHelperTest < ActionView::TestCase
|
||||
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
|
||||
assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back)
|
||||
end
|
||||
|
||||
|
||||
def test_link_tag_with_back_and_no_referer
|
||||
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
|
||||
assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back)
|
||||
@@ -212,14 +212,14 @@ class UrlHelperTest < ActionView::TestCase
|
||||
assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
|
||||
end
|
||||
|
||||
def test_link_tag_using_block
|
||||
self.output_buffer = ''
|
||||
def test_link_tag_using_block_in_erb
|
||||
__in_erb_template = ''
|
||||
|
||||
link_to("http://example.com") { concat("Example site") }
|
||||
|
||||
assert_equal '<a href="http://example.com">Example site</a>', output_buffer
|
||||
end
|
||||
|
||||
|
||||
def test_link_to_unless
|
||||
assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
|
||||
assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")
|
||||
@@ -293,7 +293,7 @@ class UrlHelperTest < ActionView::TestCase
|
||||
assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me(at)domain(dot)com</a>", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)", :replace_dot => "(dot)")
|
||||
assert_dom_equal "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
|
||||
end
|
||||
|
||||
|
||||
def protect_against_forgery?
|
||||
false
|
||||
end
|
||||
@@ -420,11 +420,11 @@ class Workshop
|
||||
def initialize(id, new_record)
|
||||
@id, @new_record = id, new_record
|
||||
end
|
||||
|
||||
|
||||
def new_record?
|
||||
@new_record
|
||||
end
|
||||
|
||||
|
||||
def to_s
|
||||
id.to_s
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user