Class: Roda::Component::Form
- Inherits:
-
Object
- Object
- Roda::Component::Form
show all
- Includes:
- Validations
- Defined in:
- lib/roda/component/form.rb,
lib/roda/component/form/validations.rb
Defined Under Namespace
Modules: Validations
Classes: Attributes
Class Method Summary
collapse
Instance Method Summary
collapse
#errors, #valid?, #validate
Constructor Details
#initialize(atts, options = {}) ⇒ Form
Initialize with a hash of attributes and values. If extra attributes are sent, a NoMethodError exception will be raised.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/roda/component/form.rb', line 71
def initialize(atts, options = {})
@_data = atts
@_data = atts.to_obj if atts.is_a? Hash
@_options = options
@_attributes = Attributes.new
@_attributes.set_attr_accessors _attr_accessors
@_attributes.set_values _data
_form.each do |key, klass|
opts = {}
opts[key] = _data.send(key) if _data.respond_to?(key)
@_attributes.set_values opts
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/roda/component/form.rb', line 106
def method_missing method, *args, &block
if _attributes.respond_to? method, true
_attributes.send method, *args, &block
else
super
end
end
|
Class Method Details
._attr_accessors ⇒ Object
216
217
218
|
# File 'lib/roda/component/form.rb', line 216
def self._attr_accessors
@_attr_accessors
end
|
220
221
222
|
# File 'lib/roda/component/form.rb', line 220
def self._form
@_form
end
|
.attr_accessor(*vars) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/roda/component/form.rb', line 88
def self.attr_accessor(*vars)
@_attr_accessors ||= []
@_form ||= {}
vars.each do |v|
if !v.is_a? Hash
@_attr_accessors << v unless @_attr_accessors.include? v
else
v = v.first
unless @_attr_accessors.include? v.first
@_attr_accessors << v.first
@_form[v.first] = v.last
end
end
end
end
|
Instance Method Details
#attributes ⇒ Object
Return hash of attributes and values.
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/roda/component/form.rb', line 116
def attributes
Hash.new.tap do |atts|
_attributes.instance_variables.each do |ivar|
next if ivar == :@constructor || ivar == :@toString || ivar == :@_attributes || ivar == :@_data || ivar == :@_forms
att = ivar[1..-1].to_sym
atts[att] = _attributes.send(att)
end
end
end
|
#display_errors(options = {}) ⇒ Object
Also known as:
render_errors
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/roda/component/form.rb', line 136
def display_errors options = {}
d_errors = errors
if override_errors = options[:override_errors]
d_errors = override_errors
end
keys = options.delete(:keys) || (_options[:key] ? [_options[:key]] : [])
if = options.delete(:errors)
.each do |key, value|
d_errors[key] = value
end
end
d_errors.each do |key, error|
d_keys = (keys.dup << key)
error = error.first
if error.is_a? Hash
d_options = options.dup
d_options[:keys] = d_keys
d_options[:override_errors] = d_errors[key].first
display_errors d_options
else
name = d_keys.each_with_index.map do |field, i|
i != 0 ? "[#{field}]" : field
end.join
field_error_dom = options.delete :tmpl
field_error_dom = DOM.new('<span class="field-error"><span>') unless field_error_dom
field_error_dom.html _error_name(key, error)
field = _dom.find("input[name='#{name}']")
field.before field_error_dom.dom
end
end
end
|
#render_values(dom = false, key = false, data = false) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/roda/component/form.rb', line 178
def render_values dom = false, key = false, data = false
dom = _options[:dom] unless dom
key = _options[:key] if !key && _options.key?(:key)
dom.find('input, select') do |element|
name = element['name']
name = name.gsub(/\A#{key}/, '') if key
keys = name.gsub(/\A\[/, '').gsub(/[^a-z0-9_]/, '|').gsub(/\|\|/, '|').gsub(/\|$/, '').split('|')
value = false
keys.each do |k|
begin
value = value ? value.send(k) : send(k)
if klass = _form[k.to_s.to_sym]
options = {}
options[:key] = _options[:key] if _options.key? :key
value = klass.new(value, options)
end
rescue
value = ''
end
end
case element.name
when 'select'
element.find('option') do |x|
x['selected'] = true if x['value'] == value.to_s
end
when 'input'
element['value'] = value.to_s
end
end
end
|
#slice(*keys) ⇒ Object
128
129
130
131
132
133
134
|
# File 'lib/roda/component/form.rb', line 128
def slice(*keys)
Hash.new.tap do |atts|
keys.each do |att|
atts[att] = _attributes.send(att)
end
end
end
|