Class: ViewComponent::Form::LabelComponent

Inherits:
FieldComponent show all
Defined in:
app/components/view_component/form/label_component.rb

Instance Attribute Summary collapse

Attributes inherited from FieldComponent

#method_name

Attributes inherited from BaseComponent

#form, #object_name, #options

Instance Method Summary collapse

Methods inherited from FieldComponent

#label_text, #method_errors, #method_errors?, #object_method_names, #optional?, #required?, #validators, #value

Methods inherited from BaseComponent

#html_class, #object_errors, #object_errors?

Methods included from ClassNamesHelper

#build_tag_values, #class_names

Constructor Details

#initialize(form, object_name, method_name, content_or_options = nil, options = nil) ⇒ LabelComponent

Returns a new instance of LabelComponent.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/components/view_component/form/label_component.rb', line 8

def initialize(form, object_name, method_name, content_or_options = nil, options = nil)
  options ||= {}

  content_is_options = content_or_options.is_a?(Hash)
  if content_is_options
    options.merge! content_or_options
    @attribute_content = nil
  else
    @attribute_content = content_or_options
  end

  super(form, object_name, method_name, options)
end

Instance Attribute Details

#attribute_contentObject (readonly)

Returns the value of attribute attribute_content.



6
7
8
# File 'app/components/view_component/form/label_component.rb', line 6

def attribute_content
  @attribute_content
end

Instance Method Details

#builderObject



32
33
34
35
36
37
38
# File 'app/components/view_component/form/label_component.rb', line 32

def builder
  @builder ||= begin
    tag_value = options.delete("value")

    ActionView::Helpers::Tags::Label::LabelBuilder.new(@view_context, object_name, method_name, object, tag_value)
  end
end

#callObject



22
23
24
25
26
27
28
29
# File 'app/components/view_component/form/label_component.rb', line 22

def call
  content_or_options = nil

  content_or_options = content || attribute_content if content.present? || attribute_content.present?

  ActionView::Helpers::Tags::Label.new(object_name, method_name, @view_context, content_or_options,
                                       options).render
end