Class: Fluxbit::Form::Component

Inherits:
Component
  • Object
show all
Defined in:
app/components/fluxbit/form/component.rb

Overview

Interface to Inputs

Direct Known Subclasses

FieldComponent, HelpTextComponent, LabelComponent

Constant Summary

Constants inherited from Component

Component::ComponentObj

Instance Method Summary collapse

Methods inherited from Component

#add, #add_popover_or_tooltip, #anyicon, #element_name, #fx_id, #initialize, #options, #random_id, #remove_class, #render_popover_or_tooltip, #target

Constructor Details

This class inherits a constructor from Fluxbit::Component

Instance Method Details

#define_help_text(help_text, object, attribute) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/components/fluxbit/form/component.rb', line 12

def define_help_text(help_text, object, attribute)
  return nil if help_text.is_a? FalseClass

  if help_text.nil? && !object.nil? && !attribute.nil?
    help_text = I18n.t(
      attribute,
      scope: [ :activerecord, :help_text, object.class.name.underscore.to_sym ],
      default: nil
    )
  end

  (help_text.is_a?(Array) ? help_text : [ help_text ]) + errors
end

#define_helper_popover(helper_popover, object, attribute) ⇒ Object



26
27
28
29
30
31
# File 'app/components/fluxbit/form/component.rb', line 26

def define_helper_popover(helper_popover, object, attribute)
  return helper_popover if (helper_popover != false && !helper_popover.nil?) || object.nil?

  object_name = object.class.name.underscore.to_sym
  I18n.t(attribute, scope: [ :activerecord, :helper_popover, object_name ], default: nil)
end

#errorsObject



53
54
55
56
57
# File 'app/components/fluxbit/form/component.rb', line 53

def errors
  return [] unless @object&.errors&.any?

  @object.errors.filter { |f| f.attribute == @attribute }.map(&:full_message)
end

#help_textObject



59
60
61
62
63
64
65
# File 'app/components/fluxbit/form/component.rb', line 59

def help_text
  return "" if @help_text.blank? || @help_text.compact.blank?

  @help_text.compact.map do |text|
    Fluxbit::Form::HelpTextComponent.new(color: @color).with_content(text).render_in(view_context)
  end.join.html_safe
end

#idObject



5
6
7
8
9
10
# File 'app/components/fluxbit/form/component.rb', line 5

def id
  return @id ||= random_id if @props[:id].nil? && @form.nil?
  return @props[:id] unless @props[:id].nil?

  "#{@form.object_name}_#{@attribute}"
end

#labelObject



41
42
43
44
45
46
47
48
49
50
51
# File 'app/components/fluxbit/form/component.rb', line 41

def label
  return "" if @label.blank?

  Fluxbit::Form::LabelComponent.new(
    for:                      id,
    color:                    @color,
    helper_popover:           @helper_popover,
    helper_popover_placement: @helper_popover_placement,
    class:                    @label_class
  ).with_content(@label).render_in(view_context)
end

#label_value(label, object, attribute, id) ⇒ Object



33
34
35
36
37
38
39
# File 'app/components/fluxbit/form/component.rb', line 33

def label_value(label, object, attribute, id)
  return object.class.human_attribute_name(attribute) if label.nil? && !object.nil? && !attribute.nil?
  return id.to_s.humanize if label.nil? && !id.nil?
  return label unless label.nil?

  nil
end