Skip to content

Instantly share code, notes, and snippets.

@endoscient
Created October 12, 2011 22:01
Show Gist options
  • Save endoscient/1282779 to your computer and use it in GitHub Desktop.
Save endoscient/1282779 to your computer and use it in GitHub Desktop.
module Padrino
module Helpers
module FormBuilder # @private
class BootstrapFormBuilder < AbstractFormBuilder # @private
##
# StandardFormBuilder
#
# text_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
# text_area_block(:summary, { :class => 'long' }, { :class => 'wide-label' })
# password_field_block(:password, { :class => 'long' }, { :class => 'wide-label' })
# file_field_block(:photo, { :class => 'long' }, { :class => 'wide-label' })
# check_box_block(:remember_me, { :class => 'long' }, { :class => 'wide-label' })
# select_block(:color, :options => ['green', 'black'])
#
(self.field_types - [ :hidden_field, :radio_button ]).each do |field_type|
class_eval <<-EOF
def #{field_type}_block(field, options={}, label_options={})
div_options = { :class => 'clearfix' }
error = object.errors[field] rescue nil
div_options[:class] = 'clearfix error' unless error.empty?
label_options.reverse_merge!(:caption => options.delete(:caption)) if options[:caption]
field_html = label(field, label_options)
field_html << @template.content_tag(:div, #{field_type}(field, options) + error_message_on(field, :class => 'help-inline'), :class => 'input')
@template.content_tag(:div, field_html, div_options)
end
EOF
end
# f.label :username, :caption => "Nickname"
def label(field, options={})
options.reverse_merge!(:caption => "#{field_human_name(field)}")
@template.label_tag(field_id(field), options)
end
# submit_block("Update")
def submit_block(caption, options={})
options.reverse_merge! :class =>'btn primary'
submit_html = self.submit(caption, options)
if options[:cancel]
submit_html << " " + @template.content_tag(:a, 'Cancel', { :href => options[:cancel], :class => 'btn' })
end
@template.content_tag(:div, submit_html, :class => 'actions')
end
# image_submit_block("submit.png")
def image_submit_block(source, options={})
options.reverse_merge! :class =>'btn primary'
submit_html = self.image_submit(source, options)
@template.content_tag(:p, submit_html)
end
end # MyFormBuilder
end # FormBuilder
end # Helpers
end # Padrino
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment