Class: ActionForm::Element

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composition

included

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.tags_list.dup
  @errors_messages = extract_errors_messages(object, name)
  tags.merge!(errors: errors_messages.any?)
  @owner = owner
end

Class Attribute Details

.defaultObject (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_messagesObject (readonly)

Returns the value of attribute errors_messages.



11
12
13
# File 'lib/action_form/element.rb', line 11

def errors_messages
  @errors_messages
end

#helpersObject

Returns the value of attribute helpers.



12
13
14
# File 'lib/action_form/element.rb', line 12

def helpers
  @helpers
end

#html_idObject (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_nameObject (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_optionsObject (readonly)

Returns the value of attribute input_options.



11
12
13
# File 'lib/action_form/element.rb', line 11

def input_options
  @input_options
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/action_form/element.rb', line 11

def name
  @name
end

#output_optionsObject (readonly)

Returns the value of attribute output_options.



11
12
13
# File 'lib/action_form/element.rb', line 11

def output_options
  @output_options
end

#select_optionsObject (readonly)

Returns the value of attribute select_options.



11
12
13
# File 'lib/action_form/element.rb', line 11

def select_options
  @select_options
end

#tagsObject (readonly)

Returns the value of attribute tags.



11
12
13
# File 'lib/action_form/element.rb', line 11

def tags
  @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, **options)
  @default = default
  @input_options = { type: type }.merge(options)
  tags_list.merge!(input: type)
end

.input_optionsObject



40
41
42
# File 'lib/action_form/element.rb', line 40

def input_options
  @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, **html_options)
  @label_options = [{ text: text, display: display }, html_options]
end

.label_optionsObject



28
29
30
# File 'lib/action_form/element.rb', line 28

def label_options
  @label_options ||= [{ text: nil, display: true }, {}]
end

.options(collection) ⇒ Object



59
60
61
62
# File 'lib/action_form/element.rb', line 59

def options(collection)
  @select_options = collection
  tags_list.merge!(options: true)
end

.output(type:, **options) ⇒ Object



54
55
56
57
# File 'lib/action_form/element.rb', line 54

def output(type:, **options)
  @output_options = { type: type }.merge(options)
  tags_list.merge!(output: type)
end

.output_optionsObject



36
37
38
# File 'lib/action_form/element.rb', line 36

def output_options
  @output_options ||= {}
end

.select_optionsObject



32
33
34
# File 'lib/action_form/element.rb', line 32

def select_options
  @select_options ||= []
end

.tags(**extra_tags) ⇒ Object



68
69
70
71
# File 'lib/action_form/element.rb', line 68

def tags(**extra_tags)
  filtered_tags = extra_tags.delete_if { |key, _| PROTECTED_TAGS.include?(key) }
  tags_list.merge!(filtered_tags)
end

.tags_listObject



44
45
46
# File 'lib/action_form/element.rb', line 44

def tags_list
  @tags_list ||= {}
end

Instance Method Details

#detached?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/action_form/element.rb', line 125

def detached?
  false
end

#disabled?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/action_form/element.rb', line 129

def disabled?
  false
end

#html_checkedObject



94
95
96
97
98
# File 'lib/action_form/element.rb', line 94

def html_checked
  return unless input_type == :checkbox

  value
end

#html_valueObject



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.input_options[:value]
  elsif object.is_a?(EasyParams::Base)
    object.public_send(name)
  else
    value.to_s
  end
end

#input_html_attributesObject

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.input_options.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_typeObject



137
138
139
# File 'lib/action_form/element.rb', line 137

def input_type
  self.class.input_options[:type].to_sym
end

#label_html_attributesObject



78
79
80
# File 'lib/action_form/element.rb', line 78

def label_html_attributes
  { for: html_id }.merge(self.class.label_options.last)
end

#label_textObject



74
75
76
# File 'lib/action_form/element.rb', line 74

def label_text
  self.class.label_options.first[:text] || name.to_s.tr("_", " ").capitalize
end

#readonly?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/action_form/element.rb', line 133

def readonly?
  false
end

#render?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/action_form/element.rb', line 121

def render?
  true
end

#valueObject



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