Module: Proscenium::Helper

Defined in:
lib/proscenium/helper.rb

Instance Method Summary collapse

Instance Method Details

#class_names(*names, path: nil) ⇒ String

Returns the transformed CSS module names concatenated as a string.

Parameters:

  • name (String, Symbol, Array<String,Symbol>)
  • path (Pathname) (defaults to: nil)

    the path to the CSS file to use for the transformation.

Returns:

  • (String)

    the transformed CSS module names concatenated as a string.



48
49
50
51
52
53
54
55
# File 'lib/proscenium/helper.rb', line 48

def class_names(*names, path: nil)
  names = names.flatten.compact

  return if names.empty?

  path ||= Pathname.new(@lookup_context.find(@virtual_path).identifier).sub_ext('')
  CssModule::Transformer.new(path).class_names(*names).map { |name, _| name }.join(' ')
end

#compute_asset_path(path, options = {}) ⇒ Object

Overriden to allow regular use of javascript_include_tag and stylesheet_link_tag, while still building with Proscenium. It’s important to note that ‘include_assets` will not call this, as those asset paths all begin with a slash, which the Rails asset helpers do not pass through to here.

If the given ‘path` is a bare path (does not start with `/` or `./` or `../`), then we use Rails default conventions, and serve CSS from /app/assets/stylesheets and JS from /app/javascript.



22
23
24
25
26
27
28
29
30
31
# File 'lib/proscenium/helper.rb', line 22

def compute_asset_path(path, options = {})
  if %i[javascript stylesheet].include?(options[:type])
    path.prepend DEFAULT_RAILS_ASSET_PATHS[options[:type]] unless path.start_with?('./', '../')

    result = Proscenium::Builder.build_to_path(path, base_url: request.base_url)
    return result.split('::').last.delete_prefix 'public'
  end

  super
end

#css_module(*names, path: nil) ⇒ String

Accepts one or more CSS class names, and transforms them into CSS module names.

Parameters:

  • name (String, Symbol, Array<String,Symbol>)
  • path (Pathname) (defaults to: nil)

    the path to the CSS module file to use for the transformation.

Returns:

  • (String)

    the transformed CSS module names concatenated as a string.

See Also:



39
40
41
42
43
# File 'lib/proscenium/helper.rb', line 39

def css_module(*names, path: nil)
  path ||= Pathname.new(@lookup_context.find(@virtual_path).identifier).sub_ext('')
  CssModule::Transformer.new(path).class_names(*names, require_prefix: false)
                        .map { |name, _| name }.join(' ')
end

#include_assetsObject



57
58
59
# File 'lib/proscenium/helper.rb', line 57

def include_assets
  include_stylesheets + include_javascripts
end

#include_javascriptsString Also known as: side_load_javascripts

Includes all javascripts that have been imported and side loaded.

Returns:

  • (String)

    the HTML tags for the javascripts.



70
71
72
# File 'lib/proscenium/helper.rb', line 70

def include_javascripts
  (SideLoad::LAZY_COMMENT + SideLoad::JS_COMMENT).html_safe
end

#include_stylesheetsObject Also known as: side_load_stylesheets



61
62
63
# File 'lib/proscenium/helper.rb', line 61

def include_stylesheets
  SideLoad::CSS_COMMENT.html_safe
end

#sideload_assets(value) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/proscenium/helper.rb', line 5

def sideload_assets(value)
  if value.nil?
    @current_template.instance_variable_defined?(:@sideload_assets_options) &&
      @current_template.remove_instance_variable(:@sideload_assets_options)
  else
    @current_template.instance_variable_set :@sideload_assets_options, value
  end
end