Class: Dao::Form

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

Direct Known Subclasses

Builder

Defined Under Namespace

Modules: Elements Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Elements

#button, #checkbox, #element, #form, #hidden, #input, #label, #radio_button, #reset, #select, #submit, #textarea, #upload

Constructor Details

#initialize(*args) ⇒ Form

Returns a new instance of Form.



44
45
46
47
48
# File 'lib/dao/form.rb', line 44

def initialize(*args)
  @object = args.shift
  @unscoped = Map.new
  @scope = []
end

Instance Attribute Details

#objectObject

instance methods



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

def object
  @object
end

#unscopedObject

Returns the value of attribute unscoped.



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

def unscoped
  @unscoped
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



566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/dao/form.rb', line 566

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



562
563
564
# File 'lib/dao/form.rb', line 562

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

.prefix_for(name) ⇒ Object



558
559
560
# File 'lib/dao/form.rb', line 558

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

Instance Method Details

#attr_for(string) ⇒ Object



642
643
644
# File 'lib/dao/form.rb', line 642

def attr_for(string)
  slug_for(string)
end

#capture(*args, &block) ⇒ Object



662
663
664
# File 'lib/dao/form.rb', line 662

def capture(*args, &block)
  tagz(*args, &block)
end

#class_for(keys, klass = nil) ⇒ Object



520
521
522
523
524
525
526
527
528
# File 'lib/dao/form.rb', line 520

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



646
647
648
# File 'lib/dao/form.rb', line 646

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

#error_for(keys, klass = nil) ⇒ Object



530
531
532
533
534
535
536
# File 'lib/dao/form.rb', line 530

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



512
513
514
# File 'lib/dao/form.rb', line 512

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

#errors_on?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


516
517
518
# File 'lib/dao/form.rb', line 516

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

#escape_html(string) ⇒ Object



554
555
556
# File 'lib/dao/form.rb', line 554

def escape_html(string)
  Tagz.escape_html(string)
end

#id_for(keys) ⇒ Object

html generation support methods



507
508
509
510
# File 'lib/dao/form.rb', line 507

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

#key_for(*keys) ⇒ Object



579
580
581
# File 'lib/dao/form.rb', line 579

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

#name_for(*keys) ⇒ Object



583
584
585
# File 'lib/dao/form.rb', line 583

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

#options_for(*hashes) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/dao/form.rb', line 619

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

#scope(*keys, &block) ⇒ Object Also known as: scope_for



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/dao/form.rb', line 587

def scope(*keys, &block)
  if block.nil?
    return [@scope, *keys].flatten.compact
  end

  #attributes = self.attributes
  #errors = self.errors

  scope = @scope
  @scope = Coerce.list_of_strings(keys)

  #@attributes = Map.for(attributes.get(*@scope))
  #@errors = Errors.new.tap{|e| e.update(errors.get(*@scope))}

#p '@scope' => @scope
#p '@errors' => @errors
#p '@attributes' => @attributes
#p 'errors' => errors
#p 'attributes' => attributes
#puts
#abort
  begin
    argv = block.arity == 0 ? [@scope] : []
    block.call(*argv)
  ensure
    @scope = scope
    #@attributes = attributes
    #@errors = errors
  end
end

#slug_for(string) ⇒ Object



650
651
652
653
654
# File 'lib/dao/form.rb', line 650

def slug_for(string)
  string = string.to_s
  words = string.scan(%r/[^\s]+/)
  words.join('--').downcase
end

#titleize(string) ⇒ Object



656
657
658
659
660
# File 'lib/dao/form.rb', line 656

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

#value_for(map, keys) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/dao/form.rb', line 538

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