Module: Rack::Webconsole::AssetHelpers

Included in:
Assets
Defined in:
lib/rack/webconsole/asset_helpers.rb

Overview

Helper module to encapsulate the asset loading logic used by the Assets middleware.

For now, the strategy is reading the files from disk. In the future, we should come up with a somewhat more sophisticated strategy, although Rack::Webconsole is used only in development environments, where performance isn’t usually a concern.

Instance Method Summary collapse

Instance Method Details

#css_codeString

Loads the CSS from a file in ‘/public`.

It contains the styles for the console.

Returns:

  • (String)

    the injectable CSS.



30
31
32
33
34
# File 'lib/rack/webconsole/asset_helpers.rb', line 30

def css_code
  '<style type="text/css">' <<
    asset('webconsole.css') <<
    '</style>'
end

#html_codeString

Loads the HTML from a file in ‘/public`.

It contains a form and the needed divs to render the console.

Returns:

  • (String)

    the injectable HTML.



18
19
20
21
22
23
# File 'lib/rack/webconsole/asset_helpers.rb', line 18

def html_code
  out = ""
  out << asset('jquery.html') if Webconsole.inject_jquery
  out << asset('webconsole.html')
  out
end

#js_codeString

Loads the JavaScript from a file in ‘/public`.

It contains the JavaScript logic of the webconsole.

Returns:

  • (String)

    the injectable JavaScript.



41
42
43
44
45
# File 'lib/rack/webconsole/asset_helpers.rb', line 41

def js_code
  '<script type="text/javascript">' <<
    asset('webconsole.js') <<
    '</script>'
end

#render(javascript, variables = {}) ⇒ String

Inteprolates the given variables inside the javascrpt code

and its values

Parameters:

  • javascript (String)

    The javascript code to insert the variables

  • variables (Hash) (defaults to: {})

    A hash containing the variables names (as keys)

Returns:

  • (String)

    the javascript code with the interpolated variables



54
55
56
57
58
59
60
# File 'lib/rack/webconsole/asset_helpers.rb', line 54

def render(javascript, variables = {})
  javascript_with_variables = javascript.dup
  variables.each_pair do |variable, value|
    javascript_with_variables.gsub!("$#{variable}", value)
  end
  javascript_with_variables
end