Class: ViewComponent::Form::FieldComponent
Direct Known Subclasses
CheckBoxComponent, CollectionCheckBoxesComponent, CollectionRadioButtonsComponent, CollectionSelectComponent, ColorFieldComponent, DateFieldComponent, DateSelectComponent, DatetimeLocalFieldComponent, DatetimeSelectComponent, EmailFieldComponent, ErrorMessageComponent, FileFieldComponent, GroupedCollectionSelectComponent, HintComponent, LabelComponent, MonthFieldComponent, NumberFieldComponent, PasswordFieldComponent, RadioButtonComponent, RangeFieldComponent, RichTextAreaComponent, SearchFieldComponent, SelectComponent, TelephoneFieldComponent, TextAreaComponent, TextFieldComponent, TimeFieldComponent, TimeSelectComponent, TimeZoneSelectComponent, UrlFieldComponent, WeekFieldComponent, WeekdaySelectComponent
Instance Attribute Summary collapse
#form, #object_name, #options
Instance Method Summary
collapse
#html_class, #object_errors, #object_errors?
Constructor Details
#initialize(form, object_name, method_name, options = {}) ⇒ FieldComponent
Returns a new instance of FieldComponent.
13
14
15
16
17
18
|
# File 'app/components/view_component/form/field_component.rb', line 13
def initialize(form, object_name, method_name, options = {})
@method_name = method_name.to_s.dup
super(form, object_name, options)
end
|
Instance Attribute Details
#method_name ⇒ Object
Returns the value of attribute method_name.
9
10
11
|
# File 'app/components/view_component/form/field_component.rb', line 9
def method_name
@method_name
end
|
Instance Method Details
#call ⇒ Object
20
21
22
23
24
|
# File 'app/components/view_component/form/field_component.rb', line 20
def call
raise "`self.tag_klass' should be defined in #{self.class.name}" unless self.class.tag_klass
self.class.tag_klass.new(object_name, method_name, @view_context, options).render
end
|
#label_text ⇒ Object
58
59
60
61
62
63
|
# File 'app/components/view_component/form/field_component.rb', line 58
def label_text
content ||= ActionView::Helpers::Tags::Translator.new(object, object_name, method_name,
scope: "helpers.label").translate
content ||= method_name.humanize
content
end
|
#method_errors ⇒ Object
26
27
28
29
30
31
32
33
|
# File 'app/components/view_component/form/field_component.rb', line 26
def method_errors
return [] unless method_errors?
@method_errors ||= object_errors.to_hash
.fetch_values(*object_method_names) { nil }
.flatten.compact
.map(&:upcase_first)
end
|
#method_errors? ⇒ Boolean
35
36
37
38
39
|
# File 'app/components/view_component/form/field_component.rb', line 35
def method_errors?
return false unless object_errors
object_errors.attribute_names.intersect?(object_method_names)
end
|
#object_method_names ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
|
# File 'app/components/view_component/form/field_component.rb', line 45
def object_method_names
@object_method_names ||= begin
object_method_names = [method_name.to_sym]
if method_name.end_with?("_id") && object.respond_to?(singular_association_method_name)
object_method_names << singular_association_method_name
elsif method_name.end_with?("_ids") && object.respond_to?(collection_association_method_name)
object_method_names << collection_association_method_name
end
object_method_names
end
end
|
#optional?(context: validation_context) ⇒ Boolean
65
66
67
68
69
|
# File 'app/components/view_component/form/field_component.rb', line 65
def optional?(context: validation_context)
return false if object.nil?
!required?(context: context)
end
|
#required?(context: validation_context) ⇒ Boolean
71
72
73
74
75
|
# File 'app/components/view_component/form/field_component.rb', line 71
def required?(context: validation_context)
return false if object.nil?
validators(context: context).any?(ActiveModel::Validations::PresenceValidator)
end
|
#validators(context: validation_context) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'app/components/view_component/form/field_component.rb', line 77
def validators(context: validation_context)
method_validators.select do |validator|
if context.nil?
validator.options[:on].blank?
else
Array(validator.options[:on]).include?(context&.to_sym)
end
end
end
|
#value ⇒ Object
41
42
43
|
# File 'app/components/view_component/form/field_component.rb', line 41
def value
object.public_send(method_name)
end
|