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 7, 2012
1 parent bcd2269 commit a41bd90
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*

* Remove the leading \n added by textarea on assert_select. *Santiago Pastorino*

* Changed default value for `config.action_view.embed_authenticity_token_in_remote_forms`
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 @@ -1026,9 +1027,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 @@ -2119,6 +2119,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 a41bd90

Please sign in to comment.