Module: DohRails::FormHelpers

Defined in:
lib/doh/rails/form_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_id(options) ⇒ Object



12
13
14
# File 'lib/doh/rails/form_helpers.rb', line 12

def self.get_id(options)
  options[:name] ? options[:name] : "obj_#{options.hash}"
end

.get_label_html(options) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/doh/rails/form_helpers.rb', line 4

def self.get_label_html(options)
  if options.key?(:label)
    label_html = "<label for='#{DohRails::FormHelpers::get_id(options)}'>#{options[:label]}</label>"
    options.delete(:label)
  end
  return label_html
end

.option_str(options) ⇒ Object



16
17
18
# File 'lib/doh/rails/form_helpers.rb', line 16

def self.option_str(options)
  options.to_a.collect{|key, value| "#{key}='#{value}'"}.join(' ')
end

Instance Method Details

#doh_checkbox(options) ⇒ Object



33
34
35
# File 'lib/doh/rails/form_helpers.rb', line 33

def doh_checkbox(options)
  doh_field('checkbox', options)
end

#doh_field(type, options) ⇒ Object



20
21
22
23
# File 'lib/doh/rails/form_helpers.rb', line 20

def doh_field(type, options)
  label_html = DohRails::FormHelpers::get_label_html(options)
  "#{label_html}<input id='#{DohRails::FormHelpers::get_id(options)}' type='#{type}' #{DohRails::FormHelpers::option_str(options)}"
end

#doh_hidden_field(options) ⇒ Object



29
30
31
# File 'lib/doh/rails/form_helpers.rb', line 29

def doh_hidden_field(options)
  doh_field('hidden', options)
end

#doh_select(select_array, options = {}) ⇒ Object

kjmtodo modify to set default value based on model / field name



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/doh/rails/form_helpers.rb', line 38

def doh_select(select_array, options = {})
  label_html = DohRails::FormHelpers::get_label_html(options)
  result = ''
  result += label_html
  result += "<select id='#{DohRails::FormHelpers::get_id(options)}' #{DohRails::FormHelpers::option_str(options)}>\n"
  select_list = select_array.collect do |elem|
    "<option value=\"#{elem[0]}\">#{elem[1]}</option>"
  end
  result += select_list.join("\n")
  result += "</select>"
  result
end

#doh_text_field(options) ⇒ Object



25
26
27
# File 'lib/doh/rails/form_helpers.rb', line 25

def doh_text_field(options)
  doh_field('text', options)
end