Class: Opal::Rails::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/rails/template_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(template, source = template.source) ⇒ Object



7
8
9
# File 'lib/opal/rails/template_handler.rb', line 7

def self.call(template, source = template.source)
  new.call(template, source)
end

Instance Method Details

#call(template, source = template.source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/opal/rails/template_handler.rb', line 11

def call(template, source = template.source)
  escaped = source.gsub(':', '\:')
  string = '%q:' + escaped + ':'

  <<-RUBY
    config = ::Rails.application.config.opal

    code = []
    code << 'Object.new.instance_eval {'

    if config.assign_locals_in_templates?
      code << ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(local_assigns)).map { |key, val| "\#{key} = \#{val.inspect};" }.join
    end

    if config.assign_instance_variables_in_templates?
      code << ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(@_assigns)).map { |key, val| "@\#{key} = \#{val.inspect};" }.join
    end

    code << #{string}
    code << '}'
    Opal.compile(code.join("\n"))
  RUBY
end