Permalink
Browse files

Clean up render @object a bit more.

  • Loading branch information...
1 parent 4ac9d39 commit 70a440aa277676078e3b8baafe1101d1287e114f @wycats wycats committed Aug 7, 2009
@@ -36,21 +36,6 @@ module RecordIdentifier
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"
@@ -177,6 +177,12 @@ module Partials
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 @@ def _render_partial(options = {}) #:nodoc:
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 @@ def _render_partial(options = {}) #:nodoc:
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 @@ def _render_partial_collection(collection, options = {}, passed_template = nil)
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
@@ -54,24 +54,6 @@ def test_dom_id_with_prefix
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 @@ def method_missing(method, *args)
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

0 comments on commit 70a440a

Please sign in to comment.