Navigation Menu

Skip to content

Commit

Permalink
Make render shorthands work with namespaced controllers
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
ncr authored and lifo committed May 10, 2008
1 parent 6776edc commit a7ea06b
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 48 deletions.
14 changes: 10 additions & 4 deletions actionpack/lib/action_controller/record_identifier.rb
Expand Up @@ -33,11 +33,17 @@ module RecordIdentifier

# Returns plural/singular for a record or class. Example:
#
# partial_path(post) # => "posts/post"
# partial_path(Person) # => "people/person"
def partial_path(record_or_class)
# partial_path(post) # => "posts/post"
# partial_path(Person) # => "people/person"
# partial_path(Person, "admin/games") # => "admin/people/person"
def partial_path(record_or_class, controller_path = nil)
klass = class_from_record_or_class(record_or_class)
"#{klass.name.tableize}/#{klass.name.demodulize.underscore}"

if controller_path && controller_path.include?("/")
"#{File.dirname(controller_path)}/#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
else
"#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
end
end

# The DOM class convention is to use the singular form of an object or class. Examples:
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/partials.rb
Expand Up @@ -119,7 +119,7 @@ def render_partial(partial_path, object_assigns = nil, local_assigns = {}) #:nod
""
end
else
render_partial(ActionController::RecordIdentifier.partial_path(partial_path), partial_path, local_assigns)
render_partial(ActionController::RecordIdentifier.partial_path(partial_path, controller.class.controller_path), partial_path, local_assigns)
end
end

Expand Down Expand Up @@ -147,7 +147,7 @@ def render_partial_collection_with_unknown_partial_path(collection, local_assign
templates = Hash.new
i = 0
collection.map do |element|
partial_path = ActionController::RecordIdentifier.partial_path(element)
partial_path = ActionController::RecordIdentifier.partial_path(element, controller.class.controller_path)
template = templates[partial_path] ||= ActionView::PartialTemplate.new(self, partial_path, nil, local_assigns)
template.counter = i
i += 1
Expand Down
@@ -1,49 +1,49 @@
require 'active_record_unit'

class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots
class RenderPartialWithRecordIdentificationController < ActionController::Base
def render_with_has_many_and_belongs_to_association
@developer = Developer.find(1)
render :partial => @developer.projects
end

class RenderPartialWithRecordIdentificationController < ActionController::Base
def render_with_has_many_and_belongs_to_association
@developer = Developer.find(1)
render :partial => @developer.projects
end

def render_with_has_many_association
@topic = Topic.find(1)
render :partial => @topic.replies
end

def render_with_named_scope
render :partial => Reply.base
end

def render_with_has_many_through_association
@developer = Developer.find(:first)
render :partial => @developer.topics
end

def render_with_has_one_association
@company = Company.find(1)
render :partial => @company.mascot
end

def render_with_belongs_to_association
@reply = Reply.find(1)
render :partial => @reply.topic
end

def render_with_record
@developer = Developer.find(:first)
render :partial => @developer
end

def render_with_record_collection
@developers = Developer.find(:all)
render :partial => @developers
end
def render_with_has_many_association
@topic = Topic.find(1)
render :partial => @topic.replies
end

def render_with_named_scope
render :partial => Reply.base
end

def render_with_has_many_through_association
@developer = Developer.find(:first)
render :partial => @developer.topics
end

def render_with_has_one_association
@company = Company.find(1)
render :partial => @company.mascot
end

def render_with_belongs_to_association
@reply = Reply.find(1)
render :partial => @reply.topic
end
RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]

def render_with_record
@developer = Developer.find(:first)
render :partial => @developer
end

def render_with_record_collection
@developers = Developer.find(:all)
render :partial => @developers
end
end
RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]

class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots

def setup
@controller = RenderPartialWithRecordIdentificationController.new
Expand Down Expand Up @@ -84,3 +84,108 @@ def test_rendering_partial_with_has_one_association
assert_equal mascot.name, @response.body
end
end

class RenderPartialWithRecordIdentificationController < ActionController::Base
def render_with_has_many_and_belongs_to_association
@developer = Developer.find(1)
render :partial => @developer.projects
end

