mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make render shorthands work with namespaced controllers
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
6776edccf6
commit
a7ea06b4eb
4 changed files with 195 additions and 48 deletions
|
@ -33,11 +33,17 @@ module ActionController
|
|||
|
||||
# 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 @@ module ActionView
|
|||
""
|
||||
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 @@ module ActionView
|
|||
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
|
||||
|
|
|
@ -1,49 +1,49 @@
|
|||
require 'active_record_unit'
|
||||
|
||||
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
|
||||
end
|
||||
RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
end
|
||||
RenderPartialWithRecordIdentificationController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
|
||||
|
||||
def setup
|
||||
@controller = RenderPartialWithRecordIdentificationController.new
|
||||
|
@ -84,3 +84,108 @@ class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
|
|||
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
|
|
@ -57,6 +57,18 @@ class RecordIdentifierTest < Test::Unit::TestCase
|
|||
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 @@ class NestedRecordIdentifierTest < RecordIdentifierTest
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue