Class: Dao::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/dao/form.rb

Direct Known Subclasses

Builder

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Form

Returns a new instance of Form.



43
44
45
# File 'lib/dao/form.rb', line 43

def initialize(*args)
  @object = args.shift
end

Instance Attribute Details

#objectObject

instance methods



41
42
43
# File 'lib/dao/form.rb', line 41

def object
  @object
end

Class Method Details

.for(*args, &block) ⇒ Object



34
35
36
# File 'lib/dao/form.rb', line 34

def for(*args, &block)
  new(*args, &block)
end

.key_for(*keys) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/dao/form.rb', line 527

def Form.key_for(*keys)
  keys.flatten.compact.map do |key|
    case
      when Integer === key
        key
      when key =~ /^\d+$/
        "~#{ key }"
      else
        key
    end
  end.join('.')
end

.name_for(name, *keys) ⇒ Object



523
524
525
# File 'lib/dao/form.rb', line 523

def Form.name_for(name, *keys)
  "#{ prefix_for(name) }[#{ key_for(*keys) }]"
end

.prefix_for(name) ⇒ Object



519
520
521
# File 'lib/dao/form.rb', line 519

def Form.prefix_for(name)
  "dao[#{ name }]"
end

Instance Method Details

#attr_for(string) ⇒ Object



571
572
573
# File 'lib/dao/form.rb', line 571

def attr_for(string)
  slug_for(string).gsub(/_/, '-')
end

#button(*args, &block) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dao/form.rb', line 213

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

  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'))

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

#checkbox(*args, &block) ⇒ Object



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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/dao/form.rb', line 255

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

  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) || options.delete(:checked)

  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

#class_for(keys, klass = nil) ⇒ Object



488
489
490
491
492
493
494
495
496
# File 'lib/dao/form.rb', line 488

def class_for(keys, klass = nil)
  klass = 
    if errors_on?(keys)
      [klass, 'dao', 'errors'].compact.join(' ')
    else
      [klass, 'dao'].compact.join(' ')
    end
  klass
end

#data_attr_for(string) ⇒ Object



575
576
577
# File 'lib/dao/form.rb', line 575

def data_attr_for(string)
  "data-#{ attr_for(string) }"
end

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

html generation methods



138
139
140
# File 'lib/dao/form.rb', line 138

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

#error_for(keys, klass = nil) ⇒ Object



498
499
500
501
502
503
504
# File 'lib/dao/form.rb', line 498

def error_for(keys, klass = nil)
  if errors_on?(keys)
    title = Array(keys).join(' ').titleize
    messages = Array(errors.get(keys)).join(', ')
    "#{ title }: #{ messages }"
  end
end

#errors_on(keys) ⇒ Object



480
481
482
# File 'lib/dao/form.rb', line 480

def errors_on(keys)
  errors.get(keys)
end

#errors_on?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


484
485
486
# File 'lib/dao/form.rb', line 484

def errors_on?(*keys)
  !errors_on(keys).blank?
end

#form(*args, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dao/form.rb', line 142

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

  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



308
309
310
311
312
313
# File 'lib/dao/form.rb', line 308

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

#id_for(keys) ⇒ Object

html generation support methods



475
476
477
478
# File 'lib/dao/form.rb', line 475

def id_for(keys)
  id = [name, keys.join('-')].compact.join('_')
  slug_for(id)
end

#input(*args, &block) ⇒ Object



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

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

  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

#key_for(*keys) ⇒ Object



540
541
542
# File 'lib/dao/form.rb', line 540

def key_for(*keys)
  Form.key_for(name, *keys)
end

#label(*args, &block) ⇒ Object



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

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

  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

#name_for(*keys) ⇒ Object



544
545
546
# File 'lib/dao/form.rb', line 544

def name_for(*keys)
  Form.name_for(name, *keys)
end

#options_for(*hashes) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/dao/form.rb', line 548

def options_for(*hashes)
  map = Map.new

  hashes.flatten.each do |h|
    case((data = h.delete(:data) || h.delete('data')))
      when Hash
        data.each{|k,v| map[data_attr_for(k)] = v unless v.nil?}
      else
        h[:data] = data
    end

    h.each do |k,v|
      map[attr_for(k)] = v unless v.nil?
    end
  end

  %w( readonly disabled autofocus checked multiple ).each do |attr|
    map.delete(attr) unless Coerce.boolean(map[attr])
  end

  map
end

#radio_button(*args, &block) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/dao/form.rb', line 230

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

  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



315
316
317
318
319
320
# File 'lib/dao/form.rb', line 315

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

#select(*args, &block) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
# File 'lib/dao/form.rb', line 341

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

  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 += '[]'
  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
    end
  }
end

#slug_for(string) ⇒ Object



579
580
581
582
583
584
585
# File 'lib/dao/form.rb', line 579

def slug_for(string)
  string = string.to_s
  words = string.to_s.scan(%r/\w+/)
  words.map!{|word| word.gsub(%r/[^0-9a-zA-Z_:-]/, '')}
  words.delete_if{|word| word.nil? or word.strip.empty?}
  words.join('-').downcase.sub(/_+$/, '')
end

#submit(*args, &block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/dao/form.rb', line 201

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

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

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

  input_(options_for(options)){}
end

#textarea(*args, &block) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/dao/form.rb', line 322

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

  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

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

#titleize(string) ⇒ Object



587
588
589
590
591
# File 'lib/dao/form.rb', line 587

def titleize(string)
  string = string.to_s
  string = string.titleize if string.respond_to?(:titleize)
  string
end

#upload(*args, &block) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/dao/form.rb', line 448

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

  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

#value_for(map, keys) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/dao/form.rb', line 506

def value_for(map, keys)
  return nil unless map.has?(keys)
  value = map.get(keys)
  value =
    case value
      when Hash, Array
        value.to_json
      else
        value
    end
  value.respond_to?(:html_safe) ? value : Tagz.escapeHTML(value)
end