Changeset 9177
- Timestamp:
- 04/01/08 00:50:09 (1 month ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_view/partial_template.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_view/partials.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/fake_models.rb (modified) (1 diff)
- trunk/actionpack/test/controller/new_render_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r9124 r9177 1 1 *SVN* 2 3 * Support render :partial => collection of heterogeneous elements. #11491 [Zach Dennis] 2 4 3 5 * Avoid remote_ip spoofing. [Brian Candler] trunk/actionpack/lib/action_view/partial_template.rb
r8976 r9177 8 8 super(view, @path, true, locals) 9 9 add_object_to_local_assigns!(object) 10 10 11 11 # This is needed here in order to compile template with knowledge of 'counter' 12 12 initialize_counter … … 25 25 @locals[@counter_name] += 1 26 26 @locals[:object] = @locals[@variable_name] = object 27 render 27 returning render do 28 @locals.delete(@variable_name) 29 @locals.delete(:object) 30 end 28 31 end 29 32 33 def counter=(num) 34 @locals[@counter_name] = num 35 end 36 30 37 private 31 38 32 39 def add_object_to_local_assigns!(object) 33 40 @locals[:object] ||= trunk/actionpack/lib/action_view/partials.rb
r8976 r9177 114 114 when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Associations::HasManyThroughAssociation 115 115 if partial_path.any? 116 path = ActionController::RecordIdentifier.partial_path(partial_path.first)117 116 collection = partial_path 118 render_partial_collection( path, collection, nil, local_assigns)117 render_partial_collection(nil, collection, nil, local_assigns) 119 118 else 120 119 "" … … 127 126 def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}) #:nodoc: 128 127 return " " if collection.empty? 129 128 130 129 local_assigns = local_assigns ? local_assigns.clone : {} 130 spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : '' 131 132 if partial_path.nil? 133 render_partial_collection_with_unknown_partial_path(collection, local_assigns, spacer) 134 else 135 render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns, spacer) 136 end.join(spacer) 137 end 138 139 def render_partial_collection_with_known_partial_path(collection, partial_path, local_assigns, spacer) 131 140 template = ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns) 132 133 spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : ''134 135 141 collection.map do |element| 136 142 template.render_member(element) 137 end.join(spacer) 143 end 144 end 145 146 def render_partial_collection_with_unknown_partial_path(collection, local_assigns, spacer) 147 templates = Hash.new 148 i = 0 149 collection.map do |element| 150 partial_path = ActionController::RecordIdentifier.partial_path(element) 151 template = templates[partial_path] ||= ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns) 152 template.counter = i 153 i += 1 154 template.render_member(element) 155 end 138 156 end 139 157 end trunk/actionpack/test/controller/fake_models.rb
r7190 r9177 4 4 end 5 5 end 6 7 class BadCustomer < Customer 8 end 9 10 class GoodCustomer < Customer 11 end trunk/actionpack/test/controller/new_render_test.rb
r8976 r9177 162 162 def partial_collection_shorthand_with_locals 163 163 render :partial => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" } 164 end 165 166 def partial_collection_shorthand_with_different_types_of_records 167 render :partial => [ 168 BadCustomer.new("mark"), 169 GoodCustomer.new("craig"), 170 BadCustomer.new("john"), 171 GoodCustomer.new("zach"), 172 GoodCustomer.new("brandon"), 173 BadCustomer.new("dan") ], 174 :locals => { :greeting => "Bonjour" } 175 end 176 177 def partial_collection_shorthand_with_different_types_of_records_with_counter 178 partial_collection_shorthand_with_different_types_of_records 164 179 end 165 180 … … 742 757 end 743 758 759 def test_partial_collection_shorthand_with_different_types_of_records 760 get :partial_collection_shorthand_with_different_types_of_records 761 assert_equal "Bonjour bad customer: mark1Bonjour good customer: craig2Bonjour bad customer: john3Bonjour good customer: zach4Bonjour good customer: brandon5Bonjour bad customer: dan6", @response.body 762 end 763 744 764 def test_empty_partial_collection 745 765 get :empty_partial_collection