Module: Padrino::Helpers::FormHelpers

Defined in:
lib/padrino-fields/form_helpers.rb

Instance Method Summary collapse

Instance Method Details

#blank_option(prompt) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/padrino-fields/form_helpers.rb', line 51

def blank_option(prompt)
  if prompt
    case prompt.class.to_s
    when 'String' ; (:option, prompt, :value => '') ;
    when 'Array'  ; (:option, prompt.first, :value => prompt.last) ;
    else          ; (:option, '', :value => '') ;
    end
  end
end

#grouped_options_for_select(collection, selected = nil, prompt = false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/padrino-fields/form_helpers.rb', line 35

def grouped_options_for_select(collection,selected=nil,prompt=false)
  if collection.is_a?(Hash)
    collection.map do |key, value|
       :optgroup, :label => key do
        options_for_select(value, selected)
      end
    end
  elsif collection.is_a?(Array)
    collection.map do |optgroup|
       :optgroup, :label => optgroup.first do
        options_for_select(optgroup.last, selected)
      end
    end
  end
end

#select_tag(name, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/padrino-fields/form_helpers.rb', line 21

def select_tag(name, options={})
  options.reverse_merge!(:name => name)
  collection, fields = options.delete(:collection), options.delete(:fields)
  options[:options] = options_from_collection(collection, fields) if collection
  prompt = options.delete(:include_blank)
  select_options_html = if options[:options]
    options_for_select(options.delete(:options), options.delete(:selected))
  elsif options[:grouped_options]
    grouped_options_for_select(options.delete(:grouped_options), options.delete(:selected), prompt)
  end.unshift(blank_option(prompt))
  options.merge!(:name => "#{options[:name]}[]") if options[:multiple]
  (:select, select_options_html, options)
end