提交 f0479cbb 编写于 作者: C Carlos Antonio da Silva

Refactor fields_for to make the api more clear, and fix usage with non nested...

Refactor fields_for to make the api more clear, and fix usage with non nested attributes and without object
上级 6871cda6
...@@ -1216,35 +1216,30 @@ def #{selector}(method, options = {}) # def text_field(method, options = {}) ...@@ -1216,35 +1216,30 @@ def #{selector}(method, options = {}) # def text_field(method, options = {})
RUBY_EVAL RUBY_EVAL
end end
def fields_for(record_or_name_or_array, *args, &block) def fields_for(record_name, record_object = nil, fields_options = {}, &block)
if options.has_key?(:index) fields_options, record_object = record_object, nil if record_object.is_a?(Hash)
index = "[#{options[:index]}]" fields_options[:builder] ||= options[:builder]
elsif defined?(@auto_index) fields_options[:parent_builder] = self
self.object_name = @object_name.to_s.sub(/\[\]$/,"")
index = "[#{@auto_index}]"
else
index = ""
end
args << {} unless args.last.is_a?(Hash)
args.last[:builder] ||= options[:builder]
args.last[:parent_builder] = self
case record_or_name_or_array case record_name
when String, Symbol when String, Symbol
if nested_attributes_association?(record_or_name_or_array) if nested_attributes_association?(record_name)
return fields_for_with_nested_attributes(record_or_name_or_array, args, block) return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
else
name = record_or_name_or_array
end end
else else
object = record_or_name_or_array.is_a?(Array) ? record_or_name_or_array.last : record_or_name_or_array record_object = record_name.is_a?(Array) ? record_name.last : record_name
name = ActiveModel::Naming.param_key(object) record_name = ActiveModel::Naming.param_key(record_object)
args.unshift(object) end
index = if options.has_key?(:index)
"[#{options[:index]}]"
elsif defined?(@auto_index)
self.object_name = @object_name.to_s.sub(/\[\]$/,"")
"[#{@auto_index}]"
end end
name = "#{object_name}#{index}[#{name}]" record_name = "#{object_name}#{index}[#{record_name}]"
@template.fields_for(name, *args, &block) @template.fields_for(record_name, record_object, fields_options, &block)
end end
def label(method, text = nil, options = {}, &block) def label(method, text = nil, options = {}, &block)
...@@ -1333,10 +1328,8 @@ def nested_attributes_association?(association_name) ...@@ -1333,10 +1328,8 @@ def nested_attributes_association?(association_name)
@object.respond_to?("#{association_name}_attributes=") @object.respond_to?("#{association_name}_attributes=")
end end
def fields_for_with_nested_attributes(association_name, args, block) def fields_for_with_nested_attributes(association_name, association, options, block)
name = "#{object_name}[#{association_name}_attributes]" name = "#{object_name}[#{association_name}_attributes]"
options = args.extract_options!
association = args.shift
association = convert_to_model(association) association = convert_to_model(association)
if association.respond_to?(:persisted?) if association.respond_to?(:persisted?)
......
...@@ -1725,6 +1725,20 @@ def test_form_for_and_fields_for_with_object ...@@ -1725,6 +1725,20 @@ def test_form_for_and_fields_for_with_object
assert_dom_equal expected, output_buffer assert_dom_equal expected, output_buffer
end end
def test_form_for_and_fields_for_with_non_nested_association_and_without_object
form_for(@post) do |f|
concat f.fields_for(:category) { |c|
concat c.text_field(:name)
}
end
expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', 'put') do
"<input name='post[category][name]' type='text' size='30' id='post_category_name' />"
end
assert_dom_equal expected, output_buffer
end
class LabelledFormBuilder < ActionView::Helpers::FormBuilder class LabelledFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(hidden_field)).each do |selector| (field_helpers - %w(hidden_field)).each do |selector|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册