Class: BootstrapFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers
Defined in:
app/helpers/bootstrap_form_builder.rb

Direct Known Subclasses

HorizontalBootstrapFormBuilder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_tagged_field(method_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/bootstrap_form_builder.rb', line 89

def self.create_tagged_field(method_name)
  define_method(method_name) do |name, *args|
    if [:file_field, :select].include? method_name
      super(@object_name, name, *args)
    elsif method_name == :radio_button
      options[:checked] = (@object.send(name) == args[0])
      super(@object_name, name, *(args << options))
    elsif method_name == :check_box
      options = args.extract_options!
      options[:checked] = (@object.send(name) == true)
      bootstrap_checkbox(name, super(@object_name, name, *(args << options)))
    else
      options = args.extract_options!
      options[:class] = "form-control"
      options[:value] = @object.kind_of?(Hash) ? @object[name] : @object.send(name)
      
      if method_name == :text_area
        options[:rows] = 15
        options[:cols] = 10
      end
      
      bootstrap_input(name, super(@object_name, name, *(args << options)))
    end
  end
end

Instance Method Details

#bootstrap_checkbox(name, input) ⇒ Object



15
16
17
18
19
20
21
# File 'app/helpers/bootstrap_form_builder.rb', line 15

def bootstrap_checkbox(name, input)
  @template.(
    "div",
    @template.label_tag(name) + input,
    {:class => "checkbox"}
  )
end

#bootstrap_fields_for(name, *args, &block) ⇒ Object



77
78
79
80
81
82
83
# File 'app/helpers/bootstrap_form_builder.rb', line 77

def bootstrap_fields_for(name, *args, &block)
  fields_for_name = "#{@object_name}[#{name}_attributes]"
  fields_for_object = @object.send(name)
      
  fields_for(fields_for_name, fields_for_object, {:builder => BootstrapFormBuilder}, &block)    
  hidden_field(fields_for_name, :id, {:value => fields_for_object.id}) if fields_for_object && !fields_for_object.kind_of?(Hash)
end

#bootstrap_input(name, input, include_br = false) ⇒ Object



7
8
9
10
11
12
13
# File 'app/helpers/bootstrap_form_builder.rb', line 7

def bootstrap_input(name, input, include_br = false)
  @template.(
    "div",
    @template.label_tag(name) + input,
    {:class => "form-group"}
  ) 
end

#collection_select(name, collection, value_method, text_method, options = {}) ⇒ Object



63
64
65
# File 'app/helpers/bootstrap_form_builder.rb', line 63

def collection_select(name, collection, value_method, text_method, options = {})
  bootstrap_input(name, super(@object_name, name, collection, value_method, text_method, options, {:class => "form-control"}))
end

#cw_image_tag(name, carrierwave_version = :thumbnail, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/bootstrap_form_builder.rb', line 23

def cw_image_tag(name, carrierwave_version = :thumbnail, options = {})
  
  html = ""
  image_url = self.object.send(name).send(carrierwave_version).url
  if image_url
    html << image_tag(image_url)
    html << "<br/>"
  end
  
  html << self.file_field(name)

  raw(html)
end

#datetime_select(name, *args) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/helpers/bootstrap_form_builder.rb', line 50

def datetime_select(name, *args)
  html = "<div class='form-group'>"
  html << self.label(@object_name, name)
  html << "<br/>"
  html << @template.datetime_select(@object_name, name, *(args << {:start_year => Time.now.year, :end_year => Time.now.year+1, :class => "form-control"}), {})
  html << "</div>"
  raw(html)
end

#hidden_field(name, *args, &block) ⇒ Object



85
86
87
# File 'app/helpers/bootstrap_form_builder.rb', line 85

def hidden_field(name, *args, &block)
  super(@object_name, name, *(args << options.merge(:value => @object.send(name))))
end

#image(name, carrierwave_version = :thumbnail) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/bootstrap_form_builder.rb', line 37

def image(name, carrierwave_version = :thumbnail)  
  uploader = self.object.send(name)
  size = uploader.upload_size
    
  html = "<div class='form-group'>"
  html << self.label(@object_name, name)
  html << "<br/>"
  html << cw_image_tag(name, carrierwave_version)
  html << "<p class='help-block'>Image must be #{size[0]}x#{size[1]}, and less than 1MB. #{uploader.extension_white_list.map(&:upcase).join(", ")} only.</p>"
  html << "</div>"
  raw(html)
end

#radio(name, values, options = {}) ⇒ Object



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

def radio(name, values, options = {})    
  html = ""
  values.each do |h|
    html << self.radio_button(name, h[0])
    html << " #{h[1]}"
    html << "<br/>"
  end    
  bootstrap_input(name, raw(html))
end

#select(name, options_for_select, options = {}) ⇒ Object



59
60
61
# File 'app/helpers/bootstrap_form_builder.rb', line 59

def select(name, options_for_select, options = {})
  bootstrap_input(name, super(@object_name, name, options_for_select, options, {:class => "form-control"}), true)
end