Module: Dao::Form::Elements

Included in:
Dao::Form
Defined in:
lib/dao/form.rb

Overview

html generation methods

Instance Method Summary collapse

Instance Method Details

#button(*args, &block) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/dao/form.rb', line 237

def button(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  type = options.delete(:type) || :button
  name = options.delete(:name) || name_for(keys)
  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  value = options.has_key?(:value) ? options.delete(:value) : value_for(attributes, keys)

  content = (block ? block.call : (options.delete(:content) || 'Submit'))

  content = escape_html(content)

  button_(options_for(options, :type => type, :name => name, :value => value, :class => klass, :id => id, :data_error => error)){ content }
end

#checkbox(*args, &block) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/dao/form.rb', line 281

def checkbox(*args, &block)
  options = args.extract_options!.to_options!
  keys = scope(args)

  type = options.delete(:type) || :checkbox
  name = options.delete(:name) || name_for(keys)
  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))
  values = options.delete(:values)

  unless options.has_key?(:checked)
    checked = Coerce.boolean(attributes.get(keys))
    options[:checked] = checked if checked
  end

  value_for =
    case values
      when false, nil
        {true => '1', false => '0'}
      when Hash
        h = {}
        values.map{|k, v| h[ k =~ /t|1|on|yes/ ? true : false ] = v}
        h
      else
        t, f, *ignored = Array(values).flatten.compact
        {true => t, false => f}
    end
  value_for[true] ||= '1'
  value_for[false] ||= '0'

  hidden_options =
    options.dup.tap{|o| o.delete(:checked)}

  tagz{
    input_(options_for(hidden_options, :type => :hidden, :name => name, :value => value_for[false])){}

    __

    input_(
      options_for(
        options,
        :type => :checkbox,
        :name => name,
        :value => value_for[true],
        :class => klass,
        :id => id,
        :data_error => error
      )
    ){}
  }
end

#element(which, *args, &block) ⇒ Object



158
159
160
# File 'lib/dao/form.rb', line 158

def element(which, *args, &block)
  send(which, *args, &block)
end

#form(*args, &block) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dao/form.rb', line 162

