Class: Felt::Help

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
lib/help.rb

Overview

Renders a help element for a form input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, form:, classes: nil, text: nil, **options) ⇒ Help

  • classes: Classes to add to the help element.

  • text: The help text to show. If not provided, the text will be looked up in the ‘forms.<object_name>.<attribute>` translation. See #help for more details. To disable the help, pass an empty string.

All remaining keyword arguments are passed to the help element. See ActionView::Helpers::FormBuilder#help for details.



24
25
26
27
28
29
30
# File 'lib/help.rb', line 24

def initialize(attribute:, form:, classes: nil, text: nil, **options)
  @attribute = attribute
  @classes = classes
  @form = form
  @text = text
  @options = options
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



8
9
10
# File 'lib/help.rb', line 8

def attribute
  @attribute
end

#formObject (readonly)

Returns the value of attribute form.



8
9
10
# File 'lib/help.rb', line 8

def form
  @form
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/help.rb', line 8

def options
  @options
end

Instance Method Details

#classesObject

Returns the classes to use for the help element



11
12
13
14
# File 'lib/help.rb', line 11

def classes
  @classes ||
    Felt.configuration.classes.dig(:help, :default, :default)
end

#render?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/help.rb', line 32

def render?
  text.present?
end

#textObject

Returns the text to render in the help. If no text is configured, returns nil.

Help texts are looked up in the following order:

  1. The text argument passed to the component.

  2. The ‘help` key in the `forms.<object_name>.<attribute>` translation.

  3. The translation value found under ‘helpers.help.<modelname>.<attribute>` (like with ActionView::Helpers::FormBuilder#help).



46
47
48
# File 'lib/help.rb', line 46

def text
  @text ||= translate("help")
end

#text?Boolean

Returns true if the input group has a help text configured

Returns:

  • (Boolean)


51
52
53
# File 'lib/help.rb', line 51

def text?
  text.present?
end