Module: CSSModules::ViewHelper

Defined in:
lib/css_modules/view_helper.rb

Overview

Provides helpers to the view layer. Add it to a controller with Rails’ ‘helper` method.

Examples:

including ViewHelper in ApplicationController (and therefore all its descendants)

class ApplicationController < ActionController::Base
  helper CSSModules::ViewHelper
end

Defined Under Namespace

Classes: ModuleLookup

Instance Method Summary collapse

Instance Method Details

#css_module(module_name, selector_name) ⇒ String #css_module(module_name) {|a| ... } ⇒ void

Overloads:

  • #css_module(module_name, selector_name) ⇒ String

    Apply the styles from ‘module_name` for `selector_name`

    Examples:

    Getting a selector within a module

    css_module("events_index", "header")
    # => "..." (opaque string which matches the stylesheet)

    Parameters:

    • module_name (String)
    • selector_names (String)

      Space-separated DOM ids or class names

    • bare_selector_names (String)

      Space-separated selectors to be appended without the module

    Returns:

    • (String)

      modulized selector name for ‘class=` or `id=` in a view

  • #css_module(module_name) {|a| ... } ⇒ void

    This method returns an undefined value.

    Modulize selectors within a block using the yielded helper.

    Examples:

    modulizing a few selectors

    <% css_module("events_index") do |events_module| %>
      <h1 class="<%= events_module.selector("heading") %>">All events</h1>
      <p id="<%= events_module.selector("description") %>"> ... </p>
    <% end %>

    Parameters:

    • module_name (String)

    Yield Parameters:

    • a (ModuleLookup)

      helper for modulizing selectors within ‘module_name`



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/css_modules/view_helper.rb', line 34

def css_module(module_name, selector_names = nil, bare_selector_names = nil, &block)
  lookup = ModuleLookup.new(module_name)

  if selector_names.nil? && block_given?
    yield(lookup)
    nil
  elsif selector_names.present?
    lookup.selector(selector_names.to_s, bare_selector_names.to_s)
  else
    raise("css_module must be called with a module_name and either selector_names or a block")
  end
end