提交 a7ea06b4 编写于 作者: J Jacek Becela 提交者: Pratik Naik

Make render shorthands work with namespaced controllers

Signed-off-by: NPratik Naik <pratiknaik@gmail.com>
上级 6776edcc
......@@ -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:
......
......@@ -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
......@@ -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
......
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
......@@ -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
\ No newline at end of file
......@@ -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
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册