Module: RailsViewAnnotator

Defined in:
lib/rails_view_annotator/version.rb,
lib/rails_view_annotator/action_view/partial_renderer.rb

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

VERSION =
"0.0.10"

Class Method Summary collapse

Class Method Details

.augment_partial_renderer(klass) ⇒ Object



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
# File 'lib/rails_view_annotator/action_view/partial_renderer.rb', line 8

def self.augment_partial_renderer klass
  stock_render = klass.instance_method :render
  klass.send(:define_method, :render) do |*args|
    inner = stock_render.bind(self).call(*args)

    return unless identifier

    short_identifier = Pathname.new(identifier).relative_path_from Rails.root

    r = /^#{Regexp.escape(Rails.root.to_s)}\/([^:]+:\d+)/
    caller.find { |line| line.match r }
    called_from = context = $1

    descriptor = "#{short_identifier} (from #{called_from})"

    if inner.present?
      comment_pattern = "%{partial}"
      template_formats = RailsViewAnnotator.extract_requested_formats_from(args)
      if template_formats.include?(:text) # Do not render any comments for raw plaintext repsonses
        return inner
      elsif template_formats.include?(:js)
        comment_pattern = "/* begin: %{comment} */\n#{comment_pattern}/* end: %{comment} */"
      elsif template_formats.empty? || template_formats.include?(:html)
        comment_pattern = "<!-- begin: %{comment} -->\n#{comment_pattern}<!-- end: %{comment} -->"
      end

      (comment_pattern % {:partial => inner, :comment => descriptor}).html_safe
    end
  end
  klass.send(:include, InstanceMethods)
end

.extract_requested_formats_from(render_arguments) ⇒ Object

Tells for which formats the partial has been requested.



3
4
5
6
# File 'lib/rails_view_annotator/action_view/partial_renderer.rb', line 3

def self.extract_requested_formats_from(render_arguments)
  lookup_context = render_arguments[0].lookup_context
  lookup_context.formats
end