Class: Wedge::Plugins::Form
Defined Under Namespace
Modules: Delegates, InstanceMethods, Validations
Classes: Attributes
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Delegates
_delegates
client?, #client?, #errors, #server?, server?, #valid?, #validate
Methods included from Methods
#client?, included, #server?
Methods inherited from Component
config, dom, html, html!, method_missing, set_dom, store, tmpl, #to_js, #wedge, wedge_config, wedge_dom, #wedge_dom, #wedge_from_client?, #wedge_from_server?, #wedge_function, wedge_html, #wedge_html, #wedge_javascript, wedge_name, wedge_new, wedge_on, wedge_on_server, #wedge_plugin, #wedge_scope, #wedge_store, #wedge_tmpl, wedge_tmpl, #wedge_trigger
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.
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/wedge/plugins/form.rb', line 91
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
_data.each do |key, val|
send("#{key}=", val)
end
_form.each do |key, form_name|
opts = {}
if _data.respond_to?(key)
opts[key] = wedge(form_name, _data.send(key))
end
@_attributes.set_values opts
send("#{key}=", opts[key])
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/wedge/plugins/form.rb', line 136
def method_missing method, *args, &block
if _data.respond_to? method, true
_data.send method, *args, &block
else
return if method[/\=\z/]
super
end
end
|
Class Method Details
.attr_accessor(*vars) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/wedge/plugins/form.rb', line 116
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
_delegates(*_attr_accessors)
end
|
Instance Method Details
#_attributes ⇒ Object
288
289
290
|
# File 'lib/wedge/plugins/form.rb', line 288
def _attributes
@_attributes ||= {}
end
|
#attributes ⇒ Object
Return hash of attributes and values.
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/wedge/plugins/form.rb', line 148
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)
if form_name = _form[att.to_s.to_sym]
atts[att] = wedge(form_name, atts[att]).attributes
end
end
end
end
|
#display_errors(options = {}, &block) ⇒ Object
Also known as:
render_errors
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/wedge/plugins/form.rb', line 196
def display_errors options = {}, &block
dom = options.delete(:dom) || _dom
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, &block
elsif !block_given? || block.call(d_keys, error) == false
name = d_keys.each_with_index.map do |field, i|
i != 0 ? "[#{field}]" : field
end.join
if tmpl = options[:tmpl]
if client?
field_error_dom = DOM.new(`#{tmpl.dom}[0].outerHTML`)
else
field_error_dom = DOM.new(tmpl.dom.to_html)
end
else
field_error_dom = DOM.new('<span class="field-error"><span>')
end
field_error_dom.html _error_name(key, error)
field = dom.find("[name='#{name}']")
field.before field_error_dom.dom
end
end
end
|
#model_attributes(data = attributes) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/wedge/plugins/form.rb', line 164
def model_attributes data = attributes
hash = {}
data.each do |k, v|
if form_name = _form[k.to_s.to_sym]
d = data[k]
d = d.attributes if d.is_a?(Form)
f = wedge(form_name, d)
k = "#{k}_attributes"
dt = f.model_attributes
hash[k] = model_attributes dt
elsif v.is_a? Hash
hash[k] = model_attributes data[k]
else
hash[k] = v
end
end
hash
end
|
#render_values(dom = false, key = false, data = false) ⇒ Object
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/wedge/plugins/form.rb', line 247
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, textarea') do |element|
name = element['name']
next if name.nil?
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 != false ? value.send(k) : send(k)
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'
if %w(radio checkbox).include? element['type']
if element['value'] == value.to_s
element['checked'] = true
else
element.delete 'checked'
end
else
value = sprintf('%.2f', value) if value.is_a? BigDecimal
element['value'] = value.to_s
end
when 'textarea'
element.val value.to_s
end
end
end
|
#slice(*keys) ⇒ Object
187
188
189
190
191
192
193
194
|
# File 'lib/wedge/plugins/form.rb', line 187
def slice(*keys)
Hash.new.tap do |atts|
keys.each do |att|
atts[att] = send(att)
end
end
end
|
#validate_msg(error, column) ⇒ Object
292
293
294
|
# File 'lib/wedge/plugins/form.rb', line 292
def validate_msg error, column
false
end
|