Skip to content
This repository
Browse code

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...
commit 6ba950e23750d7d3e7d72872a202556d0f92460d 1 parent f348e12
Mark Mansour markmansour authored committed

Showing 1 changed file with 24 additions and 30 deletions. Show diff stats Hide diff stats

  1. +24 30 README.textile
54 README.textile
Source Rendered
@@ -193,47 +193,41 @@ Pretty soon we won't have to write any code at all ;)
193 193
194 194 h2. Extending Formtastic
195 195
196   -Adding functionality to Formtastic can be done by extending SemanticFormHelper.
  196 +Adding functionality to Formtastic can be done by extending SemanticFormBuilder and configuring formtastic's builder.
  197 +
  198 +To create a new form method that displayed a star rating define the new Formtastic form builder by subclassing SemanticFormBuilder:
197 199
198 200 <pre>
199   -module JustinFrench #:nodoc:
200   - module Formtastic #:nodoc:
201   - class FancySemanticFormBuilder < SemanticFormBuilder
202   -
203   - # new method to be added allowing full control over the label and value
204   - def label_and_value(label, value, options = {})
205   - options[:label] = label
206   - content = input_label(label, options)
207   - content += @template.content_tag(:span, value, :id => "#{@object_name}_#{label}")
208   - @template.content_tag(:li, content)
209   - end
210   - end
  201 +# RAILS_ROOT/lib/rating_semantic_form_builder.rb
  202 +class RatingSemanticFormBuilder < JustinFrench::Formtastic::SemanticFormBuilder
  203 + # new method to be added allowing full control over the label and value
  204 + def rating(stars, options = {})
  205 + options[:label] ||= 'rating'
  206 + content = input_label(options[:label], options)
  207 + content += @template.content_tag(:span, '*' * stars.to_i, :id => "#{@object_name}_#{options[:label]}")
  208 + @template.content_tag(:li, content)
211 209 end
212 210 end
213 211 </pre>
214 212
215   -To use this builder specify the builder name when defining the form.
216   -
  213 +Set Formtastic's builder to your newly defined RatingSemanticFormBuilder
217 214 <pre>
218   - form_for(@post, :builder => JustinFrench::Formtastic::FancySemanticFormBuilder))
219   -</pre>
  215 +# RAILS_ROOT/config/initializers/override_formtastic_builder.rb
220 216
221   -The extended form builder can be placed in the RAILS_ROOT/lib directory and can be required in a Rails initializer.
  217 +require 'rating_semantic_form_builder.rb'
222 218
223   -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.
  219 +# set the formtastic builder to a customized version of the formtastic builder
  220 +JustinFrench::Formtastic::SemanticFormHelper.builder = RatingSemanticFormBuilder
  221 +</pre>
224 222
  223 +Use your newly created formtastic method
225 224 <pre>
226   -module JustinFrench #:nodoc:
227   - module Formtastic #:nodoc:
228   - module TextEnabledSemanticFormHelper
229   - def semantic_form_builder
230   - JustinFrench::Formtastic::TextEnabledSemanticFormBuilder
231   - end
232   - end
233   - end
234   -end
235   -
236   -ActionView::Base.send :include, JustinFrench::Formtastic::TextEnabledSemanticFormHelper
  225 + <% semantic_form_for Project.new do |form| %>
  226 + <% form.input_field_set :name => "ratings" do %>
  227 + <%= form.rating 5 %>
  228 + <%= form.rating 11, :label => 'awesomeness (out of 10)' %>
  229 + <% end %>
  230 + <% end %>
237 231 </pre>
238 232
239 233 h2. Conventions & Prerequisites

0 comments on commit 6ba950e

Please sign in to comment.
Something went wrong with that request. Please try again.