Skip to content

Commit

Permalink
Lazy load default_form_builder if it's passed as a string
Browse files Browse the repository at this point in the history
closes rails#3341
  • Loading branch information
drogus committed Apr 29, 2012
1 parent 1a4e27f commit e6ab0d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions 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
Expand Down
8 changes: 7 additions & 1 deletion actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -10,6 +10,7 @@
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
Expand Down Expand Up @@ -1039,9 +1040,14 @@ def instantiate_builder(record_name, record_object, options)
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
Expand Down
17 changes: 17 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -2169,6 +2169,23 @@ def test_default_form_builder
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)
Expand Down
2 changes: 1 addition & 1 deletion guides/source/configuring.textile
Expand Up @@ -362,7 +362,7 @@ h4. Configuring Action View
Proc.new { |html_tag, instance| %Q(<div class="field_with_errors">#{html_tag}</div>).html_safe }
</ruby>

* +config.action_view.default_form_builder+ tells Rails which form builder to use by default. The default is +ActionView::Helpers::FormBuilder+.
* +config.action_view.default_form_builder+ tells Rails which form builder to use by default. The default is +ActionView::Helpers::FormBuilder+. If you want your form builder class to be loaded after initialization (so it's reloaded on each request in development), you can pass it as a +String+

* +config.action_view.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action View. Set to +nil+ to disable logging.

Expand Down

0 comments on commit e6ab0d5

Please sign in to comment.