Class: AppMap::Handler::Rails::Template::ResolverHandler

Inherits:
Function show all
Defined in:
lib/appmap/handler/rails/template.rb

Overview

Hooks the ActionView::Resolver methods find_all, find_all_anywhere. The resolver is used during template rendering to lookup the template file path from parameters such as the template name, prefix, and partial (boolean).

Instance Attribute Summary

Attributes inherited from AppMap::Hook::Method

#arity, #hook_class, #hook_method, #hook_package, #parameters

Instance Method Summary collapse

Methods inherited from AppMap::Hook::Method

#activate, #call, #initialize

Constructor Details

This class inherits a constructor from AppMap::Hook::Method

Instance Method Details

#handle_call(receiver, args) ⇒ Object



114
115
116
117
118
119
# File 'lib/appmap/handler/rails/template.rb', line 114

def handle_call(receiver, args)
  name, prefix, partial = args
  warn "Resolver: #{{ name: name, prefix: prefix, partial: partial }}" if LOG

  super
end

#handle_return(call_event_id, elapsed, return_value, exception) ⇒ Object

When the resolver returns, look to see if there is template rendering underway. If so, populate the template path. In all cases, add a TemplateMethod so that the template will be recorded in the classMap.



124
125
126
127
128
129
130
131
132
133
# File 'lib/appmap/handler/rails/template.rb', line 124

def handle_return(call_event_id, elapsed, return_value, exception)
  renderer = Array(Thread.current[TEMPLATE_RENDERER]).last
  path_obj = Array(return_value).first

  warn "Resolver return: #{path_obj}" if LOG

  record_template_path renderer, path_obj

  super
end

#path_from_obj(path_obj) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/appmap/handler/rails/template.rb', line 143

def path_from_obj(path_obj)
  path =
    if path_obj.respond_to?(:identifier) && path_obj.inspect.index('#<')
      path_obj.identifier
    else
      path_obj.inspect
    end
  path = path[Dir.pwd.length + 1..] if path.index(Dir.pwd) == 0
  path
end

#record_template_path(renderer, path_obj) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/appmap/handler/rails/template.rb', line 135

def record_template_path(renderer, path_obj)
  return unless path_obj

  path = path_from_obj path_obj
  AppMap.tracing.record_method(TemplateMethod.new(path))
  renderer.path ||= path if renderer
end