class TbCore::FormBuilder < ActionView::Helpers::FormBuilder ############### # Basic Helpers # # Use the following methods as the basic building blocks for your forms # # Build a form group # def tb_form_group(content=nil, options={}) content_tag :div, options.merge(:class => 'form-group') do if block_given? yield else content end end end # Build a label # def tb_label(attribute) label(attribute, @object.class.human_attribute_name(attribute), :class => 'col-sm-2 control-label') end # Builds an input field with error message # def tb_input_field_tag(attribute, input_type=nil, options={}) content_tag(: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 content_tag(:p, error_message, :class => 'help-block form-field-error') end end end # Builds a text field # def tb_text_field_tag(attribute, options={}) tb_input_field_tag(attribute, :text_field, options) end # Builds a text area # def tb_text_area_tag(attribute, options={}) tb_input_field_tag(attribute, :text_area, options) end # Builds a password field # def tb_password_field_tag(attribute, options={}) tb_input_field_tag(attribute, :password_field, options) end # Builds a number field # def tb_number_field_tag(attribute, options={}) tb_input_field_tag(attribute, :number_field, options) end # Builds a check box # def tb_check_box_tag(attribute, options={}) tb_input_field_tag(attribute, :check_box, options) end # Builds a select tag # 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 # Builds a date select tag # 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 # Builds a date select tag # 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 # Builds a time select tag # 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 # Builds a row of save/cancel buttons # def tb_save_buttons(model_name, cancel_path) content_tag :div, :class => 'form-group' do content_tag :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 ################## # Advanced Helpers # # These helpers are designed to output an entire form group with child elements # ie, container div + label + input + error message # # Builds a form group, label, and input tag all in one # 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 # Builds a text field group # def tb_text_field(attribute, options={}) tb_input_field(attribute, :text_field, options) end # Builds a text area group # def tb_text_area(attribute, options={}) tb_input_field(attribute, :text_area, options) end # Builds a password field group # def tb_password_field(attribute, options={}) tb_input_field(attribute, :password_field, options) end # Builds a number field group # def tb_number_field(attribute, options={}) tb_input_field(attribute, :number_field, options) end # Builds a check box # def tb_check_box(attribute, options={}) tb_input_field(attribute) do check_box(attribute, options) end end # Builds a file field group # def tb_file_field(attribute, options={}) tb_input_field(attribute) do file_field(attribute) end end # Builds a select group # 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 # Builds a date select tag # 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 # Builds a date select tag # 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 # Builds a time select tag # 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 # Builds a time zone select group # 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 private def concat(*args) @template.concat(*args) end def content_tag(*args) if block_given? @template.content_tag(*args) do yield end else @template.content_tag(*args) end end def determine_input_type_from_attribute(attribute) # TODO: Intelligently return the correct input for the given attribute return :text_field end end