class ActionviewPrecompiler::Precompiler

Attributes

templates[R]

Public Class Methods

new(view_dirs) click to toggle source
# File lib/actionview_precompiler/precompiler.rb, line 26
def initialize(view_dirs)
  @templates =
    view_dirs.flat_map do |view_dir|
      Dir["**/*", base: view_dir].map do |file|
        fullpath = File.expand_path(file, view_dir)
        next if File.directory?(fullpath)

        Template.new(fullpath, file)
      end.compact
    end

  determine_locals
end

Public Instance Methods

determine_locals() click to toggle source
# File lib/actionview_precompiler/precompiler.rb, line 75
def determine_locals
  @locals_sets = {}

  @templates.each do |template|
    parser = TemplateParser.new(template.fullpath)
    parser.render_calls.each do |render_call|
      virtual_path = render_call.virtual_path
      unless virtual_path.include?("/")
        # Not necessarily true, since the perfix is based on the current
        # controller, but is a safe bet most of the time.
        virtual_path = "#{template.prefix}/#{virtual_path}"
      end
      @locals_sets[virtual_path] ||= []
      @locals_sets[virtual_path] << render_call.locals_keys.map(&:to_s).sort
    end
  end

  @locals_sets.each_value(&:uniq!)
end
each_lookup_args() { |action, prefix, partial?, locals, details| ... } click to toggle source
# File lib/actionview_precompiler/precompiler.rb, line 40
def each_lookup_args
  return enum_for(__method__) unless block_given?

  each_template_render do |template, locals|
    details = {
      locale: Array(template.details[:locale]),
      variants: Array(template.details[:variant]),
      formats: Array(template.details[:format]),
      handlers: Array(template.details[:handler])
    }

    yield [template.action, template.prefix, template.partial?, locals, details]
  end

  nil
end
each_template_render() { |template, locals| ... } click to toggle source
# File lib/actionview_precompiler/precompiler.rb, line 57
def each_template_render
  return enum_for(__method__) unless block_given?

  @templates.each do |template|
    locals_set = @locals_sets[template.virtual_path]
    if locals_set
      locals_set.each do |locals|
        yield template, locals
      end
    elsif !template.partial?
      # For now, guess that non-partials we haven't seen take no locals
      yield template, []
    else
      # Locals unknown
    end
  end
end