Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow registering custom dependency trackers

  • Loading branch information...
commit 1879a338655aba393726de1fc5ba0d8cb4cb26d5 1 parent 889bf19
@dasch dasch authored
View
15 actionpack/lib/action_view/dependency_tracker.rb
@@ -1,7 +1,18 @@
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
@@ -62,5 +73,7 @@ def explicit_dependencies
template.source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
end
end
+
+ register_tracker Template::Handlers::ERB, ErbTracker
end
end
View
38 actionpack/test/template/dependency_tracker_test.rb
@@ -0,0 +1,38 @@
+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
View
3  actionpack/test/template/digestor_test.rb
@@ -2,10 +2,11 @@
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
Please sign in to comment.
Something went wrong with that request. Please try again.