Class: WebConsole::View

Inherits:
ActionView::Base
  • Object
show all
Defined in:
lib/web_console/view.rb

Instance Method Summary collapse

Instance Method Details

#only_on_error_page(*args) ⇒ Object

Execute a block only on error pages.

The error pages are special, because they are the only pages that currently require multiple bindings. We get those from exceptions.



9
10
11
# File 'lib/web_console/view.rb', line 9

def only_on_error_page(*args)
  yield if Thread.current[:__web_console_exception].present?
end

#only_on_regular_page(*args) ⇒ Object

Execute a block only on regular, non-error, pages.



14
15
16
# File 'lib/web_console/view.rb', line 14

def only_on_regular_page(*args)
  yield if Thread.current[:__web_console_binding].present?
end

#renderObject

Custom ActionView::Base#render wrapper which silences all the log printings.

Helps to keep the Rails logs clean during errors.



41
42
43
44
45
46
47
# File 'lib/web_console/view.rb', line 41

def render(*)
  if (logger = WebConsole.logger) && logger.respond_to?(:silence)
    WebConsole.logger.silence { super }
  else
    super
  end
end

#render_inlined_string(template) ⇒ Object

Render inlined string to be used inside of JavaScript code.

The inlined string is returned as an actual JavaScript string. You don’t need to wrap the result yourself.



33
34
35
# File 'lib/web_console/view.rb', line 33

def render_inlined_string(template)
  render(template: template, layout: "layouts/inlined_string")
end

#render_javascript(template) ⇒ Object

Render JavaScript inside a script tag and a closure.

This one lets write JavaScript that will automatically get wrapped in a script tag and enclosed in a closure, so you don’t have to worry for leaking globals, unless you explicitly want to.



23
24
25
26
27
# File 'lib/web_console/view.rb', line 23

def render_javascript(template)
  assign(template: template)
  assign(nonce: @env["action_dispatch.content_security_policy_nonce"])
  render(template: template, layout: "layouts/javascript")
end

#t(key, options = {}) ⇒ Object

Override method for ActionView::Helpers::TranslationHelper#t.

This method escapes the original return value for JavaScript, since the method returns a HTML tag with some attributes when the key is not found, so it could cause a syntax error if we use the value in the string literals.



54
55
56
# File 'lib/web_console/view.rb', line 54

def t(key, options = {})
  j super
end