public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make render shorthands work with namespaced controllers

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Jacek Becela (author)
Thu May 08 04:47:24 -0700 2008
lifo (committer)
Sat May 10 03:28:19 -0700 2008
commit  a7ea06b4ebe252e258f83e7de945b4baa30ec3bc
tree    8c4022c3395dd485681cb40407ed3cdd7414c646
parent  6776edccf6fb553eb0ac6db55e1d30df1b5b6589
...
33
34
35
36
37
38
 
 
 
 
39
40
 
 
 
 
 
 
41
42
43
...
33
34
35
 
 
 
36
37
38
39
40
 
41
42
43
44
45
46
47
48
49
0
@@ -33,11 +33,17 @@ module ActionController
0
 
0
     # Returns plural/singular for a record or class. Example:
0
     #
0
-    #   partial_path(post)   # => "posts/post"
0
-    #   partial_path(Person) # => "people/person"
0
-    def partial_path(record_or_class)
0
+    #   partial_path(post)                   # => "posts/post"
0
+    #   partial_path(Person)                 # => "people/person"
0
+    #   partial_path(Person, "admin/games")  # => "admin/people/person"
0
+    def partial_path(record_or_class, controller_path = nil)
0
       klass = class_from_record_or_class(record_or_class)
0
-      "#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
0
+
0
+      if controller_path && controller_path.include?("/")
0
+        "#{File.dirname(controller_path)}/#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
0
+      else
0
+        "#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
0
+      end
0
     end
0
 
0
     # The DOM class convention is to use the singular form of an object or class. Examples:
...
119
120
121
122
 
123
124
125
...
147
148
149
150
 
151
152
153
...
119
120
121
 
122
123
124
125
...
147
148
149
 
150
151
152
153
0
@@ -119,7 +119,7 @@ module ActionView
0
             ""
0
           end
0
         else
0
-          render_partial(ActionController::RecordIdentifier.partial_path(partial_path), partial_path, local_assigns)
0
+          render_partial(ActionController::RecordIdentifier.partial_path(partial_path, controller.class.controller_path), partial_path, local_assigns)
0
         end
0
       end
0
 
0
@@ -147,7 +147,7 @@ module ActionView
0
         templates = Hash.new
0
         i = 0
0
         collection.map do |element|
0
-          partial_path = ActionController::RecordIdentifier.partial_path(element)
0
+          partial_path = ActionController::RecordIdentifier.partial_path(element, controller.class.controller_path)
0
           template = templates[partial_path] ||= ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns)
0
           template.counter = i
0
           i += 1
...
1
2
3
4
 
 
 
 
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
48
49
...
84
85
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
...
1
2
 
 
3
4
5
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
0
@@ -1,49 +1,49 @@
0
 require 'active_record_unit'
0
 
0
-class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
0
-  fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots
0
+class RenderPartialWithRecordIdentificationController < ActionController::Base
0
+  def render_with_has_many_and_belongs_to_association
0
+    @developer = Developer.find(1)
0
+    render :partial => @developer.projects
0
+  end
0
 
0
-  class RenderPartialWithRecordIdentificationController < ActionController::Base
0
-    def render_with_has_many_and_belongs_to_association
0
-      @developer = Developer.find(1)
0
-      render :partial => @developer.projects
0
-    end
0
-    
0
-    def render_with_has_many_association
0
-      @topic = Topic.find(1)
0
-      render :partial => @topic.replies
0
-    end
0
-    
0
-    def render_with_named_scope
0
-      render :partial => Reply.base
0
-    end
0
-    
0
-    def render_with_has_many_through_association
0
-      @developer = Developer.find(:first)
0
-      render :partial => @developer.topics
0
-    end
0
-    
0
-    def render_with_has_one_association
0
-      @company = Company.find(1)
0
-      render :partial => @company.mascot
0
-    end
0
-    
0
-    def render_with_belongs_to_association
0
-      @reply = Reply.find(1)
0
-      render :partial => @reply.topic
0
-    end
0
-    
0
-    def render_with_record
0
-      @developer = Developer.find(:first)
0
-      render :partial => @developer
0
-    end
0
-    
0
-    def render_with_record_collection
0
-      @developers = Developer.find(:all)
0
-      render :partial => @developers
0
-    end
0
+  def render_with_has_many_association
0
+    @topic = Topic.find(1)
0
+    render :partial => @topic.replies
0
+  end
0
+
0
+  def render_with_named_scope
0
+    render :partial => Reply.base
0
+  end
0
+
0
+  def render_with_has_many_through_association
0
+    @developer = Developer.find(:first)
0
+    render :partial => @developer.topics
0
+  end
0
+
0
+  def render_with_has_one_association
0
+    @company = Company.find(1)
0
+    render :partial => @company.mascot
0
+  end
0
+
0
+  def render_with_belongs_to_association
0
+    @reply = Reply.find(1)
0
+    render :partial => @reply.topic
0
   end
0
-  RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
0
+
0
+  def render_with_record
0
+    @developer = Developer.find(:first)
0
+    render :partial => @developer
0
+  end
0
+
0
+  def render_with_record_collection
0
+    @developers = Developer.find(:all)
0
+    render :partial => @developers
0
+  end
0
+end
0
+RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
0
+
0
+class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
0
+  fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots
0
   
0
   def setup
0
     @controller = RenderPartialWithRecordIdentificationController.new
0
@@ -84,3 +84,108 @@ class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
0
     assert_equal mascot.name, @response.body
0
   end
0
 end
