Permalink
Browse files

Make form helpers work with <%=

  • Loading branch information...
wycats committed Mar 10, 2010
1 parent 4464b8e commit 7b622786fcc5046a06989ec7a3cbf46f92e04dea
@@ -6,6 +6,7 @@ module DeprecatedBlockHelpers
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::JavaScriptHelper
+ include ActionView::Helpers::FormHelper
def content_tag(*, &block)
block_called_from_erb?(block) ? safe_concat(super) : super
@@ -15,6 +16,22 @@ def javascript_tag(*, &block)
block_called_from_erb?(block) ? safe_concat(super) : super
end
+ def form_for(*, &block)
+ block_called_from_erb?(block) ? safe_concat(super) : super
+ end
+
+ def form_tag(*, &block)
+ block_called_from_erb?(block) ? safe_concat(super) : super
+ end
+
+ def fields_for(*, &block)
+ block_called_from_erb?(block) ? safe_concat(super) : super
+ end
+
+ def field_set_tag(*, &block)
+ block_called_from_erb?(block) ? safe_concat(super) : super
+ end
+
BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
if RUBY_VERSION < '1.9.0'
@@ -92,6 +92,10 @@ module Helpers
# link:classes/ActionView/Helpers/DateHelper.html, and
# link:classes/ActionView/Helpers/ActiveRecordHelper.html
module FormHelper
+ extend ActiveSupport::Concern
+
+ include FormTagHelper
+
# Creates a form and a scope around a specific model object that is used
# as a base for questioning about values for the fields.
#
@@ -309,9 +313,9 @@ def form_for(record_or_name_or_array, *args, &proc)
options[:html][:remote] = true if options.delete(:remote)
- safe_concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}))
- fields_for(object_name, *(args << options), &proc)
- safe_concat('</form>')
+ output = form_tag(options.delete(:url) || {}, options.delete(:html) || {})
+ output << fields_for(object_name, *(args << options), &proc)
+ output.safe_concat('</form>')
end
def apply_form_for_options!(object_or_array, options) #:nodoc:
@@ -528,7 +532,10 @@ def fields_for(record_or_name_or_array, *args, &block)
end
builder = options[:builder] || ActionView::Base.default_form_builder
- yield builder.new(object_name, object, self, options, block)
+
+ with_output_buffer do
+ yield builder.new(object_name, object, self, options, block)
+ end
end
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
@@ -1183,9 +1190,11 @@ def fields_for_with_nested_attributes(association_name, args, block)
if association.is_a?(Array)
explicit_child_index = options[:child_index]
- association.map do |child|
- fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
- end.join
+ output = ActiveSupport::SafeBuffer.new
+ association.each do |child|
+ output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
+ end
+ output
elsif association
fields_for_nested_model(name, association, options, block)
end
@@ -97,7 +97,9 @@ module Helpers
# </select>
#
module FormOptionsHelper
+ # ERB::Util can mask some helpers like textilize. Make sure to include them.
include ERB::Util
+ include TextHelper
# Create a select tag and a series of contained option tags for the provided object and method.
# The option currently held by the object will be selected, provided that the object is available.
@@ -10,6 +10,11 @@ module Helpers
# 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
+ extend ActiveSupport::Concern
+
+ include UrlHelper
+ include TextHelper
+
# Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like
# ActionController::Base#url_for. The method for the form defaults to POST.
#
@@ -441,10 +446,10 @@ def image_submit_tag(source, options = {})
# # => <fieldset class="format"><p><input id="name" name="name" type="text" /></p></fieldset>
def field_set_tag(legend = nil, options = nil, &block)
content = capture(&block)
- safe_concat(tag(:fieldset, options, true))
- safe_concat(content_tag(:legend, legend)) unless legend.blank?
- concat(content)
- safe_concat("</fieldset>")
+ output = tag(:fieldset, options, true)
+ output.safe_concat(content_tag(:legend, legend)) unless legend.blank?
+ output.concat(content)
+ output.safe_concat("</fieldset>")
end
private
@@ -477,9 +482,10 @@ def form_tag_html(html_options)
def form_tag_in_block(html_options, &block)
content = capture(&block)
- safe_concat(form_tag_html(html_options))
- concat(content)
- safe_concat("</form>")
+ output = ActiveSupport::SafeBuffer.new
+ output.safe_concat(form_tag_html(html_options))
+ output << content
+ output.safe_concat("</form>")
end
def token_tag
@@ -28,10 +28,18 @@ def to_s
@buffer.to_s
end
+ def to_str
+ @buffer.to_str
+ end
+
def empty?
@buffer.empty?
end
+ def html_safe?
+ @buffer.html_safe?
+ end
+
if "".respond_to?(:force_encoding)
def force_encoding(encoding)
@buffer.force_encoding(encoding)
@@ -1250,7 +1250,7 @@ def test_date_select_within_fields_for
@post = Post.new
@post.written_on = Date.new(2004, 6, 15)
- fields_for :post, @post do |f|
+ output_buffer = fields_for :post, @post do |f|
concat f.date_select(:written_on)
end
@@ -1266,7 +1266,7 @@ def test_date_select_within_fields_for_with_index
@post.written_on = Date.new(2004, 6, 15)
id = 27
- fields_for :post, @post, :index => id do |f|
+ output_buffer = fields_for :post, @post, :index => id do |f|
concat f.date_select(:written_on)
end
@@ -1282,7 +1282,7 @@ def test_date_select_within_fields_for_with_blank_index
@post.written_on = Date.new(2004, 6, 15)
id = nil
- fields_for :post, @post, :index => id do |f|
+ output_buffer = fields_for :post, @post, :index => id do |f|
concat f.date_select(:written_on)
end
@@ -1478,7 +1478,7 @@ def test_date_select_with_html_options_within_fields_for
@post = Post.new
@post.written_on = Date.new(2004, 6, 15)
- fields_for :post, @post do |f|
+ output_buffer = fields_for :post, @post do |f|
concat f.date_select(:written_on, {}, :class => 'selector')
end
@@ -1642,7 +1642,7 @@ def test_time_select_with_html_options_within_fields_for
@post = Post.new
@post.written_on = Time.local(2004, 6, 15, 15, 16, 35)
- fields_for :post, @post do |f|
+ output_buffer = fields_for :post, @post do |f|
concat f.time_select(:written_on, {}, :class => 'selector')
end
@@ -1816,7 +1816,7 @@ def test_datetime_select_with_html_options_within_fields_for
@post = Post.new
@post.updated_at = Time.local(2004, 6, 15, 16, 35)
- fields_for :post, @post do |f|
+ output_buffer = fields_for :post, @post do |f|
concat f.datetime_select(:updated_at, {}, :class => 'selector')
end
@@ -2052,7 +2052,7 @@ def test_datetime_select_within_fields_for_with_options_index
@post.updated_at = Time.local(2004, 6, 15, 16, 35)
id = 456
- fields_for :post, @post, :index => id do |f|
+ output_buffer = fields_for :post, @post, :index => id do |f|
concat f.datetime_select(:updated_at)
end
@@ -1,6 +1,28 @@
require "abstract_unit"
module ERBTest
+ class ViewContext
+ mock_controller = Class.new do
+ include SharedTestRoutes.url_helpers
+ end
+
+ include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::JavaScriptHelper
+ include ActionView::Helpers::FormHelper
+
+ attr_accessor :output_buffer
+
+ def protect_against_forgery?() false end
+
+ define_method(:controller) do
+ mock_controller.new
+ end
+ end
+
+ class DeprecatedViewContext < ViewContext
+ include ActionView::Helpers::DeprecatedBlockHelpers
+ end
+
module SharedTagHelpers
extend ActiveSupport::Testing::Declarative
@@ -22,16 +44,21 @@ def render_content(start, inside)
expected_output = "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('Hello')\n//]]>\n</script>"
assert_equal expected_output, render_content("javascript_tag(:id => 'the_js_tag')", "alert('Hello')")
end
+
+ test "percent equals works with form tags" do
+ expected_output = "<form action=\"foo\" method=\"post\">hello</form>"
+ assert_equal expected_output, render_content("form_tag('foo')", "<%= 'hello' %>")
+ end
+
+ test "percent equals works with fieldset tags" do
+ expected_output = "<fieldset><legend>foo</legend>hello</fieldset>"
+ assert_equal expected_output, render_content("field_set_tag('foo')", "<%= 'hello' %>")
+ end
end
class TagHelperTest < ActiveSupport::TestCase
def context
- Class.new do
- include ActionView::Helpers::TagHelper
- include ActionView::Helpers::JavaScriptHelper
-
- attr_accessor :output_buffer
- end
+ ViewContext
end
def block_helper(str, rest)
@@ -43,12 +70,7 @@ def block_helper(str, rest)
class DeprecatedTagHelperTest < ActiveSupport::TestCase
def context
- Class.new do
- include ActionView::Helpers::TagHelper
- include ActionView::Helpers::JavaScriptHelper
- include ActionView::Helpers::DeprecatedBlockHelpers
- attr_accessor :output_buffer
- end
+ DeprecatedViewContext
end
def block_helper(str, rest)
Oops, something went wrong.

0 comments on commit 7b62278

Please sign in to comment.