Skip to content

Commit

Permalink
Refactor FormBuilder arguments and default config
Browse files Browse the repository at this point in the history
* Do not reopen AV::Base to define default form builder
  Inside the load hook we are already in AV::Base context.

* Do not pass the given block to the form builder
  The block is evaluated in fields_for context using capture, with the
  builder as argument. This means we do not need to give the block to the
  FormBuilder itself.
  • Loading branch information
carlosantoniodasilva committed Jan 18, 2012
1 parent d4a9ce8 commit 56089ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
17 changes: 7 additions & 10 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -373,7 +373,7 @@ def form_for(record, options = {}, &proc)
options[:html][:method] = options.delete(:method) if options.has_key?(:method)
options[:html][:authenticity_token] = options.delete(:authenticity_token)

builder = options[:parent_builder] = instantiate_builder(object_name, object, options, &proc)
builder = options[:parent_builder] = instantiate_builder(object_name, object, options)
fields_for = fields_for(object_name, object, options, &proc)
default_options = builder.multipart? ? { :multipart => true } : {}
default_options.merge!(options.delete(:html))
Expand Down Expand Up @@ -601,7 +601,7 @@ def apply_form_for_options!(record, object, options) #:nodoc:
# ...
# <% end %>
def fields_for(record_name, record_object = nil, options = {}, &block)
builder = instantiate_builder(record_name, record_object, options, &block)
builder = instantiate_builder(record_name, record_object, options)
output = capture(builder, &block)
output.concat builder.hidden_field(:id) if output && options[:hidden_field_id] && !builder.emitted_hidden_id?
output
Expand Down Expand Up @@ -925,7 +925,7 @@ def range_field(object_name, method, options = {})

private

def instantiate_builder(record_name, record_object, options, &block)
def instantiate_builder(record_name, record_object, options)
case record_name
when String, Symbol
object = record_object
Expand All @@ -936,7 +936,7 @@ def instantiate_builder(record_name, record_object, options, &block)
end

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

Expand Down Expand Up @@ -967,9 +967,9 @@ def to_model
self
end

def initialize(object_name, object, template, options, proc)
def initialize(object_name, object, template, options)
@nested_child_index = {}
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
@object_name, @object, @template, @options = object_name, object, template, options
@parent_builder = options[:parent_builder]
@default_options = @options ? @options.slice(:index, :namespace) : {}
if @object_name.to_s.match(/\[\]$/)
Expand Down Expand Up @@ -1183,9 +1183,6 @@ def convert_to_model(object)
end

ActiveSupport.on_load(:action_view) do
class ActionView::Base
cattr_accessor :default_form_builder
@@default_form_builder = ::ActionView::Helpers::FormBuilder
end
cattr_accessor(:default_form_builder) { ::ActionView::Helpers::FormBuilder }
end
end
6 changes: 3 additions & 3 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -9,7 +9,7 @@ def hello_world
end

def nested_partial_with_form_builder
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}, Proc.new {})
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {})
end
end
end
Expand Down Expand Up @@ -558,11 +558,11 @@ def partial_with_locals
end

def partial_with_form_builder
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}, Proc.new {})
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {})
end

def partial_with_form_builder_subclass
render :partial => LabellingFormBuilder.new(:post, nil, view_context, {}, Proc.new {})
render :partial => LabellingFormBuilder.new(:post, nil, view_context, {})
end

def partial_collection
Expand Down

0 comments on commit 56089ca

Please sign in to comment.