Module: ExecJS::Rails::Renderer

Defined in:
lib/execjs/rails/renderer.rb

Constant Summary collapse

@@config =
nil
@@context =
nil

Class Method Summary collapse

Class Method Details

.call(function, *args) ⇒ Object



26
27
28
29
# File 'lib/execjs/rails/renderer.rb', line 26

def self.call(function, *args)
  self.reset! unless @@context
  @@context.call(function, *args)
end

.configObject



12
13
14
# File 'lib/execjs/rails/renderer.rb', line 12

def self.config
  @@config
end

.dump_render_context(path, assigns, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/execjs/rails/renderer.rb', line 52

def self.dump_render_context(path, assigns, options={})
  source = @@config.debug_dump_prefix + @@config.build_source.call() + "; #{ @@config.handler_function_name }('#{ path }', '#{ options }');"
  file = File.open(assigns['__execjs_rails_dump_file'], 'w')
  file.write(source)
  file.close()
  puts "Wrote render context dump to #{ file.path }. If you have node-inspector installed, you can debug it with: "
  puts "node-debug #{ file.path }"
end

.find_and_render(path, opts = {}) ⇒ Object

Check for template using has_view and render if we have it. Otherwise, fail with a template missing error that leads back here



43
44
45
46
47
48
49
50
# File 'lib/execjs/rails/renderer.rb', line 43

def self.find_and_render(path, opts = {})
  if has_view(path)
    render(path, opts)
  else
    # Throw a missing template error if we don't have it
    fail ::ActionView::MissingTemplate.new(["ExecJS::Rails::Renderer"], path, [], true, {})
  end
end

.has_view(path) ⇒ Object



31
32
33
# File 'lib/execjs/rails/renderer.rb', line 31

def self.has_view(path)
  self.call(@@config.has_view_function_name, path)
end

.render(path, opts = {}) ⇒ Object



35
36
37
38
39
# File 'lib/execjs/rails/renderer.rb', line 35

def self.render(path, opts = {})
  output = self.call(@@config.handler_function_name, path, opts)
  fail "ExecJS call to render function `#{function}` returned null" if output.nil?
  output.html_safe
end

.reset!Object



16
17
18
19
20
21
22
23
24
# File 'lib/execjs/rails/renderer.rb', line 16

def self.reset!
  unless @@config.memoize_context && !@@context.nil?
    begin
      @@context = ExecJS.compile(@@config.build_source.call())
    rescue ExecJS::Rails::RuntimeSupport.error_class => error
      @@config.on_error.call(error)
    end
  end
end

.setup!(config = {}) ⇒ Object



8
9
10
# File 'lib/execjs/rails/renderer.rb', line 8

def self.setup!(config = {})
  @@config = config
end