Commits

Anonymous committed 1879a33

Allow registering custom dependency trackers

Comments (0)

Files changed (3)

actionpack/lib/action_view/dependency_tracker.rb

 module ActionView
   class DependencyTracker
+    @trackers = Hash.new
+
     def self.find_dependencies(name, template)
-      ErbTracker.call(name, template)
+      handler = template.handler
+      @trackers.fetch(handler).call(name, template)
+    end
+
+    def self.register_tracker(handler, tracker)
+      @trackers[handler] = tracker
+    end
+
+    def self.remove_tracker(handler)
+      @trackers.delete(handler)
     end
 
     class ErbTracker
           template.source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
         end
     end
+
+    register_tracker Template::Handlers::ERB, ErbTracker
   end
 end

actionpack/test/template/dependency_tracker_test.rb

+require 'abstract_unit'
+require 'action_view/dependency_tracker'
+
+class DependencyTrackerTest < ActionView::TestCase
+  Neckbeard = Class.new
+
+  class NeckbeardTracker
+    def self.call(name, template)
+      ["foo/#{name}"]
+    end
+  end
+
+  class FakeTemplate
+    attr_reader :source, :handler
+
+    def initialize(source)
+      @source, @handler = source, Neckbeard
+    end
+  end
+
+  def tracker
+    ActionView::DependencyTracker
+  end
+
+  def setup
+    tracker.register_tracker(Neckbeard, NeckbeardTracker)
+  end
+
+  def teardown
+    tracker.remove_tracker(Neckbeard)
+  end
+
+  def test_finds_tracker_by_template_handler
+    template = FakeTemplate.new("boo/hoo")
+    dependencies = tracker.find_dependencies("boo/hoo", template)
+    assert_equal ["foo/boo/hoo"], dependencies
+  end
+end

actionpack/test/template/digestor_test.rb

 require 'fileutils'
 
 class FixtureTemplate
-  attr_reader :source
+  attr_reader :source, :handler
 
   def initialize(template_path)
     @source = File.read(template_path)
+    @handler = ActionView::Template::Handlers::ERB
   rescue Errno::ENOENT
     raise ActionView::MissingTemplate.new([], "", [], true, [])
   end