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



213
214
215
216
217
# File 'lib/tb_core/form_builder.rb', line 213

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



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

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



237
238
239
240
241
242
# File 'lib/tb_core/form_builder.rb', line 237

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



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

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



246
247
248
249
250
251
# File 'lib/tb_core/form_builder.rb', line 246

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



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

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



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

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



221
222
223
224
225
# File 'lib/tb_core/form_builder.rb', line 221

def tb_file_field(attribute, options={})
  tb_input_field(attribute, nil, options) 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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tb_core/form_builder.rb', line 156

def tb_input_field(attribute, input_type=nil, options={})
  tb_form_group() do
    concat tb_label(attribute, options)
    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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tb_core/form_builder.rb', line 34

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 options[:help_text]
      concat (:p, @template.raw(options[:help_text]), class: 'help-block')
    end
    if error_message
      concat (:p, error_message, class: 'form-error form-error-inline')
    end
  end
end

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

Build a label



23
24
25
26
27
28
29
30
# File 'lib/tb_core/form_builder.rb', line 23

def tb_label(attribute, options={})
  if options[:label_text] != nil
    text = options[:label_text]
  else
    text = @object.class.human_attribute_name(attribute)
  end
  label(attribute, text, class: 'col-sm-2 control-label')
end

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

Builds a number field group



207
208
209
# File 'lib/tb_core/form_builder.rb', line 207

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



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

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



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

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



67
68
69
# File 'lib/tb_core/form_builder.rb', line 67

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



189
190
191
# File 'lib/tb_core/form_builder.rb', line 189

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/tb_core/form_builder.rb', line 120

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



229
230
231
232
233
# File 'lib/tb_core/form_builder.rb', line 229

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



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

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

Advanced Helpers

These helpers are designed to output an entire form group with child elements ie, container div + label + input + error message



146
147
148
149
150
151
152
# File 'lib/tb_core/form_builder.rb', line 146

def tb_sub_title(text, options={})
   :div, options.merge(class: 'form-group') do
     :div, class: 'col-sm-offset-2 col-sm-10' do
       :h4, text, class: 'form-sub-title'
    end
  end
end

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

Builds a text area group



195
196
197
# File 'lib/tb_core/form_builder.rb', line 195

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



61
62
63
# File 'lib/tb_core/form_builder.rb', line 61

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



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

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



55
56
57
# File 'lib/tb_core/form_builder.rb', line 55

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



255
256
257
258
259
260
# File 'lib/tb_core/form_builder.rb', line 255

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



111
112
113
114
115
116
# File 'lib/tb_core/form_builder.rb', line 111

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



264
265
266
267
268
# File 'lib/tb_core/form_builder.rb', line 264

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’



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/tb_core/form_builder.rb', line 277

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