2012-12-13 08:47:33 -05:00
|
|
|
require 'thread_safe'
|
2012-10-17 18:20:51 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
module ActionView
|
|
|
|
class Digestor
|
2012-12-21 16:51:41 -05:00
|
|
|
EXPLICIT_DEPENDENCY = /# Template Dependency: (\S+)/
|
2012-08-29 15:23:15 -04:00
|
|
|
|
|
|
|
# Matches:
|
|
|
|
# render partial: "comments/comment", collection: commentable.comments
|
|
|
|
# render "comments/comments"
|
|
|
|
# render 'comments/comments'
|
|
|
|
# render('comments/comments')
|
|
|
|
#
|
|
|
|
# render(@topic) => render("topics/topic")
|
|
|
|
# render(topics) => render("topics/topic")
|
|
|
|
# render(message.topics) => render("topics/topic")
|
|
|
|
RENDER_DEPENDENCY = /
|
2012-08-30 08:17:03 -04:00
|
|
|
render\s* # render, followed by optional whitespace
|
2012-08-30 08:27:07 -04:00
|
|
|
\(? # start an optional parenthesis for the render call
|
2012-08-30 07:13:47 -04:00
|
|
|
(partial:|:partial\s+=>)?\s* # naming the partial, used with collection -- 1st capture
|
|
|
|
([@a-z"'][@a-z_\/\."']+) # the template name itself -- 2nd capture
|
2012-08-29 15:23:15 -04:00
|
|
|
/x
|
|
|
|
|
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 = {})
|
2012-12-13 08:47:33 -05:00
|
|
|
@@cache["#{name}.#{format}"] ||= begin
|
2012-10-17 18:20:51 -04:00
|
|
|
klass = options[:partial] || name.include?("/_") ? PartialDigestor : Digestor
|
2012-12-13 08:47:33 -05:00
|
|
|
klass.new(name, format, finder).digest
|
2012-10-17 18:20:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :name, :format, :finder
|
|
|
|
|
|
|
|
def initialize(name, format, finder)
|
|
|
|
@name, @format, @finder = name, format, finder
|
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
|
|
|
|
render_dependencies + explicit_dependencies
|
|
|
|
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 directory
|
2012-10-11 12:36:58 -04:00
|
|
|
name.split("/")[0..-2].join("/")
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def partial?
|
2012-10-17 18:20:51 -04:00
|
|
|
false
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def source
|
|
|
|
@source ||= finder.find(logical_name, [], partial?, formats: [ format ]).source
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependency_digest
|
|
|
|
dependencies.collect do |template_name|
|
2012-12-13 08:47:33 -05:00
|
|
|
Digestor.digest(template_name, format, finder, partial: true)
|
2012-08-29 15:23:15 -04:00
|
|
|
end.join("-")
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_dependencies
|
|
|
|
source.scan(RENDER_DEPENDENCY).
|
|
|
|
collect(&:second).uniq.
|
|
|
|
|
|
|
|
# render(@topic) => render("topics/topic")
|
|
|
|
# render(topics) => render("topics/topic")
|
|
|
|
# render(message.topics) => render("topics/topic")
|
|
|
|
collect { |name| name.sub(/\A@?([a-z]+\.)*([a-z_]+)\z/) { "#{$2.pluralize}/#{$2.singularize}" } }.
|
|
|
|
|
|
|
|
# render("headline") => render("message/headline")
|
|
|
|
collect { |name| name.include?("/") ? name : "#{directory}/#{name}" }.
|
2012-09-05 10:05:50 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
# replace quotes from string renders
|
|
|
|
collect { |name| name.gsub(/["']/, "") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def explicit_dependencies
|
|
|
|
source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
|
|
|
|
end
|
|
|
|
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
|