Class: Trestle::Form::Fields::FormGroup

Inherits:
Trestle::Form::Field show all
Defined in:
lib/trestle/form/fields/form_group.rb

Constant Summary collapse

WRAPPER_OPTIONS =
[:help, :label, :hide_label]

Instance Attribute Summary

Attributes inherited from Trestle::Form::Field

#block, #builder, #name, #options, #template

Instance Method Summary collapse

Methods inherited from Trestle::Form::Field

#disabled?, #errors, #field, #form_group, #normalize_options!, #readonly?

Constructor Details

#initialize(builder, template, name = nil, options = {}, &block) ⇒ FormGroup

Returns a new instance of FormGroup.



7
8
9
10
11
12
# File 'lib/trestle/form/fields/form_group.rb', line 7

def initialize(builder, template, name=nil, options={}, &block)
  # Normalize options passed as name parameter
  name, options = nil, name if name.is_a?(Hash)

  super(builder, template, name, options, &block)
end

Instance Method Details

#defaultsObject



48
49
50
# File 'lib/trestle/form/fields/form_group.rb', line 48

def defaults
  Trestle::Options.new(class: ["form-group"])
end

#error_messagesObject



36
37
38
39
40
41
42
# File 'lib/trestle/form/fields/form_group.rb', line 36

def error_messages
  tag.ul(class: "invalid-feedback") do
    safe_join(errors.map { |error|
      tag.li(safe_join([icon("fa fa-warning"), error], " "))
    }, "\n")
  end
end

#help_messageObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trestle/form/fields/form_group.rb', line 23

def help_message
  classes = ["form-text"]

  if options[:help].is_a?(Hash)
    message = options[:help][:text]
    classes << "floating" if options[:help][:float]
  else
    message = options[:help]
  end

  tag.p(message, class: classes)
end

#labelObject



44
45
46
# File 'lib/trestle/form/fields/form_group.rb', line 44

def label
  builder.label(name, options[:label], class: ["form-label", ("visually-hidden" if options[:hide_label])].compact)
end

#renderObject



14
15
16
17
18
19
20
21
# File 'lib/trestle/form/fields/form_group.rb', line 14

def render
  tag.div(**options.except(*WRAPPER_OPTIONS)) do
    concat label if name && options[:label] != false
    concat template.capture(&block) if block
    concat help_message if options[:help]
    concat error_messages if name && errors.any?
  end
end