Permalink
Browse files

Add support for configurations, groups of related transformers

  • Loading branch information...
jeremyevans committed Jun 16, 2011
1 parent 66d5711 commit a4fa65e397fe7d2b0adcaec70b753bde036ce8f2
Showing with 81 additions and 4 deletions.
  1. +24 −0 README.rdoc
  2. +31 −4 lib/forme.rb
  3. +26 −0 spec/forme_spec.rb
View
@@ -148,6 +148,30 @@ Forme ships with a bunch of built-in transformers that you can use:
:fieldset_ol :: use both a fieldset and an ol tag to wrap inputs
:table :: uses a table tag to wrap inputs, useful with :trtd wrapper
+== Configurations
+
+You can associate a group of transformers into a configuration. This allows you to
+specify a single :config option when creating a +Form+ and have it automatically
+set all the related transformers.
+
+There are a few configurations supported by default:
+
+:default :: All +default+ transformers
+:formtastic :: +fieldset_ol+ inputs_wrapper, +li+ wrapper, +explicit+ labeler
+
+You can register and use your own configurations easily:
+
+ Forme.register_config(:mine, :wrapper=>:li, :inputs_wrapper=>:ol, :serializer=>:html_usa)
+ Forme::Form.new(:config=>:mine)
+
+If you want to, you can base your configuration on an existing configuration:
+
+ Forme.register_config(:yours, :base=>:mine, :inputs_wrapper=>:fieldset_ol)
+
+You can mark a configuration as the default using:
+
+ Forme.default_config = :mine
+
= Sequel Support
Forme ships with a Sequel plugin (use <tt>Sequel::Model.plugin :forme</tt> to enable), that makes
View
@@ -65,11 +65,31 @@ module Forme
# Exception class for exceptions raised by Forme.
class Error < StandardError
end
+
+ @default_config = :default
+ class << self
+ # Set the default configuration to use if none is explicitly
+ # specified (default: :default).
+ attr_accessor :default_config
+ end
+
+ # Array of all supported transformer types.
+ TRANSFORMER_TYPES = [:formatter, :serializer, :wrapper, :error_handler, :labeler, :inputs_wrapper]
+ # Hash storing all configurations. Configurations are groups of related transformers,
+ # so that you can specify a single :config option when creating a +Form+ and have
+ # all of the transformers set from that.
+ CONFIGURATIONS = {:default=>{}}
+
# Main hash storing the registered transformers. Maps transformer type symbols to subhashes
# containing the registered transformers for that type. Those subhashes should have symbol
# keys and values that are either classes or objects that respond to +call+.
- TRANSFORMERS = {:formatter=>{}, :serializer=>{}, :wrapper=>{}, :error_handler=>{}, :labeler=>{}, :inputs_wrapper=>{}}
+ TRANSFORMERS = {}
+
+ TRANSFORMER_TYPES.each do |t|
+ CONFIGURATIONS[:default][t] = :default
+ TRANSFORMERS[t] = {}
+ end
# Register a new transformer with this library. Arguments:
# +type+ :: Transformer type symbol
@@ -86,6 +106,14 @@ def self.register_transformer(type, sym, obj=nil, &block)
TRANSFORMERS[type][sym] = obj||block
end
+ # Register a new configuration. Type is the configuration name symbol,
+ # and hash maps transformer type symbols to transformer name symbols.
+ def self.register_config(type, hash)
+ CONFIGURATIONS[type] = CONFIGURATIONS[hash.fetch(:base, :default)].merge(hash)
+ end
+
+ register_config(:formtastic, :wrapper=>:li, :inputs_wrapper=>:fieldset_ol, :labeler=>:explicit)
+
# Call <tt>Forme::Form.form</tt> with the given arguments and block.
def self.form(*a, &block)
Form.form(*a, &block)
@@ -191,9 +219,8 @@ def initialize(obj=nil, opts={})
if @obj && @obj.respond_to?(:forme_config)
@obj.forme_config(self)
end
- TRANSFORMERS.keys.each do |k|
- instance_variable_set(:"@#{k}", transformer(k, @opts.fetch(k, :default)))
- end
+ config = CONFIGURATIONS[@opts[:config]||Forme.default_config]
+ TRANSFORMER_TYPES.each{|k| instance_variable_set(:"@#{k}", transformer(k, @opts.fetch(k, config[k])))}
@nesting = []
end
View
@@ -391,6 +391,32 @@
end
end
+describe "Forme configurations" do
+ after do
+ Forme.default_config = :default
+ end
+
+ specify "config: :formastic uses fieldset_ol inputs_wrapper and li wrapper, and explicit labeler" do
+ Forme::Form.new(:config=>:formtastic).inputs([[:textarea, {:id=>:foo, :label=>'Foo'}]]).to_s.should == '<fieldset><ol><li><label for="foo">Foo</label><textarea id="foo"></textarea></li></ol></fieldset>'
+ end
+
+ specify "should be able to set a default configuration with Forme.default_config=" do
+ Forme.default_config = :formtastic
+ Forme::Form.new.inputs([[:textarea, {:id=>:foo, :label=>'Foo'}]]).to_s.should == '<fieldset><ol><li><label for="foo">Foo</label><textarea id="foo"></textarea></li></ol></fieldset>'
+ end
+
+ specify "should have #register_config register a configuration for later use" do
+ Forme.register_config(:foo, :wrapper=>:li, :labeler=>:explicit)
+ Forme::Form.new(:config=>:foo).input(:textarea, :id=>:foo, :label=>'Foo').to_s.should == '<li><label for="foo">Foo</label><textarea id="foo"></textarea></li>'
+ end
+
+ specify "should have #register_config support a :base option to base it on an existing config" do
+ Forme.register_config(:foo2, :labeler=>:default, :base=>:formtastic)
+ Forme::Form.new(:config=>:foo2).inputs([[:textarea, {:id=>:foo, :label=>'Foo'}]]).to_s.should == '<fieldset><ol><li><label>Foo: <textarea id="foo"></textarea></label></li></ol></fieldset>'
+ end
+
+end
+
describe "Forme object forms" do
specify "should handle a simple case" do
obj = Class.new{def forme_input(form, field, opts) form._input(:text, :name=>"obj[#{field}]", :id=>"obj_#{field}", :value=>"#{field}_foo") end}.new

0 comments on commit a4fa65e

Please sign in to comment.