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



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

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



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

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



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

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_email_field(attribute, options = {}) ⇒ Object

Builds a email field group



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

def tb_email_field(attribute, options={})
  tb_input_field(attribute, :email_field, options)
end

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

Builds a file field group



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

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/tb_core/form_builder.rb', line 135

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



186
187
188
# File 'lib/tb_core/form_builder.rb', line 186

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



180
181
182
# File 'lib/tb_core/form_builder.rb', line 180

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_phone_field(attribute, options = {}) ⇒ Object

Builds a telephone field group



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

def tb_phone_field(attribute, options={})
  tb_input_field(attribute, :phone_field, options)
end

#tb_save_buttons(model_name, cancel_path, delete_path = nil) ⇒ Object

Builds a row of save/cancel buttons



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

def tb_save_buttons(model_name, cancel_path, delete_path=nil)
   :div, :class => 'form-group' do
     :div, :class => 'col-sm-offset-2 col-sm-10' do
      concat submit "Save #{model_name}", :class => 'btn btn-primary', :data => {:disable_with => "Saving #{model_name}...", :enable_with => 'Saved!'}
      concat ' '
      concat @template.link_to 'Cancel', cancel_path, :class => 'btn btn-default', :data => {:dismiss => :modal}
      if delete_path != nil
        concat ' '
        concat @template.link_to 'Delete', delete_path, :class => 'btn btn-danger', :data => {:confirm => "This action can't be undone. Would you like to delete the #{model_name.downcase}?"}, :method => :delete
      end
    end
  end
end

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

Builds a select group



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

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



174
175
176
# File 'lib/tb_core/form_builder.rb', line 174

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



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

def tb_text_field(attribute, options={})
  options[:maxlength] ||= get_limit_for_column(attribute)
  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



234
235
236
237
238
239
# File 'lib/tb_core/form_builder.rb', line 234

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



243
244
245
246
247
# File 'lib/tb_core/form_builder.rb', line 243

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

#tb_user_select(attribute = :spud_user_id, users: SpudUser.ordered, selected: nil, default_text: 'Select User') ⇒ Object

Return a picker for a new or existing user

  • attribute: The spud user column name. Default = spud_user_id.

  • users: Optional query object representing the list of users to display. Default = all users.

  • selected: Desired id to select. Will try to look up the value currently on the record if blank.

  • default_text: Text to show for blank option. Default = ‘Select User’



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/tb_core/form_builder.rb', line 256

def tb_user_select(attribute = :spud_user_id, users: SpudUser.ordered, selected: nil, default_text: 'Select User')
  selected = @object[attribute] if selected.nil?
  return  :div, :class => 'form-group' do
    concat label attribute, :class => 'control-label col-sm-2'
    concat (:div, :class => 'col-sm-10'){
      concat select(
        attribute,
        @template.options_from_collection_for_select(users, :id, :full_name_with_email, selected),
        {:include_blank => default_text},
        {:class => 'form-control tb-user-select'}
      )
      concat @template.link_to 'Edit User', @template.edit_admin_user_path(':id'), :class => 'btn btn-default tb-user-select-btn tb-user-select-edit'
      concat @template.link_to 'New User', @template.new_admin_user_path, :class => 'btn btn-default tb-user-select-btn tb-user-select-add'
    }
  end
end