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.



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

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

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

Returns the value of attribute scope.



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

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



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

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



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

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

.prefix_for(name) ⇒ Object



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

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

Instance Method Details

#attr_for(string) ⇒ Object



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

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

#capture(*args, &block) ⇒ Object



665
666
667
# File 'lib/dao/form.rb', line 665

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

#class_for(keys, klass = nil) ⇒ Object



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

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



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

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

#error_for(keys, klass = nil) ⇒ Object



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

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



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

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

#errors_on?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#escape_html(string) ⇒ Object



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

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

#id_for(keys) ⇒ Object

html generation support methods



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

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

#key_for(*keys) ⇒ Object



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

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

#name_for(*keys) ⇒ Object



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

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

#options_for(*hashes) ⇒ Object



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

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

#slug_for(string) ⇒ Object



651
652
653
654
655
656
657
# File 'lib/dao/form.rb', line 651

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

#titleize(string) ⇒ Object



659
660
661
662
663
# File 'lib/dao/form.rb', line 659

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

#value_for(map, keys) ⇒ Object



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

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