Class: TbCore::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/tb_core/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#tb_check_box(attribute, options = {}) ⇒ Object

Builds a check box



175
176
177
178
179
# File 'lib/tb_core/form_builder.rb', line 175

def tb_check_box(attribute, options={})
  tb_input_field(attribute) do
    check_box(attribute, options)
  end
end

#tb_check_box_tag(attribute, options = {}) ⇒ Object

Builds a check box



71
72
73
# File 'lib/tb_core/form_builder.rb', line 71

def tb_check_box_tag(attribute, options={})
  tb_input_field_tag(attribute, :check_box, options)
end

#tb_date_select(attribute, options = {}, html_options = {}) ⇒ Object

Builds a date select tag



199
200
201
202
203
204
# File 'lib/tb_core/form_builder.rb', line 199

def tb_date_select(attribute, options={}, html_options={})
  options[:with_css_classes] = true
  tb_input_field(attribute) do
    date_select(attribute, objectify_options(options), html_options.merge(:class => 'form-control date-select'))
  end
end

#tb_date_select_tag(attribute, options = {}, html_options = {}) ⇒ Object

Builds a date select tag



85
86
87
88
89
90
# File 'lib/tb_core/form_builder.rb', line 85

def tb_date_select_tag(attribute, options={}, html_options={})
  options[:with_css_classes] = true
  tb_input_field_tag(attribute) do
    date_select(attribute, objectify_options(options), html_options.merge(:class => 'form-control date-select'))
  end
end

#tb_datetime_select(attribute, options = {}, html_options = {}) ⇒ Object

Builds a date select tag



208
209
210
211
212
213
# File 'lib/tb_core/form_builder.rb', line 208

def tb_datetime_select(attribute, options={}, html_options={})
  options[:with_css_classes] = true
  tb_input_field(attribute) do
    datetime_select(attribute, objectify_options(options), html_options.merge(:class => 'form-control datetime-select'))
  end
end

#tb_datetime_select_tag(attribute, options = {}, html_options = {}) ⇒ Object

Builds a date select tag



94
95
96
97
98
99
# File 'lib/tb_core/form_builder.rb', line 94

def tb_datetime_select_tag(attribute, options={}, html_options={})
  options[:with_css_classes] = true
  tb_input_field_tag(attribute) do
    datetime_select(attribute, objectify_options(options), html_options.merge(:class => 'form-control datetime-select'))
  end
end

#tb_file_field(attribute, options = {}) ⇒ Object

Builds a file field group



183
184
185
186
187
# File 'lib/tb_core/form_builder.rb', line 183

def tb_file_field(attribute, options={})
  tb_input_field(attribute) do
    file_field(attribute)
  end
end

#tb_form_group(content = nil, options = {}) ⇒ Object

Build a form group



11
12
13
14
15
16
17
18
19
# File 'lib/tb_core/form_builder.rb', line 11

def tb_form_group(content=nil, options={})
   :div, options.merge(:class => 'form-group') do
    if block_given?
      yield
    else
      content
    end
  end
end

#tb_input_field(attribute, input_type = nil, options = {}) ⇒ Object

Builds a form group, label, and input tag all in one



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/tb_core/form_builder.rb', line 131

def tb_input_field(attribute, input_type=nil, options={})
  tb_form_group() do
    concat tb_label(attribute)
    if block_given?
      concat(
        tb_input_field_tag(attribute) do
          yield
        end
      )
    else
      if input_type.nil?
        input_type = determine_input_type_from_attribute(attribute)
      end
      concat tb_input_field_tag(attribute, input_type, options)
    end
  end
end

#tb_input_field_tag(attribute, input_type = nil, options = {}) ⇒ Object

Builds an input field with error message



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tb_core/form_builder.rb', line 29

def tb_input_field_tag(attribute, input_type=nil, options={})
  (:div, :class => 'col-sm-10') do
    if block_given?
      concat(yield(attribute))
    else
      options[:class] ||= 'form-control'
      options[:placeholder] ||= @object.class.human_attribute_name(attribute)
      concat send(input_type, attribute, objectify_options(options))
    end
    error_message = @object.errors[attribute].first
    if error_message
      concat (:p, error_message, :class => 'help-block form-field-error')
    end
  end
end

#tb_label(attribute) ⇒ Object

Build a label



23
24
25
# File 'lib/tb_core/form_builder.rb', line 23

def tb_label(attribute)
  label(attribute, @object.class.human_attribute_name(attribute), :class => 'col-sm-2 control-label')
end

#tb_number_field(attribute, options = {}) ⇒ Object

Builds a number field group



169
170
171
# File 'lib/tb_core/form_builder.rb', line 169

