Module: Formbuilder

Defined in:
lib/formbuilder.rb,
lib/formbuilder/version.rb

Defined Under Namespace

Modules: Rails

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Class Method Details

.build_checkbox(f) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/formbuilder.rb', line 34

def build_checkbox(f)
  s = "<label class='checkbox'>"
  s += f['required'] == 'true' ? "<abbr class='required'>*</abbr>" : '' 
  s += "<input type='checkbox'#{ f['baseline'] == 'true' ? " checked='checked'" : '' } value='1' name='custom_fields[#{nameize(f['title'])}]'> #{f['title']}"
  s += "<input type='hidden' value='0' name='custom_fields[#{nameize(f['title'])}]'>"
  s += '</label>'
end

.build_field(index, field) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/formbuilder.rb', line 9

def build_field(index, field)
  case field['cssClass']
  when 'input_text'
    @html += build_text_input(field)
  when 'textarea'
    @html += build_textarea(field)
  when 'checkbox'
    @html += build_checkbox(field)
  when 'radio'
    @html += build_radio(field)
  when 'select'
    @html += build_select(field)
  end
end

.build_radio(f) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/formbuilder.rb', line 42

def build_radio(f)
  s = "<label>#{f['title']}</label>"
  f['values'].each do |v|
    attrs = v[1]
    s += "<label class='radio'>"
    s += "<input type='radio'#{ attrs['baseline'] == 'true' ? " checked='checked'" : '' } value='#{attrs['value']}' name='custom_fields[#{nameize(f['title'])}]'>  #{ attrs['value'] }"
    s += "</label>"
  end
  s
end

.build_select(f) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formbuilder.rb', line 53

def build_select(f)
  s = "<label>#{f['title']}</label>"
  if f['multiple'] == 'true'
    s += "<select multiple='multiple' name='custom_fields[#{nameize(f['title'])}]'>"
  else
    s += "<select name='custom_fields[#{nameize(f['title'])}]'>"
  end
  f['values'].each do |v|
    attrs = v[1]
    if attrs['baseline'] == 'true'
      s += "<option value='#{attrs['value']}' selected='selected'>"
    else
      s += "<option value='#{attrs['value']}'>"
    end
    s += attrs['value']
    s += "</option>"
  end
  s += "</select>"
end

.build_text_input(f) ⇒ Object



24
25
26
27
# File 'lib/formbuilder.rb', line 24

def build_text_input(f)
  s = f['required'] == 'true' ? "<label><abbr title='required'>*</abbr>#{f['title']}</label>" : "<label>#{f['title']}</label>"
  s += "<input type='text' name='custom_fields[#{nameize(f['title'])}]' />"
end

.build_textarea(f) ⇒ Object



29
30
31
32
# File 'lib/formbuilder.rb', line 29

def build_textarea(f)
  s = f['required'] == 'true' ? "<label><abbr title='required'>*</abbr>#{f['title']}</label>" : "<label>#{f['title']}</label>"
  s += "<textarea name='custom_fields[#{nameize(f['title'])}]'></textarea>"
end

.generate_html(data) ⇒ Object



3
4
5
6
7
# File 'lib/formbuilder.rb', line 3

def generate_html(data)
  @html = ''
  JSON.parse(data).each { |field| build_field(field[0], field[1]) }
  return @html
end

.nameize(string) ⇒ Object



73
74
75
# File 'lib/formbuilder.rb', line 73

def nameize(string)
  string.gsub(/\s+/, "_").downcase rescue nil
end