Class: ActionForm::Element

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

Class Method Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#errors_messagesObject (readonly)

Returns the value of attribute errors_messages.



7
8
9
# File 'lib/action_form/element.rb', line 7

def errors_messages
  @errors_messages
end

#helpersObject

Returns the value of attribute helpers.



8
9
10
# File 'lib/action_form/element.rb', line 8

def helpers
  @helpers
end

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

Returns the value of attribute input_options.



7
8
9
# File 'lib/action_form/element.rb', line 7

def input_options
  @input_options
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/action_form/element.rb', line 7

def name
  @name
end

#output_optionsObject (readonly)

Returns the value of attribute output_options.



7
8
9
# File 'lib/action_form/element.rb', line 7

def output_options
  @output_options
end

#select_optionsObject (readonly)

Returns the value of attribute select_options.



7
8
9
# File 'lib/action_form/element.rb', line 7

def select_options
  @select_options
end

#tagsObject (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/action_form/element.rb', line 7

def tags
  @tags
end

Class Method Details

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



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

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

.input_optionsObject



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

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

.label_optionsObject



21
22
23
# File 'lib/action_form/element.rb', line 21

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

.options(collection) ⇒ Object



51
52
53
54
# File 'lib/action_form/element.rb', line 51

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

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



46
47
48
49
# File 'lib/action_form/element.rb', line 46

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

.output_optionsObject



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

def output_options
  @output_options ||= {}
end

.select_optionsObject



25
26
27
# File 'lib/action_form/element.rb', line 25

def select_options
  @select_options ||= []
end

.tags(**tags_list) ⇒ Object



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

def tags(**tags_list)
  tags_list.merge!(tags_list)
end

.tags_listObject



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

def tags_list
  @tags_list ||= {}
end

Instance Method Details

#detached?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/action_form/element.rb', line 116

def detached?
  false
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  false
end

#html_checkedObject



85
86
87
88
89
# File 'lib/action_form/element.rb', line 85

def html_checked
  return unless input_type == :checkbox

  value
end

#html_valueObject



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



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



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

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

#label_html_attributesObject



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

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

#label_textObject



65
66
67
# File 'lib/action_form/element.rb', line 65

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

#readonly?Boolean

Returns:

  • (Boolean)


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

def readonly?
  false
end

#render?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/action_form/element.rb', line 112

def render?
  true
end

#valueObject



106
107
108
109
110
# File 'lib/action_form/element.rb', line 106

def value
  return unless object

  object.public_send(name)
end