Sha256: be53f1ae343d20ea3a4f4e97b44944221cd964c48f74fb39b9b0d3b2620383e6

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
  delegate :content_tag, :safe_join, to: :@template

  def errors_for
    return "" if @object.errors.empty?

    messages = @object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
    sentence = I18n.t(
      "errors.template.header",
      :count => @object.errors.count,
      :model => @object.class.model_name.human
    )

    html = <<-HTML
      <div class="alert alert-error">
        <p><b>#{sentence}</b></p>
        <ul>#{messages}</ul>
      </div>
    HTML

    html.html_safe
  end

  %w[email_field password_field text_field text_area].each do |method_name|
    define_method method_name do |*args|
      options = args.extract_options!
      name = args.first

      wrapper name, options.slice(:hint) do
        options = options.except(:hint)
        options[:class] = [options[:class], 'input-xxlarge'].compact

        if method_name == 'text_area'
          super(name, options.merge(rows: 7))
        else
          super(name, options)
        end
      end
    end
  end

  def select(name, choices, options = {})
    wrapper name do
      super
    end
  end

  def check_box(name)
    wrapper name do
      content_tag :label, class: 'checkbox' do
        super(name)
      end
    end
  end

  def submit(value=nil)
    content_tag :div, class: 'form-actions' do
      super value, class: 'btn btn-primary'
    end
  end

  def wrapper(name, options = {}, &block)
    content_tag :div, class: ['control-group', ('error' if @object.errors[name].present?)] do
      safe_join [
        label(name, class: 'control-label'),
        content_tag(:div, class: 'controls'){
          safe_join [
            yield,
            (content_tag(:p, options[:hint], class: 'help-block') if options[:hint].present?)
          ]
        }
      ]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
booter-0.3.1 app/form_builders/bootstrap_form_builder.rb