def tb_number_field(attribute, options={})
  tb_input_field(attribute, :number_field, options)
end

#tb_number_field_tag(attribute, options = {}) ⇒ Object

Builds a number field



65
66
67
# File 'lib/tb_core/form_builder.rb', line 65

def tb_number_field_tag(attribute, options={})
  tb_input_field_tag(attribute, :number_field, options)
end

#tb_password_field(attribute, options = {}) ⇒ Object

Builds a password field group



163
164
165
# File 'lib/tb_core/form_builder.rb', line 163

def tb_password_field(attribute, options={})
  tb_input_field(attribute, :password_field, options)
end

#tb_password_field_tag(attribute, options = {}) ⇒ Object

Builds a password field



59
60
61
# File 'lib/tb_core/form_builder.rb', line 59

def tb_password_field_tag(attribute, options={})
  tb_input_field_tag(attribute, :password_field, options)
end

#tb_save_buttons(model_name, cancel_path) ⇒ Object

Builds a row of save/cancel buttons



112
113
114
115
116
117
118
119
120
# File 'lib/tb_core/form_builder.rb', line 112

def tb_save_buttons(model_name, cancel_path)
   :div, :class => 'form-group' do
     :div, :class => 'col-sm-offset-2 col-sm-10' do
      concat submit "Save #{model_name}", :class => 'btn btn-primary'
      concat ' '
      concat @template.link_to 'Cancel', cancel_path, :class => 'btn btn-default'
    end
  end
end

#tb_select(attribute, option_tags, options = {}, html_options = {}) ⇒ Object

Builds a select group



191
192
193
194
195
# File 'lib/tb_core/form_builder.rb', line 191

def tb_select(attribute, option_tags, options={}, html_options={})
  tb_input_field(attribute) do
    select(attribute, option_tags, objectify_options(options), html_options.merge(:class => 'form-control'))
  end
end

#tb_select_tag(attribute, option_tags, options = {}, html_options = {}) ⇒ Object

Builds a select tag



77
78
79
80
81
# File 'lib/tb_core/form_builder.rb', line 77

def tb_select_tag(attribute, option_tags, options={}, html_options={})
  tb_input_field_tag(attribute) do
    select(attribute, option_tags, objectify_options(options), html_options.merge(:class => 'form-control'))
  end
end

#tb_text_area(attribute, options = {}) ⇒ Object

Builds a text area group



157
158
159
# File 'lib/tb_core/form_builder.rb', line 157

def tb_text_area(attribute, options={})
  tb_input_field(attribute, :text_area, options)
end

#tb_text_area_tag(attribute, options = {}) ⇒ Object

Builds a text area



53
54
55
# File 'lib/tb_core/form_builder.rb', line 53

def tb_text_area_tag(attribute, options={})
  tb_input_field_tag(attribute, :text_area, options)
end

#tb_text_field(attribute, options = {}) ⇒ Object

Builds a text field group



151
152
153
# File 'lib/tb_core/form_builder.rb', line 151

def tb_text_field(attribute, options={})
  tb_input_field(attribute, :text_field, options)
end

#tb_text_field_tag(attribute, options = {}) ⇒ Object

Builds a text field



47
48
49
# File 'lib/tb_core/form_builder.rb', line 47

def tb_text_field_tag(attribute, options={})
  tb_input_field_tag(attribute, :text_field, options)
end

#tb_time_select(attribute, options = {}, html_options = {}) ⇒ Object

Builds a time select tag



217
218
219
220
221
222
# File 'lib/tb_core/form_builder.rb', line 217

def tb_time_select(attribute, options={}, html_options={})
  options[:with_css_classes] = true
  tb_input_field(attribute) do
    time_select(attribute, objectify_options(options), html_options.merge(:class => 'form-control datetime-select'))
  end
end

#tb_time_select_tag(attribute, options = {}, html_options = {}) ⇒ Object

Builds a time select tag



103
104
105
106
107
108
# File 'lib/tb_core/form_builder.rb', line 103

def tb_time_select_tag(attribute, options={}, html_options={})
  options[:with_css_classes] = true
  tb_input_field_tag(attribute) do
    time_select(attribute, objectify_options(options), html_options.merge(:class => 'form-control datetime-select'))
  end
end

#tb_time_zone_select(attribute, priority_zones, options = {}, html_options = {}) ⇒ Object

Builds a time zone select group



226
227
228
229
230
# File 'lib/tb_core/form_builder.rb', line 226

def tb_time_zone_select(attribute, priority_zones, options={}, html_options={})
  tb_input_field(attribute) do
    time_zone_select(attribute, priority_zones, objectify_options(options), html_options.merge(:class => 'form-control'))
  end
end