Skip to content
Browse files

Support render :partial => collection of heterogeneous elements. Closes

#11491.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9177 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 2681d55 commit 9cc478a2547bec5b72fa2a4b36b2f8e627373a74 @jeremy jeremy committed
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Support render :partial => collection of heterogeneous elements. #11491 [Zach Dennis]
+
* Avoid remote_ip spoofing. [Brian Candler]
* Added support for regexp flags like ignoring case in the :requirements part of routes declarations #11421 [NeilW]
View
13 actionpack/lib/action_view/partial_template.rb
@@ -7,7 +7,7 @@ def initialize(view, partial_path, object = nil, locals = {})
@path, @variable_name = extract_partial_name_and_path(view, partial_path)
super(view, @path, true, locals)
add_object_to_local_assigns!(object)
-
+
# This is needed here in order to compile template with knowledge of 'counter'
initialize_counter
@@ -24,11 +24,18 @@ def render
def render_member(object)
@locals[@counter_name] += 1
@locals[:object] = @locals[@variable_name] = object
- render
+ returning render do
+ @locals.delete(@variable_name)
+ @locals.delete(:object)
+ end
end
+ def counter=(num)
+ @locals[@counter_name] = num
+ end
+
private
-
+
def add_object_to_local_assigns!(object)
@locals[:object] ||=
@locals[@variable_name] ||=
View
32 actionpack/lib/action_view/partials.rb
@@ -113,9 +113,8 @@ def render_partial(partial_path, object_assigns = nil, local_assigns = {}) #:nod
render_partial(builder_partial_path, object_assigns, (local_assigns || {}).merge(builder_partial_path.to_sym => partial_path))
when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Associations::HasManyThroughAssociation
if partial_path.any?
- path = ActionController::RecordIdentifier.partial_path(partial_path.first)
collection = partial_path
- render_partial_collection(path, collection, nil, local_assigns)
+ render_partial_collection(nil, collection, nil, local_assigns)
else
""
end
@@ -126,15 +125,34 @@ def render_partial(partial_path, object_assigns = nil, local_assigns = {}) #:nod
def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}) #:nodoc:
return " " if collection.empty?
-
+
local_assigns = local_assigns ? local_assigns.clone : {}
- template = ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns)
-
spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : ''
-
+
+ if partial_path.nil?
+ render_partial_collection_with_unknown_partial_path(collection, local_assigns, spacer)
+ else
+ render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns, spacer)
+ end.join(spacer)
+ end
+
+ def render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns, spacer)
+ template = ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns)
collection.map do |element|
template.render_member(element)
- end.join(spacer)
+ end
+ end
+
+ def render_partial_collection_with_unknown_partial_path(collection, local_assigns, spacer)
+ templates = Hash.new
+ i = 0
+ collection.map do |element|
+ partial_path = ActionController::RecordIdentifier.partial_path(element)
+ template = templates[partial_path] ||= ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns)
+ template.counter = i
+ i += 1
+ template.render_member(element)
+ end
end
end
end
View
6 actionpack/test/controller/fake_models.rb
@@ -3,3 +3,9 @@ def to_param
id.to_s
end
end
+
+class BadCustomer < Customer
+end
+
+class GoodCustomer < Customer
+end
View
20 actionpack/test/controller/new_render_test.rb
@@ -163,6 +163,21 @@ def partial_collection_shorthand_with_locals
render :partial => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" }
end
+ def partial_collection_shorthand_with_different_types_of_records
+ render :partial => [
+ BadCustomer.new("mark"),
+ GoodCustomer.new("craig"),
+ BadCustomer.new("john"),
+ GoodCustomer.new("zach"),
+ GoodCustomer.new("brandon"),
+ BadCustomer.new("dan") ],
+ :locals => { :greeting => "Bonjour" }
+ end
+
+ def partial_collection_shorthand_with_different_types_of_records_with_counter
+ partial_collection_shorthand_with_different_types_of_records
+ end
+
def empty_partial_collection
render :partial => "customer", :collection => []
end
@@ -741,6 +756,11 @@ def test_partial_collection_shorthand_with_locals
assert_equal "Bonjour: davidBonjour: mary", @response.body
end
+ def test_partial_collection_shorthand_with_different_types_of_records
+ get :partial_collection_shorthand_with_different_types_of_records
+ assert_equal "Bonjour bad customer: mark1Bonjour good customer: craig2Bonjour bad customer: john3Bonjour good customer: zach4Bonjour good customer: brandon5Bonjour bad customer: dan6", @response.body
+ end
+
def test_empty_partial_collection
get :empty_partial_collection
assert_equal " ", @response.body

0 comments on commit 9cc478a

Please sign in to comment.
Something went wrong with that request. Please try again.