0
+
0
+class RenderPartialWithRecordIdentificationController < ActionController::Base
0
+  def render_with_has_many_and_belongs_to_association
0
+    @developer = Developer.find(1)
0
+    render :partial => @developer.projects
0
+  end
0
+
0
+  def render_with_has_many_association
0
+    @topic = Topic.find(1)
0
+    render :partial => @topic.replies
0
+  end
0
+
0
+  def render_with_has_many_through_association
0
+    @developer = Developer.find(:first)
0
+    render :partial => @developer.topics
0
+  end
0
+
0
+  def render_with_belongs_to_association
0
+    @reply = Reply.find(1)
0
+    render :partial => @reply.topic
0
+  end
0
+
0
+  def render_with_record
0
+    @developer = Developer.find(:first)
0
+    render :partial => @developer
0
+  end
0
+
0
+  def render_with_record_collection
0
+    @developers = Developer.find(:all)
0
+    render :partial => @developers
0
+  end
0
+end
0
+RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
0
+
0
+class Game < Struct.new(:name, :id)
0
+  def to_param
0
+    id.to_s
0
+  end
0
+end
0
+
0
+module Fun
0
+  class NestedController < ActionController::Base
0
+    def render_with_record_in_nested_controller
0
+      render :partial => Game.new("Pong")
0
+    end
0
+
0
+    def render_with_record_collection_in_nested_controller
0
+      render :partial => [ Game.new("Pong"), Game.new("Tank") ]
0
+    end
0
+  end
0
+  NestedController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
0
+
0
+  module Serious
0
+    class NestedDeeperController < ActionController::Base
0
+      def render_with_record_in_deeper_nested_controller
0
+        render :partial => Game.new("Chess")
0
+      end
0
+
0
+      def render_with_record_collection_in_deeper_nested_controller
0
+        render :partial => [ Game.new("Chess"), Game.new("Sudoku"), Game.new("Solitaire") ]
0
+      end
0
+    end
0
+    NestedDeeperController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
0
+  end
0
+end
0
+
0
+class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveRecordTestCase
0
+  def setup
0
+    @controller = Fun::NestedController.new
0
+    @request    = ActionController::TestRequest.new
0
+    @response   = ActionController::TestResponse.new
0
+    super
0
+  end
0
+
0
+  def test_render_with_record_in_nested_controller
0
+    get :render_with_record_in_nested_controller
0
+    assert_template 'fun/games/_game'
0
+  end
0
+
0
+  def test_render_with_record_collection_in_nested_controller
0
+    get :render_with_record_collection_in_nested_controller
0
+    assert_template 'fun/games/_game'
0
+  end
0
+
0
+end
0
+
0
+class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < ActiveRecordTestCase
0
+  def setup
0
+    @controller = Fun::Serious::NestedDeeperController.new
0
+    @request    = ActionController::TestRequest.new
0
+    @response   = ActionController::TestResponse.new
0
+    super
0
+  end
0
+
0
+  def test_render_with_record_in_deeper_nested_controller
0
+    get :render_with_record_in_deeper_nested_controller
0
+    assert_template 'fun/serious/games/_game'
0
+  end
0
+
0
+  def test_render_with_record_collection_in_deeper_nested_controller
0
+    get :render_with_record_collection_in_deeper_nested_controller
0
+    assert_template 'fun/serious/games/_game'
0
+  end
0
+
0
+end
0
\ No newline at end of file
...
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
60
61
62
...
100
101
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
0
@@ -57,6 +57,18 @@ class RecordIdentifierTest < Test::Unit::TestCase
0
     assert_equal expected, partial_path(Comment)
0
   end
0
 
0
+  def test_partial_path_with_namespaced_controller_path
0
+    expected = "admin/#{@plural}/#{@singular}"
0
+    assert_equal expected, partial_path(@record, "admin/posts")
0
+    assert_equal expected, partial_path(@klass, "admin/posts")
0
+  end
0
+
0
+  def test_partial_path_with_not_namespaced_controller_path
0
+    expected = "#{@plural}/#{@singular}"
0
+    assert_equal expected, partial_path(@record, "posts")
0
+    assert_equal expected, partial_path(@klass, "posts")
0
+  end
0
+
0
   def test_dom_class
0
     assert_equal @singular, dom_class(@record)
0
   end
0
@@ -100,4 +112,28 @@ class NestedRecordIdentifierTest < RecordIdentifierTest
0
     assert_equal expected, partial_path(@record)
0
     assert_equal expected, partial_path(Comment::Nested)
0
   end
0
+
0
+  def test_partial_path_with_namespaced_controller_path
0
+    expected = "admin/comment/nesteds/nested"
0
+    assert_equal expected, partial_path(@record, "admin/posts")
0
+    assert_equal expected, partial_path(@klass, "admin/posts")
0
+  end
0
+
0
+  def test_partial_path_with_deeper_namespaced_controller_path
0
+    expected = "deeper/admin/comment/nesteds/nested"
0
+    assert_equal expected, partial_path(@record, "deeper/admin/posts")
0
+    assert_equal expected, partial_path(@klass, "deeper/admin/posts")
0
+  end
0
+
0
+  def test_partial_path_with_even_deeper_namespaced_controller_path
0
+    expected = "even/more/deeper/admin/comment/nesteds/nested"
0
+    assert_equal expected, partial_path(@record, "even/more/deeper/admin/posts")
0
+    assert_equal expected, partial_path(@klass, "even/more/deeper/admin/posts")
0
+  end
0
+
0
+  def test_partial_path_with_not_namespaced_controller_path
0
+    expected = "comment/nesteds/nested"
0
+    assert_equal expected, partial_path(@record, "posts")
0
+    assert_equal expected, partial_path(@klass, "posts")
0
+  end
0
 end

Comments