Permalink
Browse files

created example which doesn't use the JustinFrench::Formtastic namesp…

…ace and is a bit more abstract

Signed-off-by: Justin French <justin@indent.com.au>
  • Loading branch information...
1 parent f348e12 commit 6ba950e23750d7d3e7d72872a202556d0f92460d @markmansour markmansour committed with Jan 14, 2009
Showing with 24 additions and 30 deletions.
  1. +24 −30 README.textile
View
54 README.textile
@@ -193,47 +193,41 @@ Pretty soon we won't have to write any code at all ;)
h2. Extending Formtastic
-Adding functionality to Formtastic can be done by extending SemanticFormHelper.
+Adding functionality to Formtastic can be done by extending SemanticFormBuilder and configuring formtastic's builder.
+
+To create a new form method that displayed a star rating define the new Formtastic form builder by subclassing SemanticFormBuilder:
<pre>
-module JustinFrench #:nodoc:
- module Formtastic #:nodoc:
- class FancySemanticFormBuilder < SemanticFormBuilder
-
- # new method to be added allowing full control over the label and value
- def label_and_value(label, value, options = {})
- options[:label] = label
- content = input_label(label, options)
- content += @template.content_tag(:span, value, :id => "#{@object_name}_#{label}")
- @template.content_tag(:li, content)
- end
- end
+# RAILS_ROOT/lib/rating_semantic_form_builder.rb
+class RatingSemanticFormBuilder < JustinFrench::Formtastic::SemanticFormBuilder
+ # new method to be added allowing full control over the label and value
+ def rating(stars, options = {})
+ options[:label] ||= 'rating'
+ content = input_label(options[:label], options)
+ content += @template.content_tag(:span, '*' * stars.to_i, :id => "#{@object_name}_#{options[:label]}")
+ @template.content_tag(:li, content)
end
end
</pre>
-To use this builder specify the builder name when defining the form.
-
+Set Formtastic's builder to your newly defined RatingSemanticFormBuilder
<pre>
- form_for(@post, :builder => JustinFrench::Formtastic::FancySemanticFormBuilder))
-</pre>
+# RAILS_ROOT/config/initializers/override_formtastic_builder.rb
-The extended form builder can be placed in the RAILS_ROOT/lib directory and can be required in a Rails initializer.
+require 'rating_semantic_form_builder.rb'
-If you want to make the extended form builder the default for the semantic_form_* methods then the SemanticFormBuilder can overridden to specify a new semantic_form_builder.
+# set the formtastic builder to a customized version of the formtastic builder
+JustinFrench::Formtastic::SemanticFormHelper.builder = RatingSemanticFormBuilder
+</pre>
+Use your newly created formtastic method
<pre>
-module JustinFrench #:nodoc:
- module Formtastic #:nodoc:
- module TextEnabledSemanticFormHelper
- def semantic_form_builder
- JustinFrench::Formtastic::TextEnabledSemanticFormBuilder
- end
- end
- end
-end
-
-ActionView::Base.send :include, JustinFrench::Formtastic::TextEnabledSemanticFormHelper
+ <% semantic_form_for Project.new do |form| %>
+ <% form.input_field_set :name => "ratings" do %>
+ <%= form.rating 5 %>
+ <%= form.rating 11, :label => 'awesomeness (out of 10)' %>
+ <% end %>
+ <% end %>
</pre>
h2. Conventions & Prerequisites

0 comments on commit 6ba950e

Please sign in to comment.