Class: BootstrapBuilder::Builder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- BootstrapBuilder::Builder
- Defined in:
- lib/bootstrap_builder/builder.rb
Instance Method Summary collapse
- #check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
-
#element(label = ' ', value = '', type = 'text_field', &block) ⇒ Object
generic container for all things form.
- #error_messages ⇒ Object
- #error_messages_for(method) ⇒ Object
- #fields_for(record_or_name_or_array, *args, &block) ⇒ Object
- #hidden_field(method, options = {}, html_options = {}) ⇒ Object
- #radio_button(method, tag_value, options = {}) ⇒ Object
- #select(method, choices, options = {}, html_options = {}) ⇒ Object
-
#submit(value, options = {}, &block) ⇒ Object
f.submit ‘Log In’, :change_to_text => ‘Logging you in …’.
Instance Method Details
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
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 |
# File 'lib/bootstrap_builder/builder.rb', line 44 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") = () if [:values].present? values = .delete(:values).collect do |key, val| name = "#{object_name}[#{method}][]" id = "#{object_name}_#{method}_#{val.to_s.gsub(' ', '_').underscore}" { :field => super(method, .merge({:name => name, :id => id}), val, nil), :label_text => key, :id => id } end @template.render(:partial => "#{BootstrapBuilder.config.template_folder}/check_box", :locals => { :builder => self, :method => method, :values => values, :label_text => label_text(method, .delete(:label)), :help_block => @template.raw(.delete(:help_block)), :error_messages => (method) }) else render_field('check_box', method, , ) do super(method, , checked_value, unchecked_value) end end end |
#element(label = ' ', value = '', type = 'text_field', &block) ⇒ Object
generic container for all things form
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/bootstrap_builder/builder.rb', line 134 def element(label = ' ', value = '', type = 'text_field', &block) value += @template.capture(&block) if block_given? %{ <div class='control-group'> <label class='control-label'>#{label}</label> <div class='controls'> #{value} </div> </div> }.html_safe end |
#error_messages ⇒ Object
146 147 148 149 150 151 |
# File 'lib/bootstrap_builder/builder.rb', line 146 def if object && !object.errors.empty? = object.errors[:base].present? ? object.errors[:base]: 'There were some problems submitting this form. Please correct all the highlighted fields and try again' @template.content_tag(:div, , :class => 'form_error') end end |
#error_messages_for(method) ⇒ Object
153 154 155 156 157 |
# File 'lib/bootstrap_builder/builder.rb', line 153 def (method) if (object and object.respond_to?(:errors) and errors = object.errors[method] and !errors.empty?) errors.is_a?(Array) ? errors.first : errors end end |
#fields_for(record_or_name_or_array, *args, &block) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/bootstrap_builder/builder.rb', line 159 def fields_for(record_or_name_or_array, *args, &block) = args. [:builder] ||= BootstrapBuilder::Builder [:html] ||= {} [:html][:class] ||= self.[:html] && self.[:html][:class] super(record_or_name_or_array, *(args << ), &block) end |
#hidden_field(method, options = {}, html_options = {}) ⇒ Object
40 41 42 |
# File 'lib/bootstrap_builder/builder.rb', line 40 def hidden_field(method, = {}, = {}) super(method, ) end |
#radio_button(method, tag_value, options = {}) ⇒ Object
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 |
# File 'lib/bootstrap_builder/builder.rb', line 71 def (method, tag_value, = {}) case tag_value when Array choices = tag_value.collect do |choice| if !choice.is_a?(Array) choice = [choice, choice] elsif choice.length == 1 choice << choice[0] end { :field => super(method, choice[1], ), :label => choice[0], :id => "#{object_name}_#{method}_#{choice[1].to_s.gsub(' ', '_').underscore}" } end else choices = [{ :field => super(method, tag_value), :label => tag_value, :label_for => "#{object_name}_#{method}_#{tag_value.to_s.gsub(' ', '_').underscore}" }] end @template.render(:partial => "#{BootstrapBuilder.config.template_folder}/radio_button", :locals => { :builder => self, :method => method, :label_text => label_text(method, .delete(:label)), :choices => choices, :required => .delete(:required), :before_text => @template.raw(.delete(:before_text)), :after_text => @template.raw(.delete(:after_text)), :help_block => @template.raw(.delete(:help_block)), :error_messages => (method) }) end |
#select(method, choices, options = {}, html_options = {}) ⇒ Object
36 37 38 |
# File 'lib/bootstrap_builder/builder.rb', line 36 def select(method, choices, = {}, = {}) render_field('select', method, , ) { super(method, choices, , ) } end |
#submit(value, options = {}, &block) ⇒ Object
f.submit ‘Log In’, :change_to_text => ‘Logging you in …’
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bootstrap_builder/builder.rb', line 108 def submit(value, ={}, &block) after_text = @template.capture(&block) if block_given? # Add specific bootstrap class [:class] ||= '' [:class] += ' btn' unless [:class] =~ /btn-/ [:class] += ' btn-primary' end # Set the script to change the text if change_to_text = .delete(:change_to_text) [:class] += ' change_to_text' [:onclick] ||= '' [:onclick] = "$(this).closest('.submit').hide();$(this).closest('.submit').after($('<div class=submit>#{change_to_text}</div>'))" end @template.render(:partial => "#{BootstrapBuilder.config.template_folder}/submit", :locals => { :builder => self, :field => super(value, ), :after_text => after_text, :change_to_text => change_to_text }) end |