def render_with_has_many_association
@topic = Topic.find(1)
render :partial => @topic.replies
end

def render_with_has_many_through_association
@developer = Developer.find(:first)
render :partial => @developer.topics
end

def render_with_belongs_to_association
@reply = Reply.find(1)
render :partial => @reply.topic
end

def render_with_record
@developer = Developer.find(:first)
render :partial => @developer
end

def render_with_record_collection
@developers = Developer.find(:all)
render :partial => @developers
end
end
RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]

class Game < Struct.new(:name, :id)
def to_param
id.to_s
end
end

module Fun
class NestedController < ActionController::Base
def render_with_record_in_nested_controller
render :partial => Game.new("Pong")
end

def render_with_record_collection_in_nested_controller
render :partial => [ Game.new("Pong"), Game.new("Tank") ]
end
end
NestedController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]

module Serious
class NestedDeeperController < ActionController::Base
def render_with_record_in_deeper_nested_controller
render :partial => Game.new("Chess")
end

def render_with_record_collection_in_deeper_nested_controller
render :partial => [ Game.new("Chess"), Game.new("Sudoku"), Game.new("Solitaire") ]
end
end
NestedDeeperController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
end
end

class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveRecordTestCase
def setup
@controller = Fun::NestedController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
super
end

def test_render_with_record_in_nested_controller
get :render_with_record_in_nested_controller
assert_template 'fun/games/_game'
end

def test_render_with_record_collection_in_nested_controller
get :render_with_record_collection_in_nested_controller
assert_template 'fun/games/_game'
end

end

class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < ActiveRecordTestCase
def setup
@controller = Fun::Serious::NestedDeeperController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
super
end

def test_render_with_record_in_deeper_nested_controller
get :render_with_record_in_deeper_nested_controller
assert_template 'fun/serious/games/_game'
end

def test_render_with_record_collection_in_deeper_nested_controller
get :render_with_record_collection_in_deeper_nested_controller
assert_template 'fun/serious/games/_game'
end

end
36 changes: 36 additions & 0 deletions actionpack/test/controller/record_identifier_test.rb
Expand Up @@ -57,6 +57,18 @@ def test_partial_path
assert_equal expected, partial_path(Comment)
end

def test_partial_path_with_namespaced_controller_path
expected = "admin/#{@plural}/#{@singular}"
assert_equal expected, partial_path(@record, "admin/posts")
assert_equal expected, partial_path(@klass, "admin/posts")
end

def test_partial_path_with_not_namespaced_controller_path
expected = "#{@plural}/#{@singular}"
assert_equal expected, partial_path(@record, "posts")
assert_equal expected, partial_path(@klass, "posts")
end

def test_dom_class
assert_equal @singular, dom_class(@record)
end
Expand Down Expand Up @@ -100,4 +112,28 @@ def test_partial_path
assert_equal expected, partial_path(@record)
assert_equal expected, partial_path(Comment::Nested)
end

def test_partial_path_with_namespaced_controller_path
expected = "admin/comment/nesteds/nested"
assert_equal expected, partial_path(@record, "admin/posts")
assert_equal expected, partial_path(@klass, "admin/posts")
end

def test_partial_path_with_deeper_namespaced_controller_path
expected = "deeper/admin/comment/nesteds/nested"
assert_equal expected, partial_path(@record, "deeper/admin/posts")
assert_equal expected, partial_path(@klass, "deeper/admin/posts")
end

def test_partial_path_with_even_deeper_namespaced_controller_path
expected = "even/more/deeper/admin/comment/nesteds/nested"
assert_equal expected, partial_path(@record, "even/more/deeper/admin/posts")
assert_equal expected, partial_path(@klass, "even/more/deeper/admin/posts")
end

def test_partial_path_with_not_namespaced_controller_path
expected = "comment/nesteds/nested"
assert_equal expected, partial_path(@record, "posts")
assert_equal expected, partial_path(@klass, "posts")
end
end

0 comments on commit a7ea06b

Please sign in to comment.