Class: Bootstrap3FormBuilder::FormBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_tagged_field(method_name) ⇒ Object

Generates form fields that work with Twitter Bootstrap 3.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bootstrap3_form_builder/form_builder.rb', line 13

def self.create_tagged_field(method_name)
  define_method(method_name) do |label, *args|
    options = args.extract_options!

    custom_label = options[:label] || label.to_s.humanize
    options[:title] = options[:title] || custom_label.capitalize
    label_class = options[:label_class] || ""

    allow_pattern = allow_pattern?(options)

    validators = @object.class.validators_on(label).select { |validator| !validator.options.key? :if }

    set_presence_options(validators, options)
    set_length_options(validators, options)
    set_inclusion_options(validators, options)
    set_numericality_options(validators, options)
    set_format_options(validators, options, method_name)

    if !options[:class] && method_name != :check_box
      options[:class] = "form-control"
    end

    input_prefix = options[:input_prefix] ? @template.("div", options[:input_prefix], :class => "input-group-addon") : ""
    input_suffix = options[:input_suffix] ? @template.("div", options[:input_suffix], :class => "input-group-addon") : ""

    if !input_prefix.empty? || !input_suffix.empty?
      input = @template.("div",
                input_prefix.html_safe +
                super(label, *(args << options)) +
                input_suffix.html_safe,
                :class => "input-group " + (options[:input_container_class] || ""))
    else
      input = super(label, *(args << options))

      if options[:input_container_class]
        input = @template.("div", input.html_safe, :class => options[:input_container_class])
      end
    end

    input = input.html_safe +
            (options[:help_block] ? @template.("p", options[:help_block], :class => "help-block") : "" ) +
            (options[:help_inline] ? @template.("span", options[:help_inline], :class => "help-inline") : "" )

    if method_name == :check_box
      return @template.("div",
                @template.("label",
                  input.html_safe + custom_label),
                    :class => "checkbox")
    end

    @template.("div",
      @template.("label",
                custom_label,
                :for => "#{@object_name}_#{label}",
                :class => label_class)  + input.html_safe,
                  :class => control_group_class(label))
  end
end

Instance Method Details

#submit(label, *args) ⇒ Object

Replace form submit input with styled buttons



4
5
6
7
8
# File 'lib/bootstrap3_form_builder/form_builder.rb', line 4

def submit(label, *args)
  options = args.extract_options!
  new_class = options[:class] || Bootstrap3FormBuilder.default_submit_style
  super(label, *(args << options.merge(:class => new_class)))
end