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

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

Constructor Details

This class inherits a constructor from Trestle::Form::Field

Instance Method Details

#defaultsObject



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

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

#error_messageObject



29
30
31
32
33
# File 'lib/trestle/form/fields/form_group.rb', line 29

def error_message
  (:p, class: "invalid-feedback") do
    safe_join([icon("fa fa-warning"), errors.first], " ")
  end
end

#help_messageObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/trestle/form/fields/form_group.rb', line 16

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

  (:p, message, class: classes)
end

#labelObject



35
36
37
# File 'lib/trestle/form/fields/form_group.rb', line 35

def label
  builder.label(name, options[:label], class: ["control-label", ("sr-only" if options[:hide_label])].compact)
end

#renderObject



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

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