Class: ActionView::ThemeFileResolver

Inherits:
OptimizedFileSystemResolver
  • Object
show all
Defined in:
lib/knitkit/extensions/railties/theme_support/theme_file_resolver.rb

Instance Method Summary collapse

Instance Method Details

#cached(key, path_info, details, locals) ⇒ Object

:nodoc:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/knitkit/extensions/railties/theme_support/theme_file_resolver.rb', line 3

def cached(key, path_info, details, locals) #:nodoc:
  name, prefix, partial = path_info
  locals = locals.map { |x| x.to_s }.sort!

  if key && caching?
    if @cached[key][name][prefix][partial][locals].nil? or @cached[key][name][prefix][partial][locals].empty?
      @cached[key][name][prefix][partial][locals] = decorate(yield, path_info, details, locals)
    else
      @cached[key][name][prefix][partial][locals].each do |template|
        #check if the file still exists
        if File.exists? template.identifier
          last_update = mtime(template.identifier)
          if last_update > template.updated_at
            @cached[key][name][prefix][partial][locals].delete_if{|item| item.identifier == template.identifier}
            @cached[key][name][prefix][partial][locals] << build_template(template.identifier, template.virtual_path, (details[:formats] || [:html] if template.formats.empty?), template.locals)
          end
        else
          @cached[key][name][prefix][partial][locals].delete_if{|item| item.identifier == template.identifier}
        end
      end
      @cached[key][name][prefix][partial][locals]
    end
  else
    fresh = decorate(yield, path_info, details, locals)
    return fresh unless key

    scope = @cached[key][name][prefix][partial]
    cache = scope[locals]
    mtime = cache && cache.map(&:updated_at).max

    if !mtime || fresh.empty?  || fresh.any? { |t| t.updated_at > mtime }
      scope[locals] = fresh
    else
      cache
    end
  end
end