Class: BootstrapFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/form_builders/bootstrap_form_builder.rb

Instance Method Summary collapse

Instance Method Details

#check_box(name) ⇒ Object



48
49
50
51
52
53
54
# File 'app/form_builders/bootstrap_form_builder.rb', line 48

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

#errors_forObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/form_builders/bootstrap_form_builder.rb', line 4

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

  messages = @object.errors.full_messages.map { |msg| (: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

#select(name, choices, options = {}) ⇒ Object



42
43
44
45
46
# File 'app/form_builders/bootstrap_form_builder.rb', line 42

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

#submit(value = nil) ⇒ Object



56
57
58
59
60
# File 'app/form_builders/bootstrap_form_builder.rb', line 56

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

#wrapper(name, options = {}, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/form_builders/bootstrap_form_builder.rb', line 62

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