diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-04-07 19:29:17 +0200 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-04-28 22:32:46 -0700 |
commit | e6ab0d56cf5d376f4b351282c606ecc74e6fad9a (patch) | |
tree | 73fc6ea95a96c41c91e321ff46fdf893275e857a /actionpack | |
parent | 1a4e27ff96b3b7add1a4a43ca757ea8ee6c05c00 (diff) | |
download | rails-e6ab0d56cf5d376f4b351282c606ecc74e6fad9a.tar.gz rails-e6ab0d56cf5d376f4b351282c606ecc74e6fad9a.tar.bz2 rails-e6ab0d56cf5d376f4b351282c606ecc74e6fad9a.zip |
Lazy load `default_form_builder` if it's passed as a string
closes #3341
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG.md | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 8 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 17 |
3 files changed, 26 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 9f1c44ca66..e827f935c3 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Allow to lazy load `default_form_builder` by passing a `String` instead of a constant. *Piotr Sarnacki* + * Session arguments passed to `process` calls in functional tests are now merged into the existing session, whereas previously they would replace the existing session. This change may break some existing tests if they are asserting the exact contents of diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 8e7224937d..67f2abe509 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -10,6 +10,7 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/string/output_safety' require 'active_support/core_ext/array/extract_options' require 'active_support/deprecation' +require 'active_support/core_ext/string/inflections' module ActionView # = Action View Form Helpers @@ -1039,9 +1040,14 @@ module ActionView object_name = ActiveModel::Naming.param_key(object) end - builder = options[:builder] || ActionView::Base.default_form_builder + builder = options[:builder] || default_form_builder builder.new(object_name, object, self, options) end + + def default_form_builder + builder = ActionView::Base.default_form_builder + builder.respond_to?(:constantize) ? builder.constantize : builder + end end class FormBuilder diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index a714264909..4018c96c99 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -2169,6 +2169,23 @@ class FormHelperTest < ActionView::TestCase ActionView::Base.default_form_builder = old_default_form_builder end + def test_lazy_loading_default_form_builder + old_default_form_builder, ActionView::Base.default_form_builder = + ActionView::Base.default_form_builder, "FormHelperTest::LabelledFormBuilder" + + form_for(@post) do |f| + concat f.text_field(:title) + end + + expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do + "<label for='title'>Title:</label> <input name='post[title]' type='text' id='post_title' value='Hello World' /><br/>" + end + + assert_dom_equal expected, output_buffer + ensure + ActionView::Base.default_form_builder = old_default_form_builder + end + def test_fields_for_with_labelled_builder output_buffer = fields_for(:post, @post, :builder => LabelledFormBuilder) do |f| concat f.text_field(:title) |