-
-
Save allard/2670024 to your computer and use it in GitHub Desktop.
Revisions
-
allard revised this gist
Jun 5, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # # Changes: # 2012-06-05 allard: Added inline help messages, set default check_box label if none is set # # This file goes in lib/ # Usage: @@ -278,7 +278,7 @@ def basic_datetime_select(field, options = {}) labelOptions = {:class => 'checkbox'}.merge(options[:label_options] || {}) labelOptions["for"] = "#{@object_name}_#{field}" label_text = options[:label] || labelOptions[:text] || field.to_s.capitalize options.delete(:label) options.delete(:label_options) -
allard revised this gist
Jun 5, 2012 . 1 changed file with 18 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,7 @@ # # Changes: # 2012-06-05 allard: Added inline help messages # # This file goes in lib/ # Usage: # <%= bootstrap_form_for @calendar_entry do |f| %> @@ -14,6 +18,10 @@ # <%= f.submit 'Save', :class => 'btn btn-primary' %> # <%= link_to 'Cancel', calendar_entries_path, :class => 'btn' %> # </div> # <% end %> # # Inline help: # <%= f.text_field :name, :help => "inline help message" # module BootstrapFormBuilder module FormHelper @@ -62,6 +70,13 @@ def get_label(field, options) labelTag = label(field, text, labelOptions) end def inline_help(options) return "" unless options[:help] text = options[:help] options.delete(:help) content_tag :span, text, :class => 'help-inline' end def submit(value, options = {}, *args) super(value, {:class => "btn btn-primary"}.merge(options), *args) end @@ -218,6 +233,7 @@ def basic_datetime_select(field, options = {}) labelTag + "<div class='controls'>" + super(field, *args) + inline_help(options) + errorSpan + (help_block ? @template.capture(&help_block) : "") + "</div>" + @@ -243,6 +259,7 @@ def basic_datetime_select(field, options = {}) labelTag + "<div class='controls'>" + super(field, *args) + inline_help(options) + errorSpan + (help_block ? @template.capture(&help_block) : "") + "</div>" + @@ -276,6 +293,7 @@ def basic_datetime_select(field, options = {}) "<div class='controls'>" + tag(:label, labelOptions, true) + super(field, *args) + inline_help(options) + errorSpan + (help_block ? @template.capture(&help_block) : "") + label_text + -
allard revised this gist
May 14, 2012 . 1 changed file with 13 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,20 @@ # This file goes in lib/ # Usage: # <%= bootstrap_form_for @calendar_entry do |f| %> # <%= content_tag :legend, (locals[:title] || 'Edit Calendar Entry') %> # <%= f.text_field :name, :class => 'span3' %> # <%= f.text_area :description, :class => 'span3' %> # <%= f.jquery_datetime_select :start_time, :class => 'span3' %> # <%= f.jquery_datetime_select :end_time, :class => 'span3' %> # <%= f.check_box :all_day, { :label => 'Agree?, :description => 'left hand label'' } %> # <%= f.text_field :tag_string, :label_options => { :text => 'Tags' }, :class => 'span3' do %> # <p class="help-block">Use commas to separate tags.</p> # <% end %> # <div class="form-actions"> # <%= f.submit 'Save', :class => 'btn btn-primary' %> # <%= link_to 'Cancel', calendar_entries_path, :class => 'btn' %> # </div> # module BootstrapFormBuilder module FormHelper [:form_for, :fields_for].each do |method| @@ -52,6 +38,7 @@ module FormHelper class FormBuilder < ActionView::Helpers::FormBuilder include FormHelper include ActionView::Helpers::TagHelper def get_error_text(object, field, options) if object.nil? || options[:hide_errors] @@ -68,10 +55,10 @@ def get_object_id(field, options) end def get_label(field, options) labelOptions = {:class => 'control-label'}.merge(options[:label_options] || {}) text = options[:label] || labelOptions[:text] || nil options.delete(:label) options.delete(:label_options) labelTag = label(field, text, labelOptions) end @@ -272,10 +259,11 @@ def basic_datetime_select(field, options = {}) options = args.first.is_a?(Hash) ? args.first : {} object = @template.instance_variable_get("@#{@object_name}") labelOptions = {:class => 'checkbox'}.merge(options[:label_options] || {}) labelOptions["for"] = "#{@object_name}_#{field}" label_text = options[:label] || labelOptions[:text] || "" options.delete(:label) options.delete(:label_options) errorText = get_error_text(object, field, options) @@ -286,7 +274,7 @@ def basic_datetime_select(field, options = {}) ("<div class='#{wrapperClass}'>" + description + "<div class='controls'>" + tag(:label, labelOptions, true) + super(field, *args) + errorSpan + (help_block ? @template.capture(&help_block) : "") + @@ -298,4 +286,4 @@ def basic_datetime_select(field, options = {}) end end end end -
allard revised this gist
May 13, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -286,7 +286,7 @@ def basic_datetime_select(field, options = {}) ("<div class='#{wrapperClass}'>" + description + "<div class='controls'>" + "<label for='#{@object_name}_#{field}' class='control-group'>" + super(field, *args) + errorSpan + (help_block ? @template.capture(&help_block) : "") + -
allard revised this gist
May 13, 2012 . 1 changed file with 42 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ # = f.text_area :description, :class => 'span3' # = f.jquery_datetime_select :start_time, :class => 'span3' # = f.jquery_datetime_select :end_time, :class => 'span3' # = f.check_box :all_day, { :label_text => 'Agree?, :description => 'left hand label'' } # = f.text_field :tag_string, :label => {:text => 'Tags'}, :class => 'span3' # .form-actions # = f.submit 'Save', :class => 'btn btn-primary' @@ -20,7 +20,7 @@ # <%= f.text_area :description, :class => 'span3' %> # <%= f.jquery_datetime_select :start_time, :class => 'span3' %> # <%= f.jquery_datetime_select :end_time, :class => 'span3' %> # <%= f.check_box :all_day, { :label_text => 'Agree?, :description => 'left hand label'' } %> # <%= f.text_field :tag_string, :label => {:text => 'Tags'}, :class => 'span3' do %> # <p class="help-block">Use commas to separate tags.</p> # <% end %> @@ -69,7 +69,9 @@ def get_object_id(field, options) def get_label(field, options) labelOptions = {:class => 'control-label'}.merge(options[:label] || {}) text = options[:label_text] || labelOptions[:text] || nil options.delete(:label) options.delete(:label_text) labelTag = label(field, text, labelOptions) end @@ -205,9 +207,11 @@ def basic_datetime_select(field, options = {}) "</div>").html_safe end basic_helpers = %w{text_field text_area select email_field password_field number_field} multipart_helpers = %w{date_select datetime_select} trailing_label_helpers = %w{check_box} basic_helpers.each do |name| # First alias old method @@ -259,5 +263,39 @@ def basic_datetime_select(field, options = {}) ).html_safe end end trailing_label_helpers.each do |name| # First alias old method class_eval("alias super_#{name.to_s} #{name}") define_method(name) do |field, *args, &help_block| options = args.first.is_a?(Hash) ? args.first : {} object = @template.instance_variable_get("@#{@object_name}") labelOptions = {:class => 'control-label'}.merge(options[:label] || {}) label_text = options[:label_text] || labelOptions[:text] || "" options.delete(:label) options.delete(:label_text) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end description = if options[:description] then %{<label class="control-label">#{options[:description]}</label>} else "" end options.delete(:description) ("<div class='#{wrapperClass}'>" + description + "<div class='controls'>" + "<label for='#{field}' class='control-group'>" + super(field, *args) + errorSpan + (help_block ? @template.capture(&help_block) : "") + label_text + "</label>" + "</div>" + "</div>" ).html_safe end end end end -
jamiepenney revised this gist
May 9, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -205,7 +205,7 @@ def basic_datetime_select(field, options = {}) "</div>").html_safe end basic_helpers = %w{text_field text_area select email_field password_field check_box number_field} multipart_helpers = %w{date_select datetime_select} @@ -260,4 +260,4 @@ def basic_datetime_select(field, options = {}) end end end end -
jamiepenney revised this gist
May 1, 2012 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -73,6 +73,10 @@ def get_label(field, options) labelTag = label(field, text, labelOptions) end def submit(value, options = {}, *args) super(value, {:class => "btn btn-primary"}.merge(options), *args) end def jquery_date_select(field, options = {}) id = get_object_id(field, options) @@ -257,4 +261,3 @@ def basic_datetime_select(field, options = {}) end end end -
jamiepenney revised this gist
Apr 29, 2012 . 1 changed file with 72 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ # = f.jquery_datetime_select :start_time, :class => 'span3' # = f.jquery_datetime_select :end_time, :class => 'span3' # = f.check_box :all_day # = f.text_field :tag_string, :label => {:text => 'Tags'}, :class => 'span3' # .form-actions # = f.submit 'Save', :class => 'btn btn-primary' # = link_to 'Cancel', calendar_entries_path, :class => 'btn' @@ -21,7 +21,7 @@ # <%= f.jquery_datetime_select :start_time, :class => 'span3' %> # <%= f.jquery_datetime_select :end_time, :class => 'span3' %> # <%= f.check_box :all_day %> # <%= f.text_field :tag_string, :label => {:text => 'Tags'}, :class => 'span3' do %> # <p class="help-block">Use commas to separate tags.</p> # <% end %> # <div class="form-actions"> @@ -40,7 +40,7 @@ module FormHelper if method == :form_for options[:html] ||= {} options[:html][:class] ||= 'form-horizontal' end # call the original method with our overridden options @@ -62,14 +62,79 @@ def get_error_text(object, field, options) end end def get_object_id(field, options) object = @template.instance_variable_get("@#{@object_name}") return options[:id] || object.class.name.underscore + '_' + field.to_s end def get_label(field, options) labelOptions = {:class => 'control-label'}.merge(options[:label] || {}) text = labelOptions[:text] || nil labelTag = label(field, text, labelOptions) end def jquery_date_select(field, options = {}) id = get_object_id(field, options) date = if options['start_date'] options['start_date'] elsif object.nil? Date.now else object.send(field.to_sym) end date_picker_script = "<script type='text/javascript'>" + "$( function() { " + "$('##{id}')" + ".datepicker( $.datepicker.regional[ 'en-NZ' ] )" + ".datepicker( 'setDate', new Date('#{date}') ); } );" + "</script>" return basic_date_select(field, options.merge(javascript: date_picker_script)) end def basic_date_select(field, options = {}) placeholder_text = options[:placeholder_text] || '' id = get_object_id(field, options) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end labelTag = get_label(field, options) date = if options[:start_date] options[:start_date] elsif object.nil? Date.now.utc else object.send(field.to_sym) end javascript = options[:javascript] || " <script> $(function() { var el = $('##{id}'); var currentValue = el.val(); if(currentValue.trim() == '') return; el.val(new Date(currentValue).toString('dd MMM, yyyy')); }); </script>" ("<div class='#{wrapperClass}'>" + labelTag + "<div class='controls'>" + super_text_field(field, { :id => id, :placeholder => placeholder_text, :value => date.to_s, :class => options[:class] }.merge(options[:text_field] || {})) + errorSpan + javascript + "</div>" + "</div>").html_safe end def jquery_datetime_select(field, options = {}) @@ -191,4 +256,5 @@ def basic_datetime_select(field, options = {}) end end end end -
jamiepenney revised this gist
Mar 14, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ # = f.submit 'Save', :class => 'btn btn-primary' # = link_to 'Cancel', calendar_entries_path, :class => 'btn' # # If you don't use HAML, here is the same thing in ERB # <%= bootstrap_form_for @calendar_entry do |f| %> # <%= content_tag :legend, (locals[:title] || 'Edit Calendar Entry') %> # <%= f.text_field :name, :class => 'span3' %> -
glongman revised this gist
Mar 3, 2012 . 1 changed file with 22 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,23 @@ # = f.text_field :tag_string, :label => 'Tags', :class => 'span3' # .form-actions # = f.submit 'Save', :class => 'btn btn-primary' # = link_to 'Cancel', calendar_entries_path, :class => 'btn' # # I don't do HAML # <%= bootstrap_form_for @calendar_entry do |f| %> # <%= content_tag :legend, (locals[:title] || 'Edit Calendar Entry') %> # <%= f.text_field :name, :class => 'span3' %> # <%= f.text_area :description, :class => 'span3' %> # <%= f.jquery_datetime_select :start_time, :class => 'span3' %> # <%= f.jquery_datetime_select :end_time, :class => 'span3' %> # <%= f.check_box :all_day %> # <%= f.text_field :tag_string, :label => 'Tags', :class => 'span3' do %> # <p class="help-block">Use commas to separate tags.</p> # <% end %> # <div class="form-actions"> # <%= f.submit 'Save', :class => 'btn btn-primary' %> # <%= link_to 'Cancel', calendar_entries_path, :class => 'btn' %> # </div> module BootstrapFormBuilder module FormHelper @@ -128,7 +144,7 @@ def basic_datetime_select(field, options = {}) # First alias old method class_eval("alias super_#{name.to_s} #{name}") define_method(name) do |field, *args, &help_block| options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") @@ -143,14 +159,15 @@ def basic_datetime_select(field, options = {}) "<div class='controls'>" + super(field, *args) + errorSpan + (help_block ? @template.capture(&help_block) : "") + "</div>" + "</div>" ).html_safe end end multipart_helpers.each do |name| define_method(name) do |field, *args, &help_block| options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") @@ -167,10 +184,11 @@ def basic_datetime_select(field, options = {}) "<div class='controls'>" + super(field, *args) + errorSpan + (help_block ? @template.capture(&help_block) : "") + "</div>" + "</div>" ).html_safe end end end end -
jamiepenney revised this gist
Feb 25, 2012 . 1 changed file with 15 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,14 +46,18 @@ def get_error_text(object, field, options) end end def get_object_id(field) object = @template.instance_variable_get("@#{@object_name}") return options[:id] || object.class.name.underscore + '_' + field.to_s end def get_label(field, options) labelOptions = {:class => 'control-label'}.merge(options[:label] || {}) labelTag = label(field, labelOptions) end def jquery_datetime_select(field, options = {}) id = get_object_id(field, options) date_time = if options['start_time'] @@ -74,16 +78,14 @@ def jquery_datetime_select(field, options = {}) end def basic_datetime_select(field, options = {}) placeholder_text = options[:placeholder_text] || '' id = get_object_id(field, options) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end labelTag = get_label(field, options) date_time = if options[:start_time] @@ -106,7 +108,7 @@ def basic_datetime_select(field, options = {}) </script>" ("<div class='#{wrapperClass}'>" + labelTag + "<div class='controls'>" + super_text_field(field, { :id => id, :placeholder => placeholder_text, :value => date_time.to_s, @@ -130,15 +132,14 @@ def basic_datetime_select(field, options = {}) options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") labelTag = get_label(field, options) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end ("<div class='#{wrapperClass}'>" + labelTag + "<div class='controls'>" + super(field, *args) + errorSpan + @@ -153,8 +154,7 @@ def basic_datetime_select(field, options = {}) options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") labelTag = get_label(field, options) options[:class] = 'inline ' + options[:class] if options[:class] @@ -163,7 +163,7 @@ def basic_datetime_select(field, options = {}) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end ("<div class='#{wrapperClass}'>" + labelTag + "<div class='controls'>" + super(field, *args) + errorSpan + @@ -173,4 +173,4 @@ def basic_datetime_select(field, options = {}) end end end end -
jamiepenney revised this gist
Feb 25, 2012 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -81,7 +81,9 @@ def basic_datetime_select(field, options = {}) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end labelOptions = {:class => 'control-label'}.merge(options[:label] || {}) label = label(field, labelOptions) date_time = if options[:start_time] @@ -128,7 +130,7 @@ def basic_datetime_select(field, options = {}) options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") labelOptions = {:class => 'control-label'}.merge(options[:label] || {}) label = label(field, labelOptions) errorText = get_error_text(object, field, options) @@ -151,7 +153,7 @@ def basic_datetime_select(field, options = {}) options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") labelOptions = {:class => 'control-label'}.merge(options[:label] || {}) label = label(field, labelOptions) options[:class] = 'inline ' + options[:class] if options[:class] -
jamiepenney revised this gist
Feb 25, 2012 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -128,7 +128,9 @@ def basic_datetime_select(field, options = {}) options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") labelOptions = {:class => 'control-label'}.merge(options[:label]) label = label(field, labelOptions) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') @@ -149,7 +151,8 @@ def basic_datetime_select(field, options = {}) options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") labelOptions = {:class => 'control-label'}.merge(options[:label]) label = label(field, labelOptions) options[:class] = 'inline ' + options[:class] if options[:class] -
jamiepenney revised this gist
Feb 22, 2012 . 1 changed file with 28 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,32 @@ # This file goes in config/initializers require 'bootstrap_form_builder' # Make this the default Form Builder. You can delete this if you don't want form_for to use # the bootstrap form builder by default ActionView::Base.default_form_builder = BootstrapFormBuilder::FormBuilder # Add in our FormHelper methods, so you can use bootstrap_form_for. ActionView::Base.send :include, BootstrapFormBuilder::FormHelper ### Only use one of these error handling methods ### # Get rid of the rails error handling completely. ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag| "#{html_tag}".html_safe end # Only remove the default rails error handling on input and label # Relies on the Nokogiri gem. # Credit to https://github.com/ripuk ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, input" elements.each do |e| if e.node_name.eql? 'label' html = %(#{e}).html_safe elsif e.node_name.eql? 'input' html = %(#{e}).html_safe end end html end -
jamiepenney revised this gist
Feb 21, 2012 . 2 changed files with 2 additions and 172 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ # This file goes in lib/ # Usage: #= bootstrap_form_for @calendar_entry do |f| # %fieldset @@ -167,4 +168,4 @@ def basic_datetime_select(field, options = {}) end end end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,171 +0,0 @@ -
jamiepenney revised this gist
Feb 21, 2012 . 2 changed files with 176 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ # This file goes in config/initializers require 'bootstrap_form_builder' ActionView::Base.default_form_builder = BootstrapFormBuilder::FormBuilder ActionView::Base.send :include, BootstrapFormBuilder::FormHelper This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,171 @@ # This file goes in lib/ # Usage: #= bootstrap_form_for @calendar_entry do |f| # %fieldset # %legend= locals[:title] || 'Edit Calendar Entry' # = f.text_field :name, :class => 'span3' # = f.text_area :description, :class => 'span3' # = f.jquery_datetime_select :start_time, :class => 'span3' # = f.jquery_datetime_select :end_time, :class => 'span3' # = f.check_box :all_day # = f.text_field :tag_string, :label => 'Tags', :class => 'span3' # .form-actions # = f.submit 'Save', :class => 'btn btn-primary' # = link_to 'Cancel', calendar_entries_path, :class => 'btn' module BootstrapFormBuilder module FormHelper [:form_for, :fields_for].each do |method| module_eval do define_method "bootstrap_#{method}" do |record, *args, &block| # add the TwitterBootstrap builder to the options options = args.extract_options! options[:builder] = BootstrapFormBuilder::FormBuilder if method == :form_for options[:html] ||= {} options[:html][:class] = 'form-horizontal' end # call the original method with our overridden options send method, record, *(args << options), &block end end end end class FormBuilder < ActionView::Helpers::FormBuilder include FormHelper def get_error_text(object, field, options) if object.nil? || options[:hide_errors] "" else errors = object.errors[field.to_sym] if errors.empty? then "" else errors.first end end end def get_object_id(field) object = @template.instance_variable_get("@#{@object_name}") return options[:id] || object.class.name.underscore + '_' + field.to_s end def jquery_datetime_select(field, options = {}) id = get_object_id(field) date_time = if options['start_time'] options['start_time'] elsif object.nil? DateTime.now.utc else object.send(field.to_sym) end datetime_picker_script = "<script type='text/javascript'>" + "$( function() { " + "$('##{id}')" + ".datetimepicker( $.datepicker.regional[ 'en-NZ' ] )" + ".datetimepicker( 'setDate', new Date('#{date_time}') ); } );" + "</script>" return basic_datetime_select(field, options.merge(javascript: datetime_picker_script)) end def basic_datetime_select(field, options = {}) print options placeholder_text = options[:placeholder_text] || '' id = get_object_id(field) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end label = label(field, options[:label]) date_time = if options[:start_time] options[:start_time] elsif object.nil? DateTime.now.utc else object.send(field.to_sym) end javascript = options[:javascript] || " <script> $(function() { var el = $('##{id}'); var currentValue = el.val(); if(currentValue.trim() == '') return; el.val(new Date(currentValue).toString('dd MMM, yyyy HH:mm')); }); </script>" ("<div class='#{wrapperClass}'>" + label + "<div class='controls'>" + super_text_field(field, { :id => id, :placeholder => placeholder_text, :value => date_time.to_s, :class => options[:class] }.merge(options[:text_field] || {})) + errorSpan + javascript + "</div>" + "</div>").html_safe end basic_helpers = %w{text_field text_area select email_field password_field check_box} multipart_helpers = %w{date_select datetime_select} basic_helpers.each do |name| # First alias old method class_eval("alias super_#{name.to_s} #{name}") define_method(name) do |field, *args| options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") label = label(field, options[:label]) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end ("<div class='#{wrapperClass}'>" + label + "<div class='controls'>" + super(field, *args) + errorSpan + "</div>" + "</div>" ).html_safe end end multipart_helpers.each do |name| define_method(name) do |field, *args| options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") label = label(field, options[:label]) options[:class] = 'inline ' + options[:class] if options[:class] errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end ("<div class='#{wrapperClass}'>" + label + "<div class='controls'>" + super(field, *args) + errorSpan + "</div>" + "</div>" ).html_safe end end end end -
Jamie Penney created this gist
Feb 14, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,170 @@ # Usage: #= bootstrap_form_for @calendar_entry do |f| # %fieldset # %legend= locals[:title] || 'Edit Calendar Entry' # = f.text_field :name, :class => 'span3' # = f.text_area :description, :class => 'span3' # = f.jquery_datetime_select :start_time, :class => 'span3' # = f.jquery_datetime_select :end_time, :class => 'span3' # = f.check_box :all_day # = f.text_field :tag_string, :label => 'Tags', :class => 'span3' # .form-actions # = f.submit 'Save', :class => 'btn btn-primary' # = link_to 'Cancel', calendar_entries_path, :class => 'btn' module BootstrapFormBuilder module FormHelper [:form_for, :fields_for].each do |method| module_eval do define_method "bootstrap_#{method}" do |record, *args, &block| # add the TwitterBootstrap builder to the options options = args.extract_options! options[:builder] = BootstrapFormBuilder::FormBuilder if method == :form_for options[:html] ||= {} options[:html][:class] = 'form-horizontal' end # call the original method with our overridden options send method, record, *(args << options), &block end end end end class FormBuilder < ActionView::Helpers::FormBuilder include FormHelper def get_error_text(object, field, options) if object.nil? || options[:hide_errors] "" else errors = object.errors[field.to_sym] if errors.empty? then "" else errors.first end end end def get_object_id(field) object = @template.instance_variable_get("@#{@object_name}") return options[:id] || object.class.name.underscore + '_' + field.to_s end def jquery_datetime_select(field, options = {}) id = get_object_id(field) date_time = if options['start_time'] options['start_time'] elsif object.nil? DateTime.now.utc else object.send(field.to_sym) end datetime_picker_script = "<script type='text/javascript'>" + "$( function() { " + "$('##{id}')" + ".datetimepicker( $.datepicker.regional[ 'en-NZ' ] )" + ".datetimepicker( 'setDate', new Date('#{date_time}') ); } );" + "</script>" return basic_datetime_select(field, options.merge(javascript: datetime_picker_script)) end def basic_datetime_select(field, options = {}) print options placeholder_text = options[:placeholder_text] || '' id = get_object_id(field) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end label = label(field, options[:label]) date_time = if options[:start_time] options[:start_time] elsif object.nil? DateTime.now.utc else object.send(field.to_sym) end javascript = options[:javascript] || " <script> $(function() { var el = $('##{id}'); var currentValue = el.val(); if(currentValue.trim() == '') return; el.val(new Date(currentValue).toString('dd MMM, yyyy HH:mm')); }); </script>" ("<div class='#{wrapperClass}'>" + label + "<div class='controls'>" + super_text_field(field, { :id => id, :placeholder => placeholder_text, :value => date_time.to_s, :class => options[:class] }.merge(options[:text_field] || {})) + errorSpan + javascript + "</div>" + "</div>").html_safe end basic_helpers = %w{text_field text_area select email_field password_field check_box} multipart_helpers = %w{date_select datetime_select} basic_helpers.each do |name| # First alias old method class_eval("alias super_#{name.to_s} #{name}") define_method(name) do |field, *args| options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") label = label(field, options[:label]) errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end ("<div class='#{wrapperClass}'>" + label + "<div class='controls'>" + super(field, *args) + errorSpan + "</div>" + "</div>" ).html_safe end end multipart_helpers.each do |name| define_method(name) do |field, *args| options = args.last.is_a?(Hash) ? args.last : {} object = @template.instance_variable_get("@#{@object_name}") label = label(field, options[:label]) options[:class] = 'inline ' + options[:class] if options[:class] errorText = get_error_text(object, field, options) wrapperClass = 'control-group' + (errorText.empty? ? '' : ' error') errorSpan = if errorText.empty? then "" else "<span class='help-inline'>#{errorText}</span>" end ("<div class='#{wrapperClass}'>" + label + "<div class='controls'>" + super(field, *args) + errorSpan + "</div>" + "</div>" ).html_safe end end end end