Module: StylesHelper

Defined in:
app/helpers/styles_helper.rb

Instance Method Summary collapse

Instance Method Details

#class_names(*classes) ⇒ Object

Conditionally joins CSS class names into a single string.

Accepts any mix of:

  • Strings / symbols (class names; nil and blank strings are skipped)
  • Hashes of { 'class-name' => boolean } (only keys with truthy values are included)
  • Trailing keyword arguments (collected as one Hash by Ruby), same as a Hash arg

Examples:

Multiple base classes + conditional hash (keywords)

class_names('btn m-0', 'btn-danger': live, 'btn-success': !live)

Multiple strings (e.g. optional extra classes)

class_names('btn', 'btn-outline-primary', btn_class, 'dropdown-toggle')


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/styles_helper.rb', line 13

def class_names(*classes)
  parts = []
  classes.each do |arg|
    case arg
    when Hash
      parts.concat(arg.select { |_, v| v }.keys.map(&:to_s))
    when nil, false
      next
    else
      s = arg.to_s.strip
      parts << s unless s.empty?
    end
  end
  parts.join(' ')
end

#stylesheet(*files) ⇒ Object



29
30
31
# File 'app/helpers/styles_helper.rb', line 29

def stylesheet(*files)
  content_for(:head) { webpack_css_include(*files) }
end