Class: RBlade::RailsTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/rblade/rails_template.rb

Instance Method Summary collapse

Instance Method Details

#call(template, source = nil) ⇒ Object



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/rblade/rails_template.rb', line 15

def call(template, source = nil)
  component_store = RBlade::ComponentStore.new

  unless template.nil?
    view_name = template.short_identifier
      .delete_prefix("app/views/")
      .delete_suffix(".rblade")
      .delete_suffix(".html")
      .tr("/", ".")

    # Let the component store know about the current view for relative components
    component_store.view_name("view::#{view_name}")
  end

  preamble = +"_stacks=[];@_rblade_once_tokens=[];@_rblade_stack_manager=RBlade::StackManager.new;"
  if RBlade.direct_component_rendering
    # If the attributes and slot are already set, we don't need to assign them
    unless template&.locals&.include?("attributes") && template.locals.include?("slot")
      preamble << "attributes=RBlade::AttributesManager.new(local_assigns);slot||=yield if block_given?;slot=attributes.delete(:slot) if slot.blank?;"
    end
  end

  -"#{preamble}\n#{component_store.get}\n#{RBlade::Compiler.compile_string(source, component_store)}@output_buffer.raw_buffer.prepend(@_rblade_stack_manager.get(_stacks));@output_buffer;"
end

#translate_location(spot, backtrace_location, source) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rblade/rails_template.rb', line 40

def translate_location(spot, backtrace_location, source)
  view_name = backtrace_location.path
    .sub(/^.*app\/views\/(.+?)(?:\.\w++)?\.rblade$/, "\\1")
    .tr("/", ".")

  # Let the component store know about the current view for relative components
  component_store = RBlade::ComponentStore.new
  component_store.view_name("view::#{view_name}")

  source_map = RBlade::Compiler.generate_source_map(source, component_store)

  # Account for the preamble and component store
  offset = 2 + StringUtility.lines(component_store.get).length
  offset += 1 if spot[:script_lines]&.first == "# frozen_string_literal: true\n"

  location = source_map.source_location(spot[:first_lineno] - offset - 1, spot[:first_column])

  before_lines = StringUtility.lines(source[0...(location[:start_offset])])
  excerpt = source[(location[:start_offset])...(location[:end_offset])]
  excerpt_lines = StringUtility.lines(excerpt)

  first_lineno = before_lines.length
  first_column = before_lines.last.length

  last_lineno = first_lineno + excerpt_lines.length - 1
  last_column = (excerpt_lines.length > 1) ? excerpt_lines.last.length : first_column + excerpt_lines.first.length

  {
    first_lineno: first_lineno,
    first_column: first_column,
    last_lineno: last_lineno,
    last_column: last_column,
    snippet: excerpt,
    script_lines: source.lines,
  }
rescue => e
  Rails.logger&.debug "Unable to locate error position in template: #{e.message}"
  spot
end