2012-12-13 08:47:33 -05:00
|
|
|
require 'thread_safe'
|
2012-12-16 17:17:53 -05:00
|
|
|
require 'action_view/dependency_tracker'
|
2012-10-17 18:20:51 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
module ActionView
|
|
|
|
class Digestor
|
2012-09-05 13:38:16 -04:00
|
|
|
cattr_reader(:cache)
|
2012-12-13 08:47:33 -05:00
|
|
|
@@cache = ThreadSafe::Cache.new
|
2012-08-29 15:23:15 -04:00
|
|
|
|
|
|
|
def self.digest(name, format, finder, options = {})
|
2013-01-08 13:20:47 -05:00
|
|
|
cache_key = [name, format] + Array.wrap(options[:dependencies])
|
|
|
|
@@cache[cache_key.join('.')] ||= begin
|
2012-10-17 18:20:51 -04:00
|
|
|
klass = options[:partial] || name.include?("/_") ? PartialDigestor : Digestor
|
2013-01-08 13:20:47 -05:00
|
|
|
klass.new(name, format, finder, options).digest
|
2012-10-17 18:20:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
attr_reader :name, :format, :finder, :options
|
2012-10-17 18:20:51 -04:00
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
def initialize(name, format, finder, options={})
|
|
|
|
@name, @format, @finder, @options = name, format, finder, options
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def digest
|
2012-09-04 18:29:00 -04:00
|
|
|
Digest::MD5.hexdigest("#{source}-#{dependency_digest}").tap do |digest|
|
2012-08-29 15:23:15 -04:00
|
|
|
logger.try :info, "Cache digest for #{name}.#{format}: #{digest}"
|
|
|
|
end
|
|
|
|
rescue ActionView::MissingTemplate
|
|
|
|
logger.try :error, "Couldn't find template for digesting: #{name}.#{format}"
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependencies
|
2012-12-16 17:17:53 -05:00
|
|
|
DependencyTracker.find_dependencies(name, template)
|
2012-08-29 15:23:15 -04:00
|
|
|
rescue ActionView::MissingTemplate
|
|
|
|
[] # File doesn't exist, so no dependencies
|
|
|
|
end
|
|
|
|
|
|
|
|
def nested_dependencies
|
|
|
|
dependencies.collect do |dependency|
|
2012-10-17 18:20:51 -04:00
|
|
|
dependencies = PartialDigestor.new(dependency, format, finder).nested_dependencies
|
2012-08-29 15:23:15 -04:00
|
|
|
dependencies.any? ? { dependency => dependencies } : dependency
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-09-05 12:51:24 -04:00
|
|
|
|
|
|
|
def logger
|
|
|
|
ActionView::Base.logger
|
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def logical_name
|
|
|
|
name.gsub(%r|/_|, "/")
|
|
|
|
end
|
2012-09-05 10:05:50 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def partial?
|
2012-10-17 18:20:51 -04:00
|
|
|
false
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-16 17:17:53 -05:00
|
|
|
def template
|
2013-04-25 10:14:22 -04:00
|
|
|
@template ||= finder.find(logical_name, [], partial?, formats: [ format ])
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def source
|
2012-12-16 17:17:53 -05:00
|
|
|
template.source
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dependency_digest
|
2013-01-08 13:20:47 -05:00
|
|
|
template_digests = dependencies.collect do |template_name|
|
2012-12-13 08:47:33 -05:00
|
|
|
Digestor.digest(template_name, format, finder, partial: true)
|
2013-01-08 13:20:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
(template_digests + injected_dependencies).join("-")
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
def injected_dependencies
|
|
|
|
Array.wrap(options[:dependencies])
|
|
|
|
end
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
2012-10-17 18:20:51 -04:00
|
|
|
|
|
|
|
class PartialDigestor < Digestor # :nodoc:
|
|
|
|
def partial?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2012-09-05 10:05:50 -04:00
|
|
|
end
|