diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2016-02-12 17:04:56 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2016-02-16 15:31:31 -0800 |
commit | 687109abba6a17c31845f79bffa7dbc429afb5e5 (patch) | |
tree | eedb4f1bff2534cc78f20bf51250f39e3ee0075a /actionview | |
parent | 5971fddb7997231f3431c26ba17471ba52704852 (diff) | |
download | rails-687109abba6a17c31845f79bffa7dbc429afb5e5.tar.gz rails-687109abba6a17c31845f79bffa7dbc429afb5e5.tar.bz2 rails-687109abba6a17c31845f79bffa7dbc429afb5e5.zip |
introduce a `tree` factory method for creating the dep tree
also add an EMPTY sentinel node
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/lib/action_view/digestor.rb | 55 | ||||
-rw-r--r-- | actionview/test/template/digestor_test.rb | 7 |
2 files changed, 62 insertions, 0 deletions
diff --git a/actionview/lib/action_view/digestor.rb b/actionview/lib/action_view/digestor.rb index 657026fa14..d31154061c 100644 --- a/actionview/lib/action_view/digestor.rb +++ b/actionview/lib/action_view/digestor.rb @@ -55,6 +55,55 @@ module ActionView end end + EMPTY = Class.new { + def name; 'missing'; end + def digest; ''; end + }.new + + def self.tree(name, finder, partial = false, seen = {}) + if obj = seen[name] + obj + else + logical_name = name.gsub(%r|/_|, "/") + template = finder.disable_cache { finder.find(logical_name, [], partial) } + node = seen[name] = Node.new(name, logical_name, template, partial, []) + deps = DependencyTracker.find_dependencies(name, template, finder.view_paths) + deps.each do |dep_file| + l_name = dep_file.gsub(%r|/_|, "/") + if finder.disable_cache { finder.exists?(l_name, [], true) } + node.children << tree(dep_file, finder, true, seen) + else + node.children << Missing.new(dep_file, l_name, nil, true, []) + end + end + node + end + end + + class Node < Struct.new(:name, :logical_name, :template, :partial, :children) + def to_dep(finder) + if partial + PartialDigestor.new(name, finder, partial: partial) + else + Digestor.new(name, finder, partial: partial) + end + end + + def digest + Digest::MD5.hexdigest("#{template.source}-#{dependency_digest}") + end + + def dependency_digest + children.map(&:digest).join("-") + end + end + + class Missing < Node + def digest + '' + end + end + attr_reader :name, :finder, :options def initialize(name, finder, options = {}) @@ -78,6 +127,12 @@ module ActionView [] end + def children + dependencies.collect do |dependency| + PartialDigestor.new(dependency, finder) + end + end + def nested_dependencies dependencies.collect do |dependency| dependencies = PartialDigestor.new(dependency, finder).nested_dependencies diff --git a/actionview/test/template/digestor_test.rb b/actionview/test/template/digestor_test.rb index bfab97cf1e..5e553859f0 100644 --- a/actionview/test/template/digestor_test.rb +++ b/actionview/test/template/digestor_test.rb @@ -36,6 +36,13 @@ class TemplateDigestorTest < ActionView::TestCase ActionView::Digestor.cache.clear end + def test_amaze + node = ActionView::Digestor.tree "messages/show", finder + x = ActionView::Digestor.new("messages/show", finder) + #assert_equal digest("messages/show"), node.to_dep(finder).digest + assert_equal digest("messages/show"), node.digest + end + def test_top_level_change_reflected assert_digest_difference("messages/show") do change_template("messages/show") |