Class: BootstrapFormBuilder::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/bootstrap_form_builder/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#collection_select(attribute, collection, value_method, text_method, options = {}) ⇒ Object



18
19
20
21
# File 'lib/bootstrap_form_builder/form_builder.rb', line 18

def collection_select(attribute, collection, value_method, text_method, options = {})
  select_tag = super
  field(select_tag, attribute, options)
end

#field(input_tag, attribute, options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bootstrap_form_builder/form_builder.rb', line 42

def field(input_tag, attribute, options)
  errors = object.errors[attribute].uniq.to_sentence
  outer_class = errors.blank? ? 'clearfix' : 'clearfix error'
  inner_class = errors.blank? ? 'input' : 'input error'

  template.(:div, id: "#{object.class.model_name.underscore}_#{attribute}_input", class: outer_class) do
    label(attribute, options[:label]) << template.(:div, class: inner_class) do
      input_div = input_tag
      input_div << template.(:span, options[:hint], class: 'help-block') if options[:hint]
      input_div << template.(:span, errors, class: 'help-inline') if errors.present?
      input_div
    end
  end
end

#file_field(attribute, options = {}, &block) ⇒ Object



13
14
15
16
# File 'lib/bootstrap_form_builder/form_builder.rb', line 13

def file_field(attribute, options = {}, &block)
  input_tag = block_given? ? template.capture(super, &block) : super
  field(input_tag, attribute, options)
end

#password_field(attribute, options = {}, &block) ⇒ Object



8
9
10
11
# File 'lib/bootstrap_form_builder/form_builder.rb', line 8

def password_field(attribute, options = {}, &block)
  input_tag = block_given? ? template.capture(super, &block) : super
  field(input_tag, attribute, options)
end

#submit(value = nil, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bootstrap_form_builder/form_builder.rb', line 28

def submit(value = nil, options = {})
  # This version of #submit differs slightly from the ActionView version in that
  # the model name is titleized to maintain consistent capitalization, e.g., for
  # a model named QuoteRequest the button label is "Create Quote Request" rather
  # than "Create Quote request".
  value ||= "#{object.new_record? ? 'Create' : 'Update'} #{object.class.model_name.titleize}"
  button_class = ['btn', *options.delete(:class)].join(' ')
  template. :div, class: 'clearfix' do
    template. :div, class: 'actions' do
      super(value, options.merge(class: button_class))
    end
  end
end

#text_field(attribute, options = {}, &block) ⇒ Object



3
4
5
6
# File 'lib/bootstrap_form_builder/form_builder.rb', line 3

def text_field(attribute, options = {}, &block)
  input_tag = block_given? ? template.capture(super, &block) : super
  field(input_tag, attribute, options)
end

#uneditable_field(attribute, options = {}) ⇒ Object



23
24
25
26
# File 'lib/bootstrap_form_builder/form_builder.rb', line 23

def uneditable_field(attribute, options = {})
  span_tag = template.(:span, object.send(attribute), class: 'uneditable-input', id: "#{object.class.model_name.underscore}_#{attribute}")
  field(span_tag, attribute, options)
end