Skip to content

Commit 353618e

Browse files
markmansourjustinfrench
authored andcommitted
Extending Formtastic can be done by overriding the semantic_form_builder method
Signed-off-by: Justin French <[email protected]>
1 parent a8f1644 commit 353618e

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
coverage
3+
*~

README.textile

+57-13
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ Hacked together forms were easy, but awesome forms with decent semantics, rich m
88

99
<pre>
1010
<% semantic_form_for @article do |form| %>
11-
11+
1212
<% form.inputs :name => "Basic" do %>
13-
<%= form.input :title %>
14-
<%= form.input :body %>
15-
<%= form.input :section_id %>
16-
<%= form.input :publication_state_id, :as => :radio %>
17-
<%= form.input :author_id, :as => :select %>
18-
<%= form.input :allow_comments, :label => "Allow commenting on this article" %>
19-
<% end %>
20-
13+
<%= form.input :title %>
14+
<%= form.input :body %>
15+
<%= form.input :section_id %>
16+
<%= form.input :publication_state_id, :as => :radio %>
17+
<%= form.input :author_id, :as => :select %>
18+
<%= form.input :allow_comments, :label => "Allow commenting on this article" %>
19+
<% end %>
20+
2121
<% form.inputs :name => "Advanced" do %>
22-
<%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
22+
<%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
2323
<%= form.input :extract, :required => false %>
2424
<%= form.input :description, :required => false %>
25-
<%= form.input :url_title, :required => false %>
25+
<%= form.input :url_title, :required => false %>
2626
<% end %>
2727

28-
<% form.buttons do %>
29-
<%= form.commit_button %>
28+
<% form.buttons do %>
29+
<%= form.commit_button %>
3030
<% end %>
3131

3232
<% end %>
@@ -191,6 +191,50 @@ You don't even have to specify the field list (Formtastic will simply render and
191191

192192
Pretty soon we won't have to write any code at all ;)
193193

194+
h2. Extending Formtastic
195+
196+
Adding functionality to Formtastic can be done by extending SemanticFormHelper.
197+
198+
<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_text, value, options = {})
205+
options[:label] = label_text
206+
content = text_label(label_text, options)
207+
content += @template.content_tag(:span, value)
208+
@template.content_tag(:li, content)
209+
end
210+
end
211+
end
212+
end
213+
</pre>
214+
215+
To use this builder specify the builder name when defining the form.
216+
217+
<pre>
218+
form_for(@post, :builder => JustinFrench::Formtastic::FancySemanticFormBuilder))
219+
</pre>
220+
221+
The extended form builder can be placed in the RAILS_ROOT/lib directory and can be required in a Rails initializer.
222+
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.
224+
225+
<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
237+
</pre>
194238

195239
h2. Conventions & Prerequisites
196240

lib/justin_french/formtastic.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ module Formtastic #:nodoc:
4242
# has too many dependencies on an ActiveRecord object being present.
4343
module SemanticFormHelper
4444
[:form_for, :fields_for, :form_remote_for, :remote_form_for].each do |meth|
45-
src = <<-END_SRC
45+
src = <<-END_SRC
4646
def semantic_#{meth}(record_or_name_or_array, *args, &proc)
4747
options = args.extract_options!
48-
options[:builder] = JustinFrench::Formtastic::SemanticFormBuilder
48+
options[:builder] = semantic_form_builder
4949
options[:html] ||= {}
5050
5151
class_names = options[:html][:class] ? options[:html][:class].split(" ") : []
@@ -59,11 +59,15 @@ def semantic_#{meth}(record_or_name_or_array, *args, &proc)
5959
6060
#{meth}(record_or_name_or_array, *(args << options), &proc)
6161
end
62+
63+
def semantic_form_builder
64+
JustinFrench::Formtastic::SemanticFormBuilder
65+
end
6266
END_SRC
6367
module_eval src, __FILE__, __LINE__
6468
end
6569
end
66-
70+
6771
class SemanticFormBuilder < ActionView::Helpers::FormBuilder
6872

6973
DEFAULT_TEXT_FIELD_SIZE = 50

0 commit comments

Comments
 (0)