1
module ActionView #:nodoc:
2
class ReloadableTemplate < Template
4
class TemplateDeleted < ActionView::ActionViewError
7
class ReloadablePath < Template::Path
18
alias_method :load!, :new_request!
21
if found_template = @paths[path]
23
found_template.reset_cache_if_stale!
24
rescue TemplateDeleted
25
unregister_template(found_template)
29
load_all_templates_from_dir(templates_dir_from_path(path))
30
# don't ever hand out a template without running a stale check
31
(new_template = @paths[path]) && new_template.reset_cache_if_stale!
36
def register_template_from_file(template_full_file_path)
37
if !@paths[relative_path = relative_path_for_template_file(template_full_file_path)] && File.file?(template_full_file_path)
38
register_template(ReloadableTemplate.new(relative_path, self))
42
def register_template(template)
43
template.accessible_paths.each do |path|
44
@paths[path] = template
48
# remove (probably deleted) template from cache
49
def unregister_template(template)
50
template.accessible_paths.each do |template_path|
51
@paths.delete(template_path) if @paths[template_path] == template
53
# fill in any newly created gaps
54
@paths.values.uniq.each do |template|
55
template.accessible_paths.each {|path| @paths[path] ||= template}
59
# load all templates from the directory of the requested template
60
def load_all_templates_from_dir(dir)
61
# hit disk only once per template-dir/request
62
@disk_cache[dir] ||= template_files_from_dir(dir).each {|template_file| register_template_from_file(template_file)}
65
def templates_dir_from_path(path)
66
dirname = File.dirname(path)
67
File.join(@path, dirname == '.' ? '' : dirname)
70
# get all the template filenames from the dir
71
def template_files_from_dir(dir)
72
Dir.glob(File.join(dir, '*'))
83
# we don't ever want to get frozen
91
attr_accessor :previously_last_modified
94
previously_last_modified.nil? || previously_last_modified < mtime
95
rescue Errno::ENOENT => e
96
undef_my_compiled_methods!
100
def reset_cache_if_stale!
102
flush_cache 'source', 'compiled_source'
103
undef_my_compiled_methods!
104
@previously_last_modified = mtime
109
# remove any compiled methods that look like they might belong to me
110
def undef_my_compiled_methods!
111
ActionView::Base::CompiledTemplates.public_instance_methods.grep(/#{Regexp.escape(method_name_without_locals)}(?:_locals_)?/).each do |m|
112
ActionView::Base::CompiledTemplates.send(:remove_method, m)