1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Simplify path traversal logic

This commit is contained in:
José Valim 2011-12-09 06:25:10 +01:00
parent 1d9de9d758
commit 2995134cba

View file

@ -375,23 +375,23 @@ module ActionView
end end
end end
@partial_names[path] ||= path.dup.tap do |object_path| @partial_names[path] ||= merge_prefix_into_object_path(@context_prefix, path.dup)
merge_prefix_into_object_path(@context_prefix, object_path)
end
end end
def merge_prefix_into_object_path(prefix, object_path) def merge_prefix_into_object_path(prefix, object_path)
if prefix.include?(?/) && object_path.include?(?/) if prefix.include?(?/) && object_path.include?(?/)
overlap = [] prefixes = []
prefix_array = File.dirname(prefix).split('/') prefix_array = File.dirname(prefix).split('/')
object_path_array = object_path.split('/')[0..-3] # skip model dir & partial object_path_array = object_path.split('/')[0..-3] # skip model dir & partial
prefix_array.each_with_index do |dir, index| prefix_array.each_with_index do |dir, index|
overlap << dir if dir == object_path_array[index] break if dir == object_path_array[index]
prefixes << dir
end end
object_path.gsub!(/^#{overlap.join('/')}\//,'') (prefixes << object_path).join("/")
object_path.insert(0, "#{File.dirname(prefix)}/") else
object_path
end end
end end