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

Ticket #9742: render_shorthand_with_namespaced_controllers_patch.diff

File render_shorthand_with_namespaced_controllers_patch.diff, 10.8 kB (added by ncr, 2 months ago)

Ensured that patch applies and tests pass in edge (bcb090c) (May 8th 2008)

  • a/actionpack/lib/action_controller/record_identifier.rb

    old new  
    3333 
    3434    # Returns plural/singular for a record or class. Example: 
    3535    # 
    36     #   partial_path(post)   # => "posts/post" 
    37     #   partial_path(Person) # => "people/person" 
    38     def partial_path(record_or_class) 
     36    #   partial_path(post)                   # => "posts/post" 
     37    #   partial_path(Person)                 # => "people/person" 
     38    #   partial_path(Person, "admin/games")  # => "admin/people/person"  
     39    def partial_path(record_or_class, controller_path = nil) 
    3940      klass = class_from_record_or_class(record_or_class) 
    40       "#{klass.name.tableize}/#{klass.name.demodulize.underscore}" 
     41       
     42      if controller_path && controller_path.include?("/") 
     43        "#{File.dirname(controller_path)}/#{klass.name.tableize}/#{klass.name.demodulize.underscore}" 
     44      else 
     45        "#{klass.name.tableize}/#{klass.name.demodulize.underscore}" 
     46      end 
    4147    end 
    4248 
    4349    # The DOM class convention is to use the singular form of an object or class. Examples: 
  • a/actionpack/lib/action_view/partials.rb

    old new  
    119119            "" 
    120120          end 
    121121        else 
    122           render_partial(ActionController::RecordIdentifier.partial_path(partial_path), partial_path, local_assigns) 
     122          render_partial(ActionController::RecordIdentifier.partial_path(partial_path, controller.class.controller_path), partial_path, local_assigns) 
    123123        end 
    124124      end 
    125125 
     
    147147        templates = Hash.new 
    148148        i = 0 
    149149        collection.map do |element| 
    150           partial_path = ActionController::RecordIdentifier.partial_path(element
     150          partial_path = ActionController::RecordIdentifier.partial_path(element, controller.class.controller_path
    151151          template = templates[partial_path] ||= ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns) 
    152152          template.counter = i 
    153153          i += 1 
  • a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb

    old new  
    11require 'active_record_unit' 
    22 
     3class RenderPartialWithRecordIdentificationController < ActionController::Base 
     4  def render_with_has_many_and_belongs_to_association 
     5    @developer = Developer.find(1) 
     6    render :partial => @developer.projects 
     7  end 
     8   
     9  def render_with_has_many_association 
     10    @topic = Topic.find(1) 
     11    render :partial => @topic.replies 
     12  end 
     13   
     14  def render_with_named_scope 
     15    render :partial => Reply.base 
     16  end 
     17   
     18  def render_with_has_many_through_association 
     19    @developer = Developer.find(:first) 
     20    render :partial => @developer.topics 
     21  end 
     22   
     23  def render_with_has_one_association 
     24    @company = Company.find(1) 
     25    render :partial => @company.mascot 
     26  end 
     27   
     28  def render_with_belongs_to_association 
     29    @reply = Reply.find(1) 
     30    render :partial => @reply.topic 
     31  end 
     32   
     33  def render_with_record 
     34    @developer = Developer.find(:first) 
     35    render :partial => @developer 
     36  end 
     37   
     38  def render_with_record_collection 
     39    @developers = Developer.find(:all) 
     40    render :partial => @developers 
     41  end 
     42end 
     43RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] 
     44 
    345class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase 
    446  fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots 
    5  
    6   class RenderPartialWithRecordIdentificationController < ActionController::Base 
    7     def render_with_has_many_and_belongs_to_association 
    8       @developer = Developer.find(1) 
    9       render :partial => @developer.projects 
    10     end 
    11      
    12     def render_with_has_many_association 
    13       @topic = Topic.find(1) 
    14       render :partial => @topic.replies 
    15     end 
    16      
    17     def render_with_named_scope 
    18       render :partial => Reply.base 
    19     end 
    20      
    21     def render_with_has_many_through_association 
    22       @developer = Developer.find(:first) 
    23       render :partial => @developer.topics 
    24     end 
    25      
    26     def render_with_has_one_association 
    27       @company = Company.find(1) 
    28       render :partial => @company.mascot 
    29     end 
    30      
    31     def render_with_belongs_to_association 
    32       @reply = Reply.find(1) 
    33       render :partial => @reply.topic 
    34     end 
    35      
    36     def render_with_record 
    37       @developer = Developer.find(:first) 
    38       render :partial => @developer 
    39     end 
    40      
    41     def render_with_record_collection 
    42       @developers = Developer.find(:all) 
    43       render :partial => @developers 
    44     end 
    45   end 
    46   RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] 
    4747   
    4848  def setup 
    4949    @controller = RenderPartialWithRecordIdentificationController.new 
     
    8484    assert_equal mascot.name, @response.body 
    8585  end 
    8686end 
     87 
     88class RenderPartialWithRecordIdentificationController < ActionController::Base 
     89  def render_with_has_many_and_belongs_to_association 
     90    @developer = Developer.find(1) 
     91    render :partial => @developer.projects 
     92  end 
     93   
     94  def render_with_has_many_association 
     95    @topic = Topic.find(1) 
     96    render :partial => @topic.replies 
     97  end 
     98   
     99  def render_with_has_many_through_association 
     100    @developer = Developer.find(:first) 
     101    render :partial => @developer.topics 
     102  end 
     103   
     104  def render_with_belongs_to_association 
     105    @reply = Reply.find(1) 
     106    render :partial => @reply.topic 
     107  end 
     108   
     109  def render_with_record 
     110    @developer = Developer.find(:first) 
     111    render :partial => @developer 
     112  end 
     113   
     114  def render_with_record_collection 
     115    @developers = Developer.find(:all) 
     116    render :partial => @developers 
     117  end 
     118end 
     119RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] 
     120 
     121class Game < Struct.new(:name, :id) 
     122  def to_param 
     123    id.to_s 
     124  end 
     125end 
     126 
     127module Fun 
     128  class NestedController < ActionController::Base 
     129    def render_with_record_in_nested_controller 
     130      render :partial => Game.new("Pong") 
     131    end 
     132 
     133    def render_with_record_collection_in_nested_controller 
     134      render :partial => [ Game.new("Pong"), Game.new("Tank") ] 
     135    end 
     136  end 
     137  NestedController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] 
     138   
     139  module Serious 
     140    class NestedDeeperController < ActionController::Base 
     141      def render_with_record_in_deeper_nested_controller 
     142        render :partial => Game.new("Chess") 
     143      end 
     144       
     145      def render_with_record_collection_in_deeper_nested_controller 
     146        render :partial => [ Game.new("Chess"), Game.new("Sudoku"), Game.new("Solitaire") ] 
     147      end 
     148    end 
     149    NestedDeeperController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] 
     150  end 
     151end 
     152 
     153class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveRecordTestCase 
     154  def setup 
     155    @controller = Fun::NestedController.new 
     156    @request    = ActionController::TestRequest.new 
     157    @response   = ActionController::TestResponse.new 
     158    super 
     159  end 
     160 
     161  def test_render_with_record_in_nested_controller 
     162    get :render_with_record_in_nested_controller 
     163    assert_template 'fun/games/_game' 
     164  end 
     165 
     166  def test_render_with_record_collection_in_nested_controller 
     167    get :render_with_record_collection_in_nested_controller 
     168    assert_template 'fun/games/_game' 
     169  end 
     170     
     171end 
     172 
     173class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < ActiveRecordTestCase 
     174  def setup 
     175    @controller = Fun::Serious::NestedDeeperController.new 
     176    @request    = ActionController::TestRequest.new 
     177    @response   = ActionController::TestResponse.new 
     178    super 
     179  end 
     180 
     181  def test_render_with_record_in_deeper_nested_controller 
     182    get :render_with_record_in_deeper_nested_controller 
     183    assert_template 'fun/serious/games/_game' 
     184  end 
     185 
     186  def test_render_with_record_collection_in_deeper_nested_controller 
     187    get :render_with_record_collection_in_deeper_nested_controller 
     188    assert_template 'fun/serious/games/_game' 
     189  end 
     190   
     191end 
  • a/actionpack/test/controller/record_identifier_test.rb

    old new  
    5656    assert_equal expected, partial_path(@record) 
    5757    assert_equal expected, partial_path(Comment) 
    5858  end 
     59   
     60  def test_partial_path_with_namespaced_controller_path 
     61    expected = "admin/#{@plural}/#{@singular}" 
     62    assert_equal expected, partial_path(@record, "admin/posts") 
     63    assert_equal expected, partial_path(@klass, "admin/posts") 
     64  end 
     65   
     66  def test_partial_path_with_not_namespaced_controller_path 
     67    expected = "#{@plural}/#{@singular}" 
     68    assert_equal expected, partial_path(@record, "posts") 
     69    assert_equal expected, partial_path(@klass, "posts") 
     70  end 
    5971 
    6072  def test_dom_class 
    6173    assert_equal @singular, dom_class(@record) 
     
    100112    assert_equal expected, partial_path(@record) 
    101113    assert_equal expected, partial_path(Comment::Nested) 
    102114  end 
     115   
     116  def test_partial_path_with_namespaced_controller_path 
     117    expected = "admin/comment/nesteds/nested" 
     118    assert_equal expected, partial_path(@record, "admin/posts") 
     119    assert_equal expected, partial_path(@klass, "admin/posts") 
     120  end 
     121 
     122  def test_partial_path_with_deeper_namespaced_controller_path 
     123    expected = "deeper/admin/comment/nesteds/nested" 
     124    assert_equal expected, partial_path(@record, "deeper/admin/posts") 
     125    assert_equal expected, partial_path(@klass, "deeper/admin/posts") 
     126  end 
     127 
     128  def test_partial_path_with_even_deeper_namespaced_controller_path 
     129    expected = "even/more/deeper/admin/comment/nesteds/nested" 
     130    assert_equal expected, partial_path(@record, "even/more/deeper/admin/posts") 
     131    assert_equal expected, partial_path(@klass, "even/more/deeper/admin/posts") 
     132  end 
     133   
     134  def test_partial_path_with_not_namespaced_controller_path 
     135    expected = "comment/nesteds/nested" 
     136    assert_equal expected, partial_path(@record, "posts") 
     137    assert_equal expected, partial_path(@klass, "posts") 
     138  end 
    103139end