Class: Dao::Form
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
#object ⇒ Object
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
|
#unscoped ⇒ Object
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
564
565
566
567
568
569
570
571
572
573
574
575
|
# File 'lib/dao/form.rb', line 564
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
560
561
562
|
# File 'lib/dao/form.rb', line 560
def Form.name_for(name, *keys)
"#{ prefix_for(name) }[#{ key_for(*keys) }]"
end
|
.prefix_for(name) ⇒ Object
556
557
558
|
# File 'lib/dao/form.rb', line 556
def Form.prefix_for(name)
"dao[#{ name }]"
end
|
Instance Method Details
#attr_for(string) ⇒ Object
640
641
642
|
# File 'lib/dao/form.rb', line 640
def attr_for(string)
slug_for(string).gsub(/_/, '-')
end
|
#class_for(keys, klass = nil) ⇒ Object
518
519
520
521
522
523
524
525
526
|
# File 'lib/dao/form.rb', line 518
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
644
645
646
|
# File 'lib/dao/form.rb', line 644
def data_attr_for(string)
"data-#{ attr_for(string) }"
end
|
#error_for(keys, klass = nil) ⇒ Object
528
529
530
531
532
533
534
|
# File 'lib/dao/form.rb', line 528
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
510
511
512
|
# File 'lib/dao/form.rb', line 510
def errors_on(keys)
errors.get(keys)
end
|
#errors_on?(*keys) ⇒ Boolean
514
515
516
|
# File 'lib/dao/form.rb', line 514
def errors_on?(*keys)
!errors_on(keys).blank?
end
|
#escape_html(string) ⇒ Object
552
553
554
|
# File 'lib/dao/form.rb', line 552
def escape_html(string)
Tagz.escape_html(string)
end
|
#id_for(keys) ⇒ Object
html generation support methods
505
506
507
508
|
# File 'lib/dao/form.rb', line 505
def id_for(keys)
id = [name, keys.join('-')].compact.join('_')
slug_for(id)
end
|
#key_for(*keys) ⇒ Object
577
578
579
|
# File 'lib/dao/form.rb', line 577
def key_for(*keys)
Form.key_for(name, *keys)
end
|
#name_for(*keys) ⇒ Object
581
582
583
|
# File 'lib/dao/form.rb', line 581
def name_for(*keys)
Form.name_for(name, *keys)
end
|
#options_for(*hashes) ⇒ Object
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
# File 'lib/dao/form.rb', line 617
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
648
649
650
651
652
653
654
|
# File 'lib/dao/form.rb', line 648
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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
# File 'lib/dao/form.rb', line 536
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
|