Skip to content

Instantly share code, notes, and snippets.

@wndxlori
Forked from jamiepenney/bootstrap.rb
Created August 29, 2012 21:31
Show Gist options
  • Save wndxlori/3519232 to your computer and use it in GitHub Desktop.
Save wndxlori/3519232 to your computer and use it in GitHub Desktop.

Revisions

  1. @jamiepenney jamiepenney revised this gist May 9, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -205,7 +205,7 @@ def basic_datetime_select(field, options = {})
    "</div>").html_safe
    end

    basic_helpers = %w{text_field text_area select email_field password_field check_box}
    basic_helpers = %w{text_field text_area select email_field password_field check_box number_field}

    multipart_helpers = %w{date_select datetime_select}

    @@ -260,4 +260,4 @@ def basic_datetime_select(field, options = {})
    end
    end
    end
    end
    end
  2. @jamiepenney jamiepenney revised this gist May 1, 2012. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -73,6 +73,10 @@ def get_label(field, options)
    labelTag = label(field, text, labelOptions)
    end

    def submit(value, options = {}, *args)
    super(value, {:class => "btn btn-primary"}.merge(options), *args)
    end

    def jquery_date_select(field, options = {})
    id = get_object_id(field, options)

    @@ -257,4 +261,3 @@ def basic_datetime_select(field, options = {})
    end
    end
    end

  3. @jamiepenney jamiepenney revised this gist Apr 29, 2012. 1 changed file with 72 additions and 6 deletions.
    78 changes: 72 additions & 6 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    # = f.jquery_datetime_select :start_time, :class => 'span3'
    # = f.jquery_datetime_select :end_time, :class => 'span3'
    # = f.check_box :all_day
    # = f.text_field :tag_string, :label => 'Tags', :class => 'span3'
    # = f.text_field :tag_string, :label => {:text => 'Tags'}, :class => 'span3'
    # .form-actions
    # = f.submit 'Save', :class => 'btn btn-primary'
    # = link_to 'Cancel', calendar_entries_path, :class => 'btn'
    @@ -21,7 +21,7 @@
    # <%= f.jquery_datetime_select :start_time, :class => 'span3' %>
    # <%= f.jquery_datetime_select :end_time, :class => 'span3' %>
    # <%= f.check_box :all_day %>
    # <%= f.text_field :tag_string, :label => 'Tags', :class => 'span3' do %>
    # <%= f.text_field :tag_string, :label => {:text => 'Tags'}, :class => 'span3' do %>
    # <p class="help-block">Use commas to separate tags.</p>
    # <% end %>
    # <div class="form-actions">
    @@ -40,7 +40,7 @@ module FormHelper

    if method == :form_for
    options[:html] ||= {}
    options[:html][:class] = 'form-horizontal'
    options[:html][:class] ||= 'form-horizontal'
    end

    # call the original method with our overridden options
    @@ -62,14 +62,79 @@ def get_error_text(object, field, options)
    end
    end

    def get_object_id(field)
    def get_object_id(field, options)
    object = @template.instance_variable_get("@#{@object_name}")
    return options[:id] || object.class.name.underscore + '_' + field.to_s
    end

    def get_label(field, options)
    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    labelTag = label(field, labelOptions)
    text = labelOptions[:text] || nil
    labelTag = label(field, text, labelOptions)
    end

    def jquery_date_select(field, options = {})
    id = get_object_id(field, options)

    date =
    if options['start_date']
    options['start_date']
    elsif object.nil?
    Date.now
    else
    object.send(field.to_sym)
    end

    date_picker_script = "<script type='text/javascript'>" +
    "$( function() { " +
    "$('##{id}')" +
    ".datepicker( $.datepicker.regional[ 'en-NZ' ] )" +
    ".datepicker( 'setDate', new Date('#{date}') ); } );" +
    "</script>"
    return basic_date_select(field, options.merge(javascript: date_picker_script))
    end

    def basic_date_select(field, options = {})
    placeholder_text = options[:placeholder_text] || ''
    id = get_object_id(field, options)

    errorText = get_error_text(object, field, options)
    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end

    labelTag = get_label(field, options)

    date =
    if options[:start_date]
    options[:start_date]
    elsif object.nil?
    Date.now.utc
    else
    object.send(field.to_sym)
    end

    javascript = options[:javascript] ||
    "
    <script>
    $(function() {
    var el = $('##{id}');
    var currentValue = el.val();
    if(currentValue.trim() == '') return;
    el.val(new Date(currentValue).toString('dd MMM, yyyy'));
    });
    </script>"

    ("<div class='#{wrapperClass}'>" +
    labelTag +
    "<div class='controls'>" +
    super_text_field(field, {
    :id => id, :placeholder => placeholder_text, :value => date.to_s,
    :class => options[:class]
    }.merge(options[:text_field] || {})) +
    errorSpan +
    javascript +
    "</div>" +
    "</div>").html_safe
    end

    def jquery_datetime_select(field, options = {})
    @@ -191,4 +256,5 @@ def basic_datetime_select(field, options = {})
    end
    end
    end
    end
    end

  4. @jamiepenney jamiepenney revised this gist Mar 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    # = f.submit 'Save', :class => 'btn btn-primary'
    # = link_to 'Cancel', calendar_entries_path, :class => 'btn'
    #
    # I don't do HAML
    # If you don't use HAML, here is the same thing in ERB
    # <%= bootstrap_form_for @calendar_entry do |f| %>
    # <%= content_tag :legend, (locals[:title] || 'Edit Calendar Entry') %>
    # <%= f.text_field :name, :class => 'span3' %>
  5. @glongman glongman revised this gist Mar 3, 2012. 1 changed file with 22 additions and 4 deletions.
    26 changes: 22 additions & 4 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,23 @@
    # = f.text_field :tag_string, :label => 'Tags', :class => 'span3'
    # .form-actions
    # = f.submit 'Save', :class => 'btn btn-primary'
    # = link_to 'Cancel', calendar_entries_path, :class => 'btn'
    # = link_to 'Cancel', calendar_entries_path, :class => 'btn'
    #
    # I don't do HAML
    # <%= bootstrap_form_for @calendar_entry do |f| %>
    # <%= content_tag :legend, (locals[:title] || 'Edit Calendar Entry') %>
    # <%= f.text_field :name, :class => 'span3' %>
    # <%= f.text_area :description, :class => 'span3' %>
    # <%= f.jquery_datetime_select :start_time, :class => 'span3' %>
    # <%= f.jquery_datetime_select :end_time, :class => 'span3' %>
    # <%= f.check_box :all_day %>
    # <%= f.text_field :tag_string, :label => 'Tags', :class => 'span3' do %>
    # <p class="help-block">Use commas to separate tags.</p>
    # <% end %>
    # <div class="form-actions">
    # <%= f.submit 'Save', :class => 'btn btn-primary' %>
    # <%= link_to 'Cancel', calendar_entries_path, :class => 'btn' %>
    # </div>

    module BootstrapFormBuilder
    module FormHelper
    @@ -128,7 +144,7 @@ def basic_datetime_select(field, options = {})
    # First alias old method
    class_eval("alias super_#{name.to_s} #{name}")

    define_method(name) do |field, *args|
    define_method(name) do |field, *args, &help_block|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    @@ -143,14 +159,15 @@ def basic_datetime_select(field, options = {})
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    (help_block ? @template.capture(&help_block) : "") +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end

    multipart_helpers.each do |name|
    define_method(name) do |field, *args|
    define_method(name) do |field, *args, &help_block|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    @@ -167,10 +184,11 @@ def basic_datetime_select(field, options = {})
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    (help_block ? @template.capture(&help_block) : "") +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end
    end
    end
    end
  6. @jamiepenney jamiepenney revised this gist Feb 25, 2012. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -46,14 +46,18 @@ def get_error_text(object, field, options)
    end
    end


    def get_object_id(field)
    object = @template.instance_variable_get("@#{@object_name}")
    return options[:id] || object.class.name.underscore + '_' + field.to_s
    end

    def get_label(field, options)
    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    labelTag = label(field, labelOptions)
    end

    def jquery_datetime_select(field, options = {})
    id = get_object_id(field)
    id = get_object_id(field, options)

    date_time =
    if options['start_time']
    @@ -74,16 +78,14 @@ def jquery_datetime_select(field, options = {})
    end

    def basic_datetime_select(field, options = {})
    print options
    placeholder_text = options[:placeholder_text] || ''
    id = get_object_id(field)
    id = get_object_id(field, options)

    errorText = get_error_text(object, field, options)
    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end

    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    label = label(field, labelOptions)

    labelTag = get_label(field, options)

    date_time =
    if options[:start_time]
    @@ -106,7 +108,7 @@ def basic_datetime_select(field, options = {})
    </script>"

    ("<div class='#{wrapperClass}'>" +
    label +
    labelTag +
    "<div class='controls'>" +
    super_text_field(field, {
    :id => id, :placeholder => placeholder_text, :value => date_time.to_s,
    @@ -130,15 +132,14 @@ def basic_datetime_select(field, options = {})
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    label = label(field, labelOptions)
    labelTag = get_label(field, options)

    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    labelTag +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    @@ -153,8 +154,7 @@ def basic_datetime_select(field, options = {})
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    label = label(field, labelOptions)
    labelTag = get_label(field, options)

    options[:class] = 'inline ' + options[:class] if options[:class]

    @@ -163,7 +163,7 @@ def basic_datetime_select(field, options = {})
    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    labelTag +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    @@ -173,4 +173,4 @@ def basic_datetime_select(field, options = {})
    end
    end
    end
    end
    end
  7. @jamiepenney jamiepenney revised this gist Feb 25, 2012. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -81,7 +81,9 @@ def basic_datetime_select(field, options = {})
    errorText = get_error_text(object, field, options)
    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    label = label(field, options[:label])

    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    label = label(field, labelOptions)

    date_time =
    if options[:start_time]
    @@ -128,7 +130,7 @@ def basic_datetime_select(field, options = {})
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    labelOptions = {:class => 'control-label'}.merge(options[:label])
    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    label = label(field, labelOptions)

    errorText = get_error_text(object, field, options)
    @@ -151,7 +153,7 @@ def basic_datetime_select(field, options = {})
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    labelOptions = {:class => 'control-label'}.merge(options[:label])
    labelOptions = {:class => 'control-label'}.merge(options[:label] || {})
    label = label(field, labelOptions)

    options[:class] = 'inline ' + options[:class] if options[:class]
  8. @jamiepenney jamiepenney revised this gist Feb 25, 2012. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -128,7 +128,9 @@ def basic_datetime_select(field, options = {})
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])
    labelOptions = {:class => 'control-label'}.merge(options[:label])
    label = label(field, labelOptions)

    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    @@ -149,7 +151,8 @@ def basic_datetime_select(field, options = {})
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])
    labelOptions = {:class => 'control-label'}.merge(options[:label])
    label = label(field, labelOptions)

    options[:class] = 'inline ' + options[:class] if options[:class]

  9. @jamiepenney jamiepenney revised this gist Feb 22, 2012. 1 changed file with 28 additions and 1 deletion.
    29 changes: 28 additions & 1 deletion bootstrap.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,32 @@
    # This file goes in config/initializers
    require 'bootstrap_form_builder'

    # Make this the default Form Builder. You can delete this if you don't want form_for to use
    # the bootstrap form builder by default
    ActionView::Base.default_form_builder = BootstrapFormBuilder::FormBuilder
    ActionView::Base.send :include, BootstrapFormBuilder::FormHelper

    # Add in our FormHelper methods, so you can use bootstrap_form_for.
    ActionView::Base.send :include, BootstrapFormBuilder::FormHelper

    ### Only use one of these error handling methods ###

    # Get rid of the rails error handling completely.
    ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
    "#{html_tag}".html_safe
    end

    # Only remove the default rails error handling on input and label
    # Relies on the Nokogiri gem.
    # Credit to https://github.com/ripuk
    ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
    html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
    elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, input"
    elements.each do |e|
    if e.node_name.eql? 'label'
    html = %(#{e}).html_safe
    elsif e.node_name.eql? 'input'
    html = %(#{e}).html_safe
    end
    end
    html
    end
  10. @jamiepenney jamiepenney revised this gist Feb 21, 2012. 2 changed files with 2 additions and 172 deletions.
    3 changes: 2 additions & 1 deletion bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # This file goes in lib/
    # Usage:
    #= bootstrap_form_for @calendar_entry do |f|
    # %fieldset
    @@ -167,4 +168,4 @@ def basic_datetime_select(field, options = {})
    end
    end
    end
    end
    end
    171 changes: 0 additions & 171 deletions libbootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -1,171 +0,0 @@
    # This file goes in lib/
    # Usage:
    #= bootstrap_form_for @calendar_entry do |f|
    # %fieldset
    # %legend= locals[:title] || 'Edit Calendar Entry'
    # = f.text_field :name, :class => 'span3'
    # = f.text_area :description, :class => 'span3'
    # = f.jquery_datetime_select :start_time, :class => 'span3'
    # = f.jquery_datetime_select :end_time, :class => 'span3'
    # = f.check_box :all_day
    # = f.text_field :tag_string, :label => 'Tags', :class => 'span3'
    # .form-actions
    # = f.submit 'Save', :class => 'btn btn-primary'
    # = link_to 'Cancel', calendar_entries_path, :class => 'btn'

    module BootstrapFormBuilder
    module FormHelper
    [:form_for, :fields_for].each do |method|
    module_eval do
    define_method "bootstrap_#{method}" do |record, *args, &block|
    # add the TwitterBootstrap builder to the options
    options = args.extract_options!
    options[:builder] = BootstrapFormBuilder::FormBuilder

    if method == :form_for
    options[:html] ||= {}
    options[:html][:class] = 'form-horizontal'
    end

    # call the original method with our overridden options
    send method, record, *(args << options), &block
    end
    end
    end
    end

    class FormBuilder < ActionView::Helpers::FormBuilder
    include FormHelper

    def get_error_text(object, field, options)
    if object.nil? || options[:hide_errors]
    ""
    else
    errors = object.errors[field.to_sym]
    if errors.empty? then "" else errors.first end
    end
    end


    def get_object_id(field)
    object = @template.instance_variable_get("@#{@object_name}")
    return options[:id] || object.class.name.underscore + '_' + field.to_s
    end

    def jquery_datetime_select(field, options = {})
    id = get_object_id(field)

    date_time =
    if options['start_time']
    options['start_time']
    elsif object.nil?
    DateTime.now.utc
    else
    object.send(field.to_sym)
    end

    datetime_picker_script = "<script type='text/javascript'>" +
    "$( function() { " +
    "$('##{id}')" +
    ".datetimepicker( $.datepicker.regional[ 'en-NZ' ] )" +
    ".datetimepicker( 'setDate', new Date('#{date_time}') ); } );" +
    "</script>"
    return basic_datetime_select(field, options.merge(javascript: datetime_picker_script))
    end

    def basic_datetime_select(field, options = {})
    print options
    placeholder_text = options[:placeholder_text] || ''
    id = get_object_id(field)

    errorText = get_error_text(object, field, options)
    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    label = label(field, options[:label])

    date_time =
    if options[:start_time]
    options[:start_time]
    elsif object.nil?
    DateTime.now.utc
    else
    object.send(field.to_sym)
    end

    javascript = options[:javascript] ||
    "
    <script>
    $(function() {
    var el = $('##{id}');
    var currentValue = el.val();
    if(currentValue.trim() == '') return;
    el.val(new Date(currentValue).toString('dd MMM, yyyy HH:mm'));
    });
    </script>"

    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super_text_field(field, {
    :id => id, :placeholder => placeholder_text, :value => date_time.to_s,
    :class => options[:class]
    }.merge(options[:text_field] || {})) +
    errorSpan +
    javascript +
    "</div>" +
    "</div>").html_safe
    end

    basic_helpers = %w{text_field text_area select email_field password_field check_box}

    multipart_helpers = %w{date_select datetime_select}

    basic_helpers.each do |name|
    # First alias old method
    class_eval("alias super_#{name.to_s} #{name}")

    define_method(name) do |field, *args|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])
    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end

    multipart_helpers.each do |name|
    define_method(name) do |field, *args|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])

    options[:class] = 'inline ' + options[:class] if options[:class]

    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end
    end
    end
  11. @jamiepenney jamiepenney revised this gist Feb 21, 2012. 2 changed files with 176 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions bootstrap.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # This file goes in config/initializers
    require 'bootstrap_form_builder'

    ActionView::Base.default_form_builder = BootstrapFormBuilder::FormBuilder
    ActionView::Base.send :include, BootstrapFormBuilder::FormHelper
    171 changes: 171 additions & 0 deletions libbootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,171 @@
    # This file goes in lib/
    # Usage:
    #= bootstrap_form_for @calendar_entry do |f|
    # %fieldset
    # %legend= locals[:title] || 'Edit Calendar Entry'
    # = f.text_field :name, :class => 'span3'
    # = f.text_area :description, :class => 'span3'
    # = f.jquery_datetime_select :start_time, :class => 'span3'
    # = f.jquery_datetime_select :end_time, :class => 'span3'
    # = f.check_box :all_day
    # = f.text_field :tag_string, :label => 'Tags', :class => 'span3'
    # .form-actions
    # = f.submit 'Save', :class => 'btn btn-primary'
    # = link_to 'Cancel', calendar_entries_path, :class => 'btn'

    module BootstrapFormBuilder
    module FormHelper
    [:form_for, :fields_for].each do |method|
    module_eval do
    define_method "bootstrap_#{method}" do |record, *args, &block|
    # add the TwitterBootstrap builder to the options
    options = args.extract_options!
    options[:builder] = BootstrapFormBuilder::FormBuilder

    if method == :form_for
    options[:html] ||= {}
    options[:html][:class] = 'form-horizontal'
    end

    # call the original method with our overridden options
    send method, record, *(args << options), &block
    end
    end
    end
    end

    class FormBuilder < ActionView::Helpers::FormBuilder
    include FormHelper

    def get_error_text(object, field, options)
    if object.nil? || options[:hide_errors]
    ""
    else
    errors = object.errors[field.to_sym]
    if errors.empty? then "" else errors.first end
    end
    end


    def get_object_id(field)
    object = @template.instance_variable_get("@#{@object_name}")
    return options[:id] || object.class.name.underscore + '_' + field.to_s
    end

    def jquery_datetime_select(field, options = {})
    id = get_object_id(field)

    date_time =
    if options['start_time']
    options['start_time']
    elsif object.nil?
    DateTime.now.utc
    else
    object.send(field.to_sym)
    end

    datetime_picker_script = "<script type='text/javascript'>" +
    "$( function() { " +
    "$('##{id}')" +
    ".datetimepicker( $.datepicker.regional[ 'en-NZ' ] )" +
    ".datetimepicker( 'setDate', new Date('#{date_time}') ); } );" +
    "</script>"
    return basic_datetime_select(field, options.merge(javascript: datetime_picker_script))
    end

    def basic_datetime_select(field, options = {})
    print options
    placeholder_text = options[:placeholder_text] || ''
    id = get_object_id(field)

    errorText = get_error_text(object, field, options)
    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    label = label(field, options[:label])

    date_time =
    if options[:start_time]
    options[:start_time]
    elsif object.nil?
    DateTime.now.utc
    else
    object.send(field.to_sym)
    end

    javascript = options[:javascript] ||
    "
    <script>
    $(function() {
    var el = $('##{id}');
    var currentValue = el.val();
    if(currentValue.trim() == '') return;
    el.val(new Date(currentValue).toString('dd MMM, yyyy HH:mm'));
    });
    </script>"

    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super_text_field(field, {
    :id => id, :placeholder => placeholder_text, :value => date_time.to_s,
    :class => options[:class]
    }.merge(options[:text_field] || {})) +
    errorSpan +
    javascript +
    "</div>" +
    "</div>").html_safe
    end

    basic_helpers = %w{text_field text_area select email_field password_field check_box}

    multipart_helpers = %w{date_select datetime_select}

    basic_helpers.each do |name|
    # First alias old method
    class_eval("alias super_#{name.to_s} #{name}")

    define_method(name) do |field, *args|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])
    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end

    multipart_helpers.each do |name|
    define_method(name) do |field, *args|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])

    options[:class] = 'inline ' + options[:class] if options[:class]

    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end
    end
    end
  12. Jamie Penney created this gist Feb 14, 2012.
    170 changes: 170 additions & 0 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    # Usage:
    #= bootstrap_form_for @calendar_entry do |f|
    # %fieldset
    # %legend= locals[:title] || 'Edit Calendar Entry'
    # = f.text_field :name, :class => 'span3'
    # = f.text_area :description, :class => 'span3'
    # = f.jquery_datetime_select :start_time, :class => 'span3'
    # = f.jquery_datetime_select :end_time, :class => 'span3'
    # = f.check_box :all_day
    # = f.text_field :tag_string, :label => 'Tags', :class => 'span3'
    # .form-actions
    # = f.submit 'Save', :class => 'btn btn-primary'
    # = link_to 'Cancel', calendar_entries_path, :class => 'btn'

    module BootstrapFormBuilder
    module FormHelper
    [:form_for, :fields_for].each do |method|
    module_eval do
    define_method "bootstrap_#{method}" do |record, *args, &block|
    # add the TwitterBootstrap builder to the options
    options = args.extract_options!
    options[:builder] = BootstrapFormBuilder::FormBuilder

    if method == :form_for
    options[:html] ||= {}
    options[:html][:class] = 'form-horizontal'
    end

    # call the original method with our overridden options
    send method, record, *(args << options), &block
    end
    end
    end
    end

    class FormBuilder < ActionView::Helpers::FormBuilder
    include FormHelper

    def get_error_text(object, field, options)
    if object.nil? || options[:hide_errors]
    ""
    else
    errors = object.errors[field.to_sym]
    if errors.empty? then "" else errors.first end
    end
    end


    def get_object_id(field)
    object = @template.instance_variable_get("@#{@object_name}")
    return options[:id] || object.class.name.underscore + '_' + field.to_s
    end

    def jquery_datetime_select(field, options = {})
    id = get_object_id(field)

    date_time =
    if options['start_time']
    options['start_time']
    elsif object.nil?
    DateTime.now.utc
    else
    object.send(field.to_sym)
    end

    datetime_picker_script = "<script type='text/javascript'>" +
    "$( function() { " +
    "$('##{id}')" +
    ".datetimepicker( $.datepicker.regional[ 'en-NZ' ] )" +
    ".datetimepicker( 'setDate', new Date('#{date_time}') ); } );" +
    "</script>"
    return basic_datetime_select(field, options.merge(javascript: datetime_picker_script))
    end

    def basic_datetime_select(field, options = {})
    print options
    placeholder_text = options[:placeholder_text] || ''
    id = get_object_id(field)

    errorText = get_error_text(object, field, options)
    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    label = label(field, options[:label])

    date_time =
    if options[:start_time]
    options[:start_time]
    elsif object.nil?
    DateTime.now.utc
    else
    object.send(field.to_sym)
    end

    javascript = options[:javascript] ||
    "
    <script>
    $(function() {
    var el = $('##{id}');
    var currentValue = el.val();
    if(currentValue.trim() == '') return;
    el.val(new Date(currentValue).toString('dd MMM, yyyy HH:mm'));
    });
    </script>"

    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super_text_field(field, {
    :id => id, :placeholder => placeholder_text, :value => date_time.to_s,
    :class => options[:class]
    }.merge(options[:text_field] || {})) +
    errorSpan +
    javascript +
    "</div>" +
    "</div>").html_safe
    end

    basic_helpers = %w{text_field text_area select email_field password_field check_box}

    multipart_helpers = %w{date_select datetime_select}

    basic_helpers.each do |name|
    # First alias old method
    class_eval("alias super_#{name.to_s} #{name}")

    define_method(name) do |field, *args|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])
    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end

    multipart_helpers.each do |name|
    define_method(name) do |field, *args|
    options = args.last.is_a?(Hash) ? args.last : {}
    object = @template.instance_variable_get("@#{@object_name}")

    label = label(field, options[:label])

    options[:class] = 'inline ' + options[:class] if options[:class]

    errorText = get_error_text(object, field, options)

    wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error')
    errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end
    ("<div class='#{wrapperClass}'>" +
    label +
    "<div class='controls'>" +
    super(field, *args) +
    errorSpan +
    "</div>" +
    "</div>"
    ).html_safe
    end
    end
    end
    end