mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix incomplete work from [6951] that was hidden by test stubs. Closes #6432.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6959 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
5dd3db8615
commit
5600776e30
2 changed files with 30 additions and 19 deletions
|
@ -155,23 +155,24 @@ module ActionView
|
|||
# end
|
||||
#
|
||||
# If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag.
|
||||
def form_for(record_or_name, *args, &proc)
|
||||
def form_for(record_or_name_or_array, *args, &proc)
|
||||
raise ArgumentError, "Missing block" unless block_given?
|
||||
|
||||
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||
|
||||
case record_or_name
|
||||
case record_or_name_or_array
|
||||
when String, Symbol
|
||||
object_name = record_or_name
|
||||
object_name = record_or_name_or_array
|
||||
when Array
|
||||
object = record_or_name.last
|
||||
object = record_or_name_or_array.last
|
||||
object_name = ActionController::RecordIdentifier.singular_class_name(object)
|
||||
apply_form_for_options!(object, options, *record_or_name)
|
||||
# apply_form_for_options!(object, options, *record_or_name_or_array)
|
||||
apply_form_for_options!(record_or_name_or_array, options)
|
||||
args.unshift object
|
||||
else
|
||||
object = record_or_name
|
||||
object = record_or_name_or_array
|
||||
object_name = ActionController::RecordIdentifier.singular_class_name(object)
|
||||
apply_form_for_options!(object, options)
|
||||
apply_form_for_options!([ object ], options)
|
||||
args.unshift object
|
||||
end
|
||||
|
||||
|
@ -180,7 +181,9 @@ module ActionView
|
|||
concat('</form>', proc.binding)
|
||||
end
|
||||
|
||||
def apply_form_for_options!(object, options, *nested_objects) #:nodoc:
|
||||
def apply_form_for_options!(object_or_array, options) #:nodoc:
|
||||
object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array
|
||||
|
||||
html_options =
|
||||
if object.respond_to?(:new_record?) && object.new_record?
|
||||
{ :class => dom_class(object, :new), :id => dom_id(object), :method => :post }
|
||||
|
@ -190,7 +193,7 @@ module ActionView
|
|||
|
||||
options[:html] ||= {}
|
||||
options[:html].reverse_merge!(html_options)
|
||||
options[:url] ||= polymorphic_path(object, *nested_objects)
|
||||
options[:url] ||= polymorphic_path(object_or_array)
|
||||
end
|
||||
|
||||
# Creates a scope around a specific model object like form_for, but doesn't create the form tags themselves. This makes
|
||||
|
|
|
@ -641,19 +641,27 @@ class FormHelperTest < Test::Unit::TestCase
|
|||
"/posts/#{post.id}/comments/#{comment.id}"
|
||||
end
|
||||
|
||||
def polymorphic_path(object, *nested_objects)
|
||||
if nested_objects.empty?
|
||||
if object.new_record?
|
||||
"/posts"
|
||||
else
|
||||
"/posts/#{object.id}"
|
||||
end
|
||||
def polymorphic_path(record_or_hash_or_array)
|
||||
if record_or_hash_or_array.is_a?(Array)
|
||||
record = record_or_hash_or_array.pop
|
||||
array = record_or_hash_or_array
|
||||
else
|
||||
if object.new_record?
|
||||
record = record_or_hash_or_array
|
||||
array = [ ]
|
||||
end
|
||||
|
||||
if array.size > 0
|
||||
if record.new_record?
|
||||
"/posts/123/comments"
|
||||
else
|
||||
"/posts/123/comments/#{object.id}"
|
||||
"/posts/123/comments/#{record.id}"
|
||||
end
|
||||
end
|
||||
else
|
||||
if record.new_record?
|
||||
"/posts"
|
||||
else
|
||||
"/posts/#{record.id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue