Module: ViewSourceMap

Defined in:
lib/view_source_map.rb,
lib/view_source_map/railtie.rb,
lib/view_source_map/version.rb,
lib/view_source_map_rails_6.rb,
lib/view_source_map_rails_6_0.rb,
lib/view_source_map_rails_4_and_5.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.attachObject



2
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
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/view_source_map_rails_6.rb', line 2

def self.attach
  return if defined?(@attached) && @attached
  @attached = true

  ActionView::PartialRenderer.class_eval do
    def render_with_path_comment(partial, context, block)
      content = render_without_path_comment(partial, context, block)
      return content if ViewSourceMap.force_disabled?(@options)

      if @lookup_context.formats.first == :html
        case content
        when ActionView::AbstractRenderer::RenderedCollection
          content.rendered_templates.each do |rendered_template|
            ViewSourceMap.wrap_rendered_template(rendered_template, @options)
          end
        when ActionView::AbstractRenderer::RenderedTemplate
          ViewSourceMap.wrap_rendered_template(content, @options)
        end
      end
      content
    end
    alias_method :render_without_path_comment, :render
    alias_method :render, :render_with_path_comment
  end

  ActionView::TemplateRenderer.class_eval do
    def render_template_with_path_comment(view, template, layout_name, locals)
      render_with_layout(view, template, layout_name, locals) do |layout|
        ActiveSupport::Notifications.instrument(
          'render_template.action_view',
          identifier: template.identifier,
          layout: layout.try(:virtual_path),
        ) do
          content = template.render(view, locals) { |*name| view._layout_for(*name) }
          return content if ViewSourceMap.force_disabled?(locals)

          path = Pathname.new(template.identifier)

          if @lookup_context.formats.first == :html && path.file?
            name = path.relative_path_from(Rails.root)
            "<!-- BEGIN #{name} -->\n#{content}<!-- END #{name} -->".html_safe
          else
            content
          end
        end
      end
    end
    alias_method :render_template_without_path_comment, :render_template
    alias_method :render_template, :render_template_with_path_comment
  end
end

.detachObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/view_source_map.rb', line 12

def self.detach
  return unless @attached
  @attached = false
  ActionView::PartialRenderer.class_eval do
    undef_method :render_with_path_comment
    alias_method :render, :render_without_path_comment
  end

  ActionView::TemplateRenderer.class_eval do
    undef_method :render_template_with_path_comment
    alias_method :render_template, :render_template_without_path_comment
  end
end

.force_disabled?(options) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/view_source_map.rb', line 26

def self.force_disabled?(options)
  return false if options.nil?
  return true  if options[:view_source_map] == false
  return false if options[:locals].nil?
  options[:locals][:view_source_map] == false
end

.wrap_rendered_template(rendered_template, options) ⇒ Object

Parameters:

  • rendered_template (ActionView::AbstractRenderer::RenderedTemplate)
  • options (Hash)

    Options passed to #render method.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/view_source_map_rails_6.rb', line 57

def self.wrap_rendered_template(rendered_template, options)
  name = begin
    if options[:layout]
      "#{options[:layout]}(layout)"
    elsif rendered_template.template.respond_to?(:identifier)
      Pathname.new(rendered_template.template.identifier).relative_path_from(Rails.root)
    end
  end

  if name
    rendered_template.instance_variable_set(
      :@body,
      "<!-- BEGIN #{name} -->\n#{rendered_template.body}<!-- END #{name} -->".html_safe
    )
  end
end