Class: Bootstrap3FormBuilder::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Bootstrap3FormBuilder::FormBuilder
- Defined in:
- lib/bootstrap3_form_builder/form_builder.rb
Class Method Summary collapse
-
.create_tagged_field(method_name) ⇒ Object
Generates form fields that work with Twitter Bootstrap 3.
Instance Method Summary collapse
-
#submit(label, *args) ⇒ Object
Replace form submit input with styled buttons.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bootstrap3_form_builder/form_builder.rb', line 13 def self.create_tagged_field(method_name) define_method(method_name) do |label, *args| = args. custom_label = [:label] || label.to_s.humanize [:title] = [:title] || custom_label.capitalize label_class = [:label_class] || "" validators = @object.class.validators_on(label) if validators.collect(&:class).include?(ActiveRecord::Validations::PresenceValidator) || validators.collect(&:class).include?(ActiveModel::Validations::PresenceValidator) [:required] = true end allow_pattern = true if [:pattern] allow_pattern = false end length_validator = (validators.select { |v| v.class == ActiveModel::Validations::LengthValidator}).first if length_validator && allow_pattern maximum = length_validator.[:maximum] || length_validator.[:is] || (length_validator.[:in] ? length_validator.[:in].max : "") minimum = length_validator.[:minimum] || length_validator.[:is] || (length_validator.[:in] ? length_validator.[:in].min : "0") [:pattern] = ".{#{minimum},#{maximum}}" if minimum == "0" [:title] = "#{options[:title]} - #{maximum} characters maximum" elsif maximum == "" [:title] = "#{options[:title]} - #{minimum} characters minimum" elsif minimum == maximum [:title] = "#{options[:title]} - Must be exactly #{maximum} characters" else [:title] = "#{options[:title]} - #{minimum} to #{maximum} characters" end end inclusion_validator = (validators.select { |v| v.class == ActiveModel::Validations::InclusionValidator}).first if inclusion_validator && allow_pattern = inclusion_validator.[:in] || inclusion_validator.[:within] [:pattern] = "(#{valid_options.join("|")})" [:title] = inclusion_validator.[:message] || "#{options[:title]} - Must be one of the following: #{valid_options.join(", ")}" end numericality_validator = (validators.select{ |v| v.class == ActiveModel::Validations::NumericalityValidator}).first if numericality_validator && ![:type] [:type] = "number" if allow_pattern [:pattern] = "\\d*" end if numericality_validator.[:only_integer] [:step] = 1 else [:step] = "any" end end format_validator = (validators.select { |v| v.class == ActiveModel::Validations::FormatValidator}).first if format_validator && allow_pattern && !method_name.to_s.include?('email') [:pattern] = format_validator.[:with].source.html_safe [:title] = format_validator.[:message] || "#{options[:title]} is not a valid format" end control_group_class = "form-group" if @object.errors.[label] control_group_class = control_group_class + " error" end if ![:class] [:class] = "form-control" end input_prefix = "" input_suffix = "" if [:input_prefix] input_prefix = @template.content_tag("div", [:input_prefix], :class => "input-group-addon") end if [:input_suffix] input_suffix = @template.content_tag("div", [:input_suffix], :class => "input-group-addon") end if !input_prefix.empty? || !input_suffix.empty? input = @template.content_tag("div", input_prefix.html_safe + super(label, *(args << )) + input_suffix.html_safe, :class => "input-group " + ([:input_container_class] || "")) else input = super(label, *(args << )) if [:input_container_class] input = @template.content_tag("div", input.html_safe, :class => [:input_container_class]) end end input = input.html_safe + ([:help_block] ? @template.content_tag("p", [:help_block], :class => "help-block") : "" ) + ([:help_inline] ? @template.content_tag("span", [:help_inline], :class => "help-inline") : "" ) @template.content_tag("div", @template.content_tag("label", custom_label, :for => "#{@object_name}_#{label}", :class => label_class) + input.html_safe, :class => control_group_class) 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) = args. new_class = [:class] || Bootstrap3FormBuilder.default_submit_style super(label, *(args << .merge(:class => new_class))) end |