class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
delegate :content_tag, :safe_join, to: :@template
attr_reader :form_actions_content # only for modal forms
def modal_title
if @options[:modal]
if @options[:modal].is_a?(Hash) and @options[:modal].key?(:title)
@options[:modal][:title]
else
I18n.t('booter.modal.title.' + @object.class.model_name, default: @object.class.model_name.human)
end
end
end
def errors_for
return "" if @object.errors.empty?
messages = @object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t(
"errors.template.header",
:count => @object.errors.count,
:model => @object.class.model_name.human
)
html = <<-HTML
HTML
html.html_safe
end
%w[email_field file_field password_field text_field text_area].each do |method_name|
define_method method_name do |*args|
options = args.extract_options!
name = args.first
wrapper name, options.slice(:hint) do
options = options.except(:hint)
if method_name == 'text_area'
options[:rows] ||= 7
options[:cols] ||= 40
super(name, options)
else
super(name, options)
end
end
end
end
def actions &block
begin
@actions_scope = true
content = @template.capture(&block)
ensure
@actions_scope = false
end
form_actions_wrapper content
end
def select(name, choices, options = {})
wrapper name do
super
end
end
def check_box(name)
wrapper name do
content_tag :label, class: 'checkbox' do
super(name)
end
end
end
def submit value = nil, *args
form_actions_wrapper super(value, make_button_options(args, style: :primary))
end
def button value = nil, *args
form_actions_wrapper super(value, make_button_options(args))
end
def wrapper(name, options = {}, &block)
content_tag :div, class: ['control-group', ('error' if @object.errors[name].present?)] do
safe_join [
label(name, class: 'control-label'),
content_tag(:div, class: 'controls'){
safe_join [
yield,
(content_tag(:p, options[:hint], class: 'help-block') if options[:hint].present?)
]
}
]
end
end
private
def form_actions_wrapper(content)
if @actions_scope
content
else
if @options[:modal]
@form_actions_content = content
''
else
content_tag :div, content, class: 'form-actions'
end
end
end
def make_button_options args, *default_args
options = default_args.extract_options!
options.merge! args.extract_options!
options[:class] ||= 'btn'
if options[:style] && options[:style].to_sym != :none
options[:class] += " btn-#{options[:style]}"
end
options.except(:style)
end
end