Class: ViewComponent::Template::File

Inherits:
ViewComponent::Template show all
Defined in:
lib/view_component/template.rb

Instance Attribute Summary

Attributes inherited from ViewComponent::Template

#details, #path

Instance Method Summary collapse

Methods inherited from ViewComponent::Template

#call_method_name, #compile_to_component, #coverage_running?, #default_format?, #inline_call?, #normalized_variant_name, #requires_compiled_superclass?, #safe_method_name, #safe_method_name_call

Constructor Details

#initialize(component:, details:, path:) ⇒ File

Returns a new instance of File.



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
53
54
55
56
57
58
# File 'lib/view_component/template.rb', line 23

def initialize(component:, details:, path:)
  # If the template file has no format (e.g. .erb instead of .html.erb),
  # assume the default format (html).
  if details.format.nil?
    Kernel.warn("WARNING: Template format for #{path} is missing, defaulting to :html.")
    details = ActionView::TemplateDetails.new(details.locale, details.handler, DEFAULT_FORMAT, details.variant)
  end

  @strip_annotation_line = false

  # Rails 8.1 added a newline to compiled ERB output (rails/rails#53731).
  # Use -1 to compensate for correct line numbers in stack traces.
  # However, negative line numbers cause segfaults when Ruby's coverage
  # is enabled (bugs.ruby-lang.org/issues/19363). In that case, strip the
  # annotation line from compiled source instead.
  lineno =
    if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
      if coverage_running?
        # Can't use negative lineno with coverage (causes segfault on Linux).
        # Strip annotation line if enabled to preserve correct line numbers.
        @strip_annotation_line = ActionView::Base.annotate_rendered_view_with_filenames
        0
      else
        -1
      end
    else
      0
    end

  super(
    component: component,
    details: details,
    path: path,
    lineno: lineno
  )
end

Instance Method Details

#sourceObject

Load file each time we look up #source in case the file has been modified



65
66
67
# File 'lib/view_component/template.rb', line 65

def source
  ::File.read(@path)
end

#typeObject



60
61
62
# File 'lib/view_component/template.rb', line 60

def type
  :file
end