Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9177

Show
Ignore:
Timestamp:
04/01/08 00:50:09 (1 month ago)
Author:
bitsweat
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r9124 r9177  
    11*SVN* 
     2 
     3* Support render :partial => collection of heterogeneous elements.  #11491 [Zach Dennis] 
    24 
    35* Avoid remote_ip spoofing.  [Brian Candler] 
  • trunk/actionpack/lib/action_view/partial_template.rb

    r8976 r9177  
    88      super(view, @path, true, locals) 
    99      add_object_to_local_assigns!(object) 
    10        
     10 
    1111      # This is needed here in order to compile template with knowledge of 'counter' 
    1212      initialize_counter 
     
    2525      @locals[@counter_name] += 1 
    2626      @locals[:object] = @locals[@variable_name] = object 
    27       render 
     27      returning render do 
     28        @locals.delete(@variable_name) 
     29        @locals.delete(:object) 
     30      end 
    2831    end 
    2932     
     33    def counter=(num) 
     34      @locals[@counter_name] = num 
     35    end 
     36 
    3037    private 
    31      
     38 
    3239    def add_object_to_local_assigns!(object) 
    3340      @locals[:object] ||= 
  • trunk/actionpack/lib/action_view/partials.rb

    r8976 r9177  
    114114        when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Associations::HasManyThroughAssociation 
    115115          if partial_path.any? 
    116             path       = ActionController::RecordIdentifier.partial_path(partial_path.first) 
    117116            collection = partial_path 
    118             render_partial_collection(path, collection, nil, local_assigns) 
     117            render_partial_collection(nil, collection, nil, local_assigns) 
    119118          else 
    120119            "" 
     
    127126      def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}) #:nodoc: 
    128127        return " " if collection.empty? 
    129          
     128 
    130129        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) 
    131140        template = ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns) 
    132          
    133         spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : '' 
    134          
    135141        collection.map do |element| 
    136142          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 
    138156      end 
    139157  end 
  • trunk/actionpack/test/controller/fake_models.rb

    r7190 r9177  
    44  end 
    55end 
     6 
     7class BadCustomer < Customer 
     8end 
     9 
     10class GoodCustomer < Customer 
     11end 
  • trunk/actionpack/test/controller/new_render_test.rb

    r8976 r9177  
    162162  def partial_collection_shorthand_with_locals 
    163163    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 
    164179  end 
    165180 
     
    742757  end 
    743758 
     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 
    744764  def test_empty_partial_collection 
    745765    get :empty_partial_collection