Skip to content

Instantly share code, notes, and snippets.

@robacarp
Created January 30, 2012 16:31

Revisions

  1. Robert L. Carpenter revised this gist Jan 30, 2012. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,15 @@
    # /app/form_builders/bootstrap_form_builder.rb
    #
    # This started out as a gist but I can't seem to find the original. :-/
    # It is probably the weakest link in this library between bootstrap and rails3.1
    # because you're always going to come across circumstances where its not doing
    # *quite* what you want.
    #
    # I've been meaning to put in a :dont_bootstrap param into the meta-method
    # but haven't really taken the time just yet. As it stands now you'll have to
    # write the form fields directly using rails' form helpers as if you were
    # outside of the context of a form_for in order to get direct control over
    # the field html being generated.

    class BootstrapFormBuilder < ActionView::Helpers::FormBuilder

  2. Robert L. Carpenter revised this gist Jan 30, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # A good place to save this file is:
    # /app/form_builders/bootstrap_form_builder.rb
    #
    # This started out as a gist but I can't seem to find the original. :-/

    class BootstrapFormBuilder < ActionView::Helpers::FormBuilder

  3. Robert L. Carpenter revised this gist Jan 30, 2012. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions bootstrap_pagination_helper.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@

    # this file probably belongs in /app/helpers/bootstrap_pagination_helper.rb
    #
    # This is sort of a compilation of several gists I found while looking for
    # something that was already written to do the job.
    #
    # A non-exhaustive list of gists which attempt to accomplish this and may or may
    # not have been used as a starting point for some or all of the code:
    # - https://gist.github.com/1182136
    # - https://gist.github.com/1205828

    module BootstrapPaginationHelper
    class LinkRenderer < WillPaginate::ActionView::LinkRenderer
    @@ -52,4 +59,4 @@ def link(text, target, attributes = {})
    tag(:li, tag(:a, text, attributes), :class => classname)
    end
    end
    end
    end
  4. Robert L. Carpenter created this gist Jan 30, 2012.
    14 changes: 14 additions & 0 deletions application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    # toss these two methods in your app/helpers/application_helper.rb

    #this is to be used instead of will_paginate's inbuilt view function and calls the BootstrapPaginationHelper
    def paginate *params
    params[1] = {} if params[1].nil?
    params[1][:renderer] = BootstrapPaginationHelper::LinkRenderer
    will_paginate *params
    end

    #this is to be used instead of form_for as a shortcut to calling BootstrapFormBuilder
    def bootstrapped_form object, options={}, &block
    options[:builder] = BootstrapFormBuilder
    form_for(object, options, &block)
    end
    75 changes: 75 additions & 0 deletions bootstrap_form_builder.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    # A good place to save this file is:
    # /app/form_builders/bootstrap_form_builder.rb

    class BootstrapFormBuilder < ActionView::Helpers::FormBuilder

    delegate :capture, :content_tag, :tag, to: :@template

    %w[text_field text_area password_field collection_select].each do |method_name|
    define_method(method_name) do |name, *args|
    options = args.extract_options!

    errors = object.errors[name].any?? 'error' : ''
    error_msg = object.errors[name].any?? content_tag(:span, object.errors[name].join(","), class: 'help-inline') : ''

    help_text = options[:help_text].blank? ? '' : content_tag(:span,options[:help_text], class: 'help-block')
    label = options[:label] == '' ? ''.html_safe : field_label(name, options)
    if options[:large] || method_name == 'text_area'
    options[:class] = [options[:class]] || []
    options[:class].push 'xxlarge'
    end

    content_tag :div, class: "clearfix #{errors}" do
    #field_label(name, options) + content_tag(:div, class: "input#{errors}") do
    label + content_tag(:div, class: "input #{errors}") do
    super(name, *(args.push(options))) + ' ' + error_msg
    end + help_text
    end
    end
    end

    def check_box(name, *args)
    content_tag :div, class:"clearfix" do
    content_tag(:div, class:"input") do
    content_tag(:ul, class:"inputs-list") do
    content_tag(:li) do
    content_tag(:label) do
    super(name, *args) + content_tag(:span) do
    field_label(name, *args)
    end
    end
    end
    end
    end
    end
    end

    def submit *args
    options = args.extract_options!
    args = args.push options

    if options[:clean]
    super *args
    else
    content_tag :div, class: :clearfix do
    content_tag :div, class: :input do
    super *args
    end
    end
    end

    end

    private

    def field_label(name, *args)
    options = args.extract_options!
    required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator}
    label(name, options[:label], class: ("required" if required))
    end

    def objectify_options(options)
    super.except(:label)
    end

    end
    55 changes: 55 additions & 0 deletions bootstrap_pagination_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@

    # this file probably belongs in /app/helpers/bootstrap_pagination_helper.rb

    module BootstrapPaginationHelper
    class LinkRenderer < WillPaginate::ActionView::LinkRenderer
    protected

    def page_number(page)
    unless page == current_page
    link(page, page, :rel => rel_value(page))
    else
    link(page, "#", :class => 'active')
    end
    end

    def gap
    text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
    %(<li class="disabled"><a>#{text}</a></li>)
    end

    def next_page
    num = @collection.current_page < @collection.total_pages && @collection.current_page + 1
    previous_or_next_page(num, @options[:next_label], 'next')
    end

    def previous_or_next_page(page, text, classname)
    if page
    link(text, page, :class => classname)
    else
    link(text, "#", :class => classname + ' disabled')
    end
    end

    def html_container(html)
    tag(:div, tag(:ul, html), container_attributes)
    end

    private

    def link(text, target, attributes = {})
    if target.is_a? Fixnum
    attributes[:rel] = rel_value(target)
    target = url(target)
    end

    unless target == "#"
    attributes[:href] = target
    end

    classname = attributes[:class]
    attributes.delete(:classname)
    tag(:li, tag(:a, text, attributes), :class => classname)
    end
    end
    end