Permalink
Browse files

in progress

  • Loading branch information...
1 parent 50149b0 commit 19f075dbd4beb9b146330adf3c1fe8badc822989 @MikeBlyth committed Jan 21, 2013
Showing with 63 additions and 0 deletions.
  1. +63 −0 app/form builders/bootstrap_form_builder.rb
@@ -0,0 +1,63 @@
+# Based on Railscasts episode 311, by Ryan Bates
+# LIMITATION: tag ID is formed from name, so will not be unique if the same name is used
+# more than once on a page!
+
+class LabeledFormBuilder < ActionView::Helpers::FormBuilder
+ delegate :content_tag, :tag, to: :@template
+
+ %w[text_field text_area password_field collection_select].each do |method_name|
+ define_method(method_name) do |name, *args|
+ content_tag :div, class: "field" do
+ field_label(name, *args) + tag(:br) + super(name, *args)
+ end
+ end
+ end
+
+ def check_box(name, *args)
+ content_tag :div, class: "field" do
+ super + " " + field_label(name, *args)
+ end
+ end
+
+ def collection_check_boxes(attribute, records, record_id, record_name)
+ content_tag :div, class: "field" do
+ @template.hidden_field_tag("#{object_name}[#{attribute}][]") +
+ records.map do |record|
+ element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
+ checkbox = @template.check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute).include?(record.send(record_id)), id: element_id)
+ checkbox + " " + @template.label_tag(element_id, record.send(record_name))
+ end.join(tag(:br)).html_safe
+ end
+ end
+
+ def submit(*args)
+ content_tag :div, class: "actions" do
+ super
+ end
+ end
+
+ def error_messages
+ if object.errors.full_messages.any?
+ content_tag(:div, :class => "error_messages") do
+ content_tag(:h2, "Invalid Fields") +
+ content_tag(:ul) do
+ object.errors.full_messages.map do |msg|
+ content_tag(:li, msg)
+ end.join.html_safe
+ end
+ end
+ end
+ end
+
+ private
+
+ def field_label(name, *args)
+ options = args.extract_options!
+ required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
+ label(name, options[:label], class: ("required" if required), for: "#{name}_id")
+ end
+
+ def objectify_options(options)
+ super.except(:label)
+ end
+end

0 comments on commit 19f075d

Please sign in to comment.