def form(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  action = options.delete(:action) || './'
  method = options.delete(:method) || 'post'
  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  content =
    if block.nil? and !options.has_key?(:content)
      ''
    else
      block ? block.call(form=self) : options.delete(:content)
    end

  form_(options_for(options, :action => action, :method => method, :class => klass, :id => id, :data_error => error)){ content }
end

#hidden(*args, &block) ⇒ Object



334
335
336
337
338
339
# File 'lib/dao/form.rb', line 334

def hidden(*args, &block)
  options = args.extract_options!.to_options!
  options[:type] = :hidden
  args.push(options)
  input(*args, &block)
end

#input(*args, &block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/dao/form.rb', line 201

def input(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  type = options.delete(:type) || :text
  name = options.delete(:name) || name_for(keys)
  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  value =
    if block.nil? and !options.has_key?(:value) 
      value_for(attributes, keys)
    else
      block ? block.call(attributes.get(keys)) : options.delete(:value)
    end

  input_(options_for(options, :type => type, :name => name, :value => value, :class => klass, :id => id, :data_error => error)){}
end

#label(*args, &block) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/dao/form.rb', line 182

def label(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  block ||=
    proc do
      options.delete(:content) ||
      options.delete(:value) ||
      keys.map{|key| key.to_s.titleize}.join(' ')
    end

  id = options.delete(:id) || id_for(keys + [:label])
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))
  target = options.delete(:for) || id_for(keys)

  label_(options_for(options, :for => target, :class => klass, :id => id, :data_error => error), &block)
end

#radio_button(*args, &block) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/dao/form.rb', line 256

def radio_button(*args, &block)
  options = args.extract_options!.to_options!
  keys = scope(args)

  type = options.delete(:type) || :radio
  name = options.delete(:name) || name_for(keys)
  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  unless options.has_key?(:checked)
    checked =
      if options.has_key?(:value) and attributes.has?(keys)
        a = attributes.get(keys)
        b = options[:value]
        a==b or a.to_s==b.to_s
      else
        false
      end
    options[:checked] = checked if checked
  end

  input_(options_for(options, :type => :radio, :name => name, :class => klass, :id => id, :data_error => error)){}
end

#reset(*args) ⇒ Object



341
342
343
344
345
346
# File 'lib/dao/form.rb', line 341

def reset(*args)
  options = args.extract_options!.to_options! 
  options[:type] = :reset
  args.push(options)
  button(*args)
end

#select(*args, &block) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/dao/form.rb', line 369

def select(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  name = options.delete(:name) || name_for(keys)
  values = options.delete(:values) || options.delete(:options) || options.delete(:from)

  has_blank = options.has_key?(:blank) && options[:blank] != false
  blank = options.delete(:blank)

  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  if values.nil?
    key = keys.map{|key| "#{ key }"}
    key.last << "_options"
    values = attributes.get(*key) if attributes.has?(*key)
  end

  if options[:multiple]
    name = name.to_s
    name += '[]'
  end

  list = Array(values).map{|value| value.dup rescue value} # ensure list is dup'd

  case list.first
    when Hash, Array
      nil
    else
      list.flatten!
      list.compact!
      list.map!{|element| [element, element]}
  end

  if has_blank
    case blank
      when false
        blank = nil
      when nil, true
        blank = [nil, nil]
      else
        blank = [Array(blank).first, '']
    end
  end

  selected_value =
    if options.has_key?(:selected)
      options.delete(:selected)
    else
      attributes.get(keys)
    end

  selected_values = {}

  Array(selected_value).flatten.compact.each do |val|
    selected_values[val.to_s] = true
  end

  select_(options_for(options, :name => name, :class => klass, :id => id, :data_error => error)){
    if blank
      content = blank.first || ''
      value = blank.last
      value.nil? ? option_(){ content } : option_(:value => value){ content }
    end

    unless list.empty?
      list.each do |pair|
        returned = block ? Dao.call(block, :call, pair.first, pair.last, selected_value) : pair 

        opts = Map.new
        selected = nil

        case returned
          when Array
            content, value, selected, *ignored = returned
            if value.is_a?(Hash)
              map = Map.for(value)
              value = map.delete(:value)
              selected = map.delete(:selected)
              opts.update(map)
            end

          when Hash
            content = returned[:content]
            value = returned[:value]
            selected = returned[:selected]

          else
            content = returned
            value = returned
            selected = nil
        end

        if selected.nil?
          selected = selected_values.has_key?(value.to_s)
        end

        opts[:value] = (value.nil? ? content : value)
        opts[:selected] = Coerce.boolean(selected) if selected

        option_(opts){ content }
      end
    else
      ' '
    end
  }
end

#submit(*args, &block) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/dao/form.rb', line 221

def submit(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  keys.push(:submit) if keys.empty?
  name = options.delete(:name) || name_for(keys)

  content = block ? block.call : (args.first || 'Submit')

  options[:name] ||= name
  options[:type] ||= :submit
  options[:value] ||= content

  input_(options_for(options)){}
end

#textarea(*args, &block) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/dao/form.rb', line 348

def textarea(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  name = options.delete(:name) || name_for(keys)
  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  value =
    if block.nil? and !options.has_key?(:value) 
      value_for(attributes, keys)
    else
      block ? block.call(attributes.get(keys)) : options.delete(:value)
    end

  value = escape_html(value)

  textarea_(options_for(options, :name => name, :class => klass, :id => id, :data_error => error)){ value }
end

#upload(*args, &block) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/dao/form.rb', line 479

def upload(*args, &block)
  options = args.extract_options!.to_options! 
  keys = scope(args)

  cache_key = keys + [:cache]
  file_key = keys + [:file]

  cache_name = options.delete(:cache_name) || name_for(cache_key)
  file_name = options.delete(:file_name) || options.delete(:name) || name_for(file_key)

  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  cache_value = attributes.get(cache_key)

  tagz{
    input_(:name => cache_name, :value => cache_value, :type => :hidden){ }

    __

    input_(options_for(options, :name => file_name, :class => klass, :id => id, :data_error => error, :type => :file)){ }
  }
end