Module: BootstrapFormHelper

Defined in:
app/helpers/bootstrap_form_helper.rb

Instance Method Summary collapse

Instance Method Details

#control_group(form, field, opts = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/bootstrap_form_helper.rb', line 3

def control_group form, field, opts={}, &block
  errors = opts.delete(:errors)
  label = opts.delete(:label) || field_name(field)
  (:div, :class=>"control-group") do
    concat (:label, label, :class=>"control-label")
    concat((:div, :class=>"controls"){
      if(block_given?)
       concat yield(form, field, opts)
      else
       concat form_field(form, field, opts)
      end
      concat (:span, errors.join(","), :class=>"help-inline error-help") if (errors && errors.count >0)
    })
  end
end

#field_of(form, field) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/bootstrap_form_helper.rb', line 67

def field_of form, field

  nested_fields = field.to_s.split(".")
  if (nested_fields.size == 2)
    form.fields_for(nested_fields[0].to_sym) {|fields|
      yield(fields, nested_fields[1].to_sym)
    }
  else
    yield(form, field)
  end
end

#form_action(form, opts, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/bootstrap_form_helper.rb', line 19

def form_action form, opts, &block
  (:div, :class=>"form-actions") do
    if block_given?
      concat yield(form, opts)
    else
      submit_label = opts.delete(:submit)
      concat form.submit(submit_label, :class=>'btn btn-primary')

      cancel = opts.delete(:cancel)
      if (cancel)
        label = cancel[0]
        url = cancel[1]
        concat (:span, "OR")
        concat link_to(label, url, :class=>"cancel")
      end
    end
  end
end

#form_field(form, field, opts) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/bootstrap_form_helper.rb', line 38

def form_field form, field, opts
  type = opts.delete :type
  raise "No type defined for #{field} field" if type.nil?

  cls =  opts.delete(:class)
  size = opts.delete(:size)
  help = opts.delete(:help)

  opts = opts.merge(:class=>[cls, size].compact.join(" "))
  opts = opts.merge(:title=>help, :"data-toggle"=>"tooltip", :"data-trigger"=>"focus", :"data-placement"=>"bottom") if help.present?

  field_of(form, field) do |f, field|
    case type
    when :select
      f.select(field, selection_values(opts), opts)

    when :radio_button
      render_radio_button(f, field, selection_values(opts))

    when :check_box
      render_check_box(f, field, selection_values(opts))  

    else
      f.send(type.to_sym, field, opts)
    end
  end
end

#render_check_box(form, field, selection) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'app/helpers/bootstrap_form_helper.rb', line 86

def render_check_box form, field, selection
  check_boxes = selection.collect do |item|
    (:label, :class=>"checkbox") do
      concat form.check_box(field, {}, item[1], nil)
      concat item[0]
    end
  end
  check_boxes.join("\n").html_safe
end

#render_fileupload(file_field, extra = nil) ⇒ Object



81
82
83
84
# File 'app/helpers/bootstrap_form_helper.rb', line 81

def render_fileupload(file_field, extra=nil)
  extra ||= ""
  render :partial=>"components/bootstrap/fileupload", :locals=>{file_field: file_field, extra:extra}
end

#render_radio_button(form, field, selection) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'app/helpers/bootstrap_form_helper.rb', line 96

def render_radio_button form, field, selection
  radio_buttons = selection.collect do |item|
    (:label, :class=>"radio") do
      concat form.radio_button(field, item[1])
      concat item[0]
    end
  end
  radio_buttons.join("\n").html_safe
end