Class: ActionForm::Element
- Inherits:
-
Object
- Object
- ActionForm::Element
- Includes:
- Composition
- Defined in:
- lib/action_form/element.rb
Overview
Represents a form element with input/output configuration and HTML attributes rubocop:disable Metrics/ClassLength
Constant Summary collapse
- PROTECTED_TAGS =
%i[input output options errors].freeze
Class Attribute Summary collapse
-
.default ⇒ Object
readonly
Returns the value of attribute default.
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:, default: nil, **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(**extra_tags) ⇒ Object
- .tags_list ⇒ Object
Instance Method Summary collapse
- #detached? ⇒ Boolean
- #disabled? ⇒ Boolean
- #html_checked ⇒ Object
- #html_value ⇒ Object
-
#initialize(name, object, parent_name: nil, owner: 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
Methods included from Composition
Constructor Details
#initialize(name, object, parent_name: nil, owner: nil) ⇒ Element
Returns a new instance of Element.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/action_form/element.rb', line 14 def initialize(name, object, parent_name: nil, owner: 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?) @owner = owner end |
Class Attribute Details
.default ⇒ Object (readonly)
Returns the value of attribute default.
26 27 28 |
# File 'lib/action_form/element.rb', line 26 def default @default end |
Instance Attribute Details
#errors_messages ⇒ Object (readonly)
Returns the value of attribute errors_messages.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def @errors_messages end |
#helpers ⇒ Object
Returns the value of attribute helpers.
12 13 14 |
# File 'lib/action_form/element.rb', line 12 def helpers @helpers end |
#html_id ⇒ Object (readonly)
Returns the value of attribute html_id.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def html_id @html_id end |
#html_name ⇒ Object (readonly)
Returns the value of attribute html_name.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def html_name @html_name end |
#input_options ⇒ Object (readonly)
Returns the value of attribute input_options.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def @input_options end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def name @name end |
#output_options ⇒ Object (readonly)
Returns the value of attribute output_options.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def @output_options end |
#select_options ⇒ Object (readonly)
Returns the value of attribute select_options.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def @select_options end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
11 12 13 |
# File 'lib/action_form/element.rb', line 11 def @tags end |
Class Method Details
.input(type:, default: nil, **options) ⇒ Object
48 49 50 51 52 |
# File 'lib/action_form/element.rb', line 48 def input(type:, default: nil, **) @default = default @input_options = { type: type }.merge() .merge!(input: type) end |
.input_options ⇒ Object
40 41 42 |
# File 'lib/action_form/element.rb', line 40 def @input_options ||= {} end |
.label(text: nil, display: true, **html_options) ⇒ Object
64 65 66 |
# File 'lib/action_form/element.rb', line 64 def label(text: nil, display: true, **) @label_options = [{ text: text, display: display }, ] end |
.label_options ⇒ Object
28 29 30 |
# File 'lib/action_form/element.rb', line 28 def @label_options ||= [{ text: nil, display: true }, {}] end |
.options(collection) ⇒ Object
59 60 61 62 |
# File 'lib/action_form/element.rb', line 59 def (collection) @select_options = collection .merge!(options: true) end |
.output(type:, **options) ⇒ Object
54 55 56 57 |
# File 'lib/action_form/element.rb', line 54 def output(type:, **) @output_options = { type: type }.merge() .merge!(output: type) end |
.output_options ⇒ Object
36 37 38 |
# File 'lib/action_form/element.rb', line 36 def @output_options ||= {} end |
.select_options ⇒ Object
32 33 34 |
# File 'lib/action_form/element.rb', line 32 def @select_options ||= [] end |
.tags(**extra_tags) ⇒ Object
68 69 70 71 |
# File 'lib/action_form/element.rb', line 68 def (**) = .delete_if { |key, _| PROTECTED_TAGS.include?(key) } .merge!() end |
.tags_list ⇒ Object
44 45 46 |
# File 'lib/action_form/element.rb', line 44 def @tags_list ||= {} end |
Instance Method Details
#detached? ⇒ Boolean
125 126 127 |
# File 'lib/action_form/element.rb', line 125 def detached? false end |
#disabled? ⇒ Boolean
129 130 131 |
# File 'lib/action_form/element.rb', line 129 def disabled? false end |
#html_checked ⇒ Object
94 95 96 97 98 |
# File 'lib/action_form/element.rb', line 94 def html_checked return unless input_type == :checkbox value end |
#html_value ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/action_form/element.rb', line 82 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/action_form/element.rb', line 100 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
137 138 139 |
# File 'lib/action_form/element.rb', line 137 def input_type self.class.[:type].to_sym end |
#label_html_attributes ⇒ Object
78 79 80 |
# File 'lib/action_form/element.rb', line 78 def label_html_attributes { for: html_id }.merge(self.class..last) end |
#label_text ⇒ Object
74 75 76 |
# File 'lib/action_form/element.rb', line 74 def label_text self.class..first[:text] || name.to_s.tr("_", " ").capitalize end |
#readonly? ⇒ Boolean
133 134 135 |
# File 'lib/action_form/element.rb', line 133 def readonly? false end |
#render? ⇒ Boolean
121 122 123 |
# File 'lib/action_form/element.rb', line 121 def render? true end |
#value ⇒ Object
115 116 117 118 119 |
# File 'lib/action_form/element.rb', line 115 def value return self.class.default unless object object.public_send(name) || self.class.default end |