Skip to content

Commit

Permalink
Set ActionView::Base.default_form_builder once rather than passing th…
Browse files Browse the repository at this point in the history
…e :builder option to every form or overriding the form helper methods.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5422 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Nov 3, 2006
1 parent 9a39a86 commit 7f6c5a5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Set ActionView::Base.default_form_builder once rather than passing the :builder option to every form or overriding the form helper methods. [Jeremy Kemper]

* Deprecate expire_matched_fragments. Use expire_fragment instead. #6535 [Bob Silva]

* Update to latest Prototype, which doesn't serialize disabled form elements, adds clone() to arrays, empty/non-string Element.update() and adds a fixes excessive error reporting in WebKit beta versions [Thomas Fuchs]
Expand Down
11 changes: 9 additions & 2 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -142,11 +142,13 @@ def form_for(object_name, *args, &proc)
#
# Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base.
# Like collection_select and datetime_select.
def fields_for(object_name, *args, &proc)
def fields_for(object_name, *args, &block)
raise ArgumentError, "Missing block" unless block_given?
options = args.last.is_a?(Hash) ? args.pop : {}
object = args.first
yield((options[:builder] || FormBuilder).new(object_name, object, self, options, proc))

builder = options[:builder] || ActionView::Base.default_form_builder
yield builder.new(object_name, object, self, options, block)
end

# Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object
Expand Down Expand Up @@ -436,4 +438,9 @@ def radio_button(method, tag_value, options = {})
end
end
end

class Base
cattr_accessor :default_form_builder
self.default_form_builder = ::ActionView::Helpers::FormBuilder
end
end
26 changes: 25 additions & 1 deletion actionpack/test/template/form_helper_test.rb
Expand Up @@ -396,7 +396,31 @@ def test_form_for_with_labelled_builder

assert_dom_equal expected, _erbout
end


def test_default_form_builder
old_default_form_builder, ActionView::Base.default_form_builder =
ActionView::Base.default_form_builder, LabelledFormBuilder

_erbout = ''
form_for(:post, @post) do |f|
_erbout.concat f.text_field(:title)
_erbout.concat f.text_area(:body)
_erbout.concat f.check_box(:secret)
end

expected =
"<form action='http://www.example.com' method='post'>" +
"<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
"<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
"<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/>" +
"</form>"

assert_dom_equal expected, _erbout
ensure
ActionView::Base.default_form_builder = old_default_form_builder
end

# Perhaps this test should be moved to prototype helper tests.
def test_remote_form_for_with_labelled_builder
self.extend ActionView::Helpers::PrototypeHelper
Expand Down

0 comments on commit 7f6c5a5

Please sign in to comment.