Class: RenderTurboStream::CheckTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/render_turbo_stream/check_template.rb

Instance Method Summary collapse

Constructor Details

#initialize(partial: nil, template: nil, available_instance_variables: nil, action: nil) ⇒ CheckTemplate



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
# File 'lib/render_turbo_stream/check_template.rb', line 4

def initialize(partial: nil, template: nil, available_instance_variables: nil, action: nil)
  if !partial && !template
    raise 'missing attribute partial xor template'
  end
  unless action
    raise 'missing required attribute: action'
  end
  @action = action
  @partial_path = partial
  @template_path = template
  @available_instance_variables = nil #available_instance_variables #=> because if an instance variable was not set on production and the first call, and it was set on a later call, the gem would not know that and not set it.
  prt = (partial ? partial : template).split('/')
  if prt.length < 2
    raise 'Partial or template path must always be specified with the controller path, for example «articles/partial_name».'
  end
  @controller_path = prt[0..-2].join('/')
  @key = relative_path(partial: partial, template: template)
  if production?
    $render_turbo_stream_check_templates ||= {}
    if $render_turbo_stream_check_templates[@key]
      @result = $render_turbo_stream_check_templates[@key]
    else
      @result = { instance_variables: [] }
      fetch_nested_partials(1, partial: @partial_path, template: @template_path)
      $render_turbo_stream_check_templates[@key] ||= @result
    end
  else
    @result = { instance_variables: [] }
    fetch_nested_partials(1, partial: @partial_path, template: @template_path)
  end
end

Instance Method Details

#templates_instance_variablesObject



36
37
38
# File 'lib/render_turbo_stream/check_template.rb', line 36

def templates_instance_variables
  @result[:instance_variables]
end