Skip to content
Browse files

Handle case where object does not respond to forme_input

  • Loading branch information...
1 parent aa222e8 commit 889b27bd04e7f437976ffb51c99d52ffec7f0a35 @jeremyevans committed
Showing with 20 additions and 1 deletion.
  1. +7 −0 README.rdoc
  2. +5 −1 lib/forme.rb
  3. +8 −0 spec/forme_simple_spec.rb
View
7 README.rdoc
@@ -26,6 +26,13 @@ should return a <tt>Forme::Input</tt> instance.
# => Forme::Input.new(f, :text, :name=>'obj[field]', :id=>'obj_field', :value=>'foo')
f.input(:field) # '<input id="obj_field" name="obj[field]" type="text" value="foo"/>'
+If the object doesn't respond to +forme_input+, it falls back to creating text fields
+with the name and id set to the field name and the value set by calling the given method
+on the object.
+
+ f = Forme::Form.new([:foo])
+ f.input(:first) # '<input id="first" name="first" type="text" value="foo"/>'
+
= Main Classes
<tt>Forme::Form</tt> :: main object
View
6 lib/forme.rb
@@ -18,7 +18,11 @@ def initialize(obj=nil, opts={})
def input(field, opts={})
if obj
- obj.forme_input(self, field, opts)
+ if obj.respond_to?(:forme_input)
+ obj.forme_input(self, field, opts)
+ else
+ Input.new(self, :text, :name=>field, :id=>field, :value=>obj.send(field))
+ end
else
Input.new(self, field, opts)
end.serialize
View
8 spec/forme_simple_spec.rb
@@ -60,4 +60,12 @@ def forme_input(form, field, opts={})
f.input(:x).should == '<label>X: <textarea id="foo_x" name="foo[x]">&amp;foo</textarea></label>'
f.input(:y, :brain=>'no').should == '<label>Y: <input brain="no" id="foo_y" name="foo[y]" type="text" value="3"/></label>'
end
+
+ specify "should handle case where obj doesn't respond to forme_input" do
+ Forme::Form.new([:foo]).input(:first).should == '<input id="first" name="first" type="text" value="foo"/>'
+ obj = Class.new{attr_accessor :foo}.new
+ obj.foo = 'bar'
+ Forme::Form.new(obj).input(:foo).should == '<input id="foo" name="foo" type="text" value="bar"/>'
+ end
+
end

0 comments on commit 889b27b

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