diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/record_identifier.rb | 15 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/partials.rb | 25 | ||||
-rw-r--r-- | actionpack/test/controller/record_identifier_test.rb | 57 |
3 files changed, 19 insertions, 78 deletions
diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index b4408e4e1d..1165c3b7c5 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -36,21 +36,6 @@ module ActionController JOIN = '_'.freeze NEW = 'new'.freeze - # Returns plural/singular for a record or class. Example: - # - # 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) - name = model_name_from_record_or_class(record_or_class) - - if controller_path && controller_path.include?("/") - "#{File.dirname(controller_path)}/#{name.partial_path}" - else - name.partial_path - end - end - # The DOM class convention is to use the singular form of an object or class. Examples: # # dom_class(post) # => "post" diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 581742e875..6e5810d148 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -177,6 +177,12 @@ module ActionView attr_accessor :_partial end + module ClassMethods + def _partial_names + @_partial_names ||= ActiveSupport::ConcurrentHash.new + end + end + def render_partial(*args) @assigns_added = false _render_partial(*args) @@ -189,12 +195,9 @@ module ActionView if partial.respond_to?(:to_ary) return _render_partial_collection(partial, options) - elsif partial.is_a?(ActionView::Helpers::FormBuilder) - path = partial.class.model_name.partial_path - options[:locals].merge!(path.to_sym => partial) elsif !partial.is_a?(String) options[:object] = object = partial - path = ActionController::RecordIdentifier.partial_path(object, controller_path) + path = _partial_path(object) end parts = partial_parts(path, options) @@ -203,6 +206,17 @@ module ActionView end private + def _partial_path(object) + self.class._partial_names[[controller.class, object.class]] ||= begin + name = object.class.model_name + if controller_path && controller_path.include?("/") + File.join(File.dirname(controller_path), name.partial_path) + else + name.partial_path + end + end + end + def partial_parts(name, options) segments = name.split("/") parts = segments.pop.split(".") @@ -278,8 +292,7 @@ module ActionView index, @_partial_path = 0, nil collection.map do |object| options[:_template] = template = passed_template || begin - _partial_path = - ActionController::RecordIdentifier.partial_path(object, controller_path) + _partial_path = _partial_path(object) template = _pick_partial_template(_partial_path) end diff --git a/actionpack/test/controller/record_identifier_test.rb b/actionpack/test/controller/record_identifier_test.rb index 44e49ed3f8..6b6d154faa 100644 --- a/actionpack/test/controller/record_identifier_test.rb +++ b/actionpack/test/controller/record_identifier_test.rb @@ -54,24 +54,6 @@ class RecordIdentifierTest < Test::Unit::TestCase assert_equal "edit_#{@singular}_1", dom_id(@record, :edit) end - def test_partial_path - expected = "#{@plural}/#{@singular}" - assert_equal expected, partial_path(@record) - 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 @@ -101,42 +83,3 @@ class RecordIdentifierTest < Test::Unit::TestCase RecordIdentifier.send(method, *args) end end - -class NestedRecordIdentifierTest < RecordIdentifierTest - def setup - @klass = Comment::Nested - @record = @klass.new - @singular = 'comment_nested' - @plural = 'comment_nesteds' - end - - def test_partial_path - expected = "comment/nesteds/nested" - 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 |