Class: ActionForm::Element
- Inherits:
-
Object
- Object
- ActionForm::Element
- Defined in:
- lib/action_form/element.rb
Overview
Represents a form element with input/output configuration and HTML attributes rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#errors_messages ⇒ Object
readonly
Returns the value of attribute errors_messages.
-
#helpers ⇒ Object
Returns the value of attribute helpers.
-
#html_id ⇒ Object
readonly
Returns the value of attribute html_id.
-
#html_name ⇒ Object
readonly
Returns the value of attribute html_name.
-
#input_options ⇒ Object
readonly
Returns the value of attribute input_options.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#output_options ⇒ Object
readonly
Returns the value of attribute output_options.
-
#select_options ⇒ Object
readonly
Returns the value of attribute select_options.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
- .input(type:, **options) ⇒ Object
- .input_options ⇒ Object
- .label(text: nil, display: true, **html_options) ⇒ Object
- .label_options ⇒ Object
- .options(collection) ⇒ Object
- .output(type:, **options) ⇒ Object
- .output_options ⇒ Object
- .select_options ⇒ Object
- .tags(**tags_list) ⇒ Object
- .tags_list ⇒ Object
Instance Method Summary collapse
- #detached? ⇒ Boolean
- #disabled? ⇒ Boolean
- #html_checked ⇒ Object
- #html_value ⇒ Object
-
#initialize(name, object, parent_name: nil) ⇒ Element
constructor
A new instance of Element.
-
#input_html_attributes ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #input_type ⇒ Object
- #label_html_attributes ⇒ Object
- #label_text ⇒ Object
- #readonly? ⇒ Boolean
- #render? ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(name, object, parent_name: nil) ⇒ Element
Returns a new instance of Element.
10 11 12 13 14 15 16 17 18 |
# File 'lib/action_form/element.rb', line 10 def initialize(name, object, parent_name: nil) @name = name @object = object @html_name = build_html_name(name, parent_name) @html_id = build_html_id(name, parent_name) @tags = self.class..dup @errors_messages = (object, name) .merge!(errors: .any?) end |
Instance Attribute Details
#errors_messages ⇒ Object (readonly)
Returns the value of attribute errors_messages.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def @errors_messages end |
#helpers ⇒ Object
Returns the value of attribute helpers.
8 9 10 |
# File 'lib/action_form/element.rb', line 8 def helpers @helpers end |
#html_id ⇒ Object (readonly)
Returns the value of attribute html_id.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def html_id @html_id end |
#html_name ⇒ Object (readonly)
Returns the value of attribute html_name.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def html_name @html_name end |
#input_options ⇒ Object (readonly)
Returns the value of attribute input_options.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def @input_options end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def name @name end |
#output_options ⇒ Object (readonly)
Returns the value of attribute output_options.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def @output_options end |
#select_options ⇒ Object (readonly)
Returns the value of attribute select_options.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def @select_options end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
7 8 9 |
# File 'lib/action_form/element.rb', line 7 def @tags end |
Class Method Details
.input(type:, **options) ⇒ Object
41 42 43 44 |
# File 'lib/action_form/element.rb', line 41 def input(type:, **) @input_options = { type: type }.merge() .merge!(input: type) end |
.input_options ⇒ Object
33 34 35 |
# File 'lib/action_form/element.rb', line 33 def @input_options ||= {} end |
.label(text: nil, display: true, **html_options) ⇒ Object
56 57 58 |
# File 'lib/action_form/element.rb', line 56 def label(text: nil, display: true, **) @label_options = [{ text: text, display: display }, ] end |
.label_options ⇒ Object
21 22 23 |
# File 'lib/action_form/element.rb', line 21 def @label_options ||= [{ text: nil, display: true }, {}] end |
.options(collection) ⇒ Object
51 52 53 54 |
# File 'lib/action_form/element.rb', line 51 def (collection) @select_options = collection .merge!(options: true) end |
.output(type:, **options) ⇒ Object
46 47 48 49 |
# File 'lib/action_form/element.rb', line 46 def output(type:, **) @output_options = { type: type }.merge() .merge!(output: type) end |
.output_options ⇒ Object
29 30 31 |
# File 'lib/action_form/element.rb', line 29 def @output_options ||= {} end |
.select_options ⇒ Object
25 26 27 |
# File 'lib/action_form/element.rb', line 25 def @select_options ||= [] end |
.tags(**tags_list) ⇒ Object
60 61 62 |
# File 'lib/action_form/element.rb', line 60 def (**) .merge!() end |
.tags_list ⇒ Object
37 38 39 |
# File 'lib/action_form/element.rb', line 37 def @tags_list ||= {} end |
Instance Method Details
#detached? ⇒ Boolean
116 117 118 |
# File 'lib/action_form/element.rb', line 116 def detached? false end |
#disabled? ⇒ Boolean
120 121 122 |
# File 'lib/action_form/element.rb', line 120 def disabled? false end |
#html_checked ⇒ Object
85 86 87 88 89 |
# File 'lib/action_form/element.rb', line 85 def html_checked return unless input_type == :checkbox value end |
#html_value ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/action_form/element.rb', line 73 def html_value if input_type == :checkbox value ? "1" : "0" elsif detached? self.class.[:value] elsif object.is_a?(EasyParams::Base) object.public_send(name) else value.to_s end end |
#input_html_attributes ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/action_form/element.rb', line 91 def input_html_attributes # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength attrs = self.class..dup attrs[:name] ||= html_name attrs[:id] ||= html_id attrs[:value] ||= html_value attrs[:checked] ||= html_checked attrs[:disabled] ||= disabled? attrs[:readonly] ||= readonly? unless input_tag? attrs.delete(:type) attrs.delete(:value) end attrs end |
#input_type ⇒ Object
128 129 130 |
# File 'lib/action_form/element.rb', line 128 def input_type self.class.[:type].to_sym end |
#label_html_attributes ⇒ Object
69 70 71 |
# File 'lib/action_form/element.rb', line 69 def label_html_attributes { for: html_id }.merge(self.class..last) end |
#label_text ⇒ Object
65 66 67 |
# File 'lib/action_form/element.rb', line 65 def label_text self.class..first[:text] || name.to_s.tr("_", " ").capitalize end |
#readonly? ⇒ Boolean
124 125 126 |
# File 'lib/action_form/element.rb', line 124 def readonly? false end |
#render? ⇒ Boolean
112 113 114 |
# File 'lib/action_form/element.rb', line 112 def render? true end |
#value ⇒ Object
106 107 108 109 110 |
# File 'lib/action_form/element.rb', line 106 def value return unless object object.public_send(name) end |