Permalink
Browse files

Handle forms used in partials where template output is injected and a…

… block is not used

Basically, if a block is not used, capture to a string and return
that string.  This makes it so using <% does not inject into the
template if a block is not used.
  • Loading branch information...
1 parent b970eea commit 73a5e28c597d8f4a6560550a79d7d376e5717457 @jeremyevans committed May 1, 2012
Showing with 14 additions and 6 deletions.
  1. +8 −2 lib/forme.rb
  2. +3 −3 lib/forme/sinatra.rb
  3. +3 −1 spec/sinatra_integration_spec.rb
View
@@ -205,7 +205,7 @@ def self.form(obj=nil, attr={}, opts={}, &block)
button = opts[:button]
if ins || button
block = Proc.new do |form|
- form.inputs(ins, opts) if ins
+ form._inputs(ins, opts) if ins
yield form if block_given?
form.emit(form.button(button)) if button
end
@@ -354,7 +354,13 @@ def _input(*a)
# The given +opts+ are passed to the +inputs_wrapper+, and the default
# +inputs_wrapper+ supports a <tt>:legend</tt> option that is used to
# set the legend for the fieldset.
- def inputs(inputs=[], opts={})
+ def inputs(*a, &block)
+ _inputs(*a, &block)
+ end
+
+ # Internals of #inputs, should be used internally by the library, where #inputs
+ # is designed for external use.
+ def _inputs(inputs=[], opts={})
if inputs.is_a?(Hash)
opts = inputs.merge(opts)
inputs = []
View
@@ -27,7 +27,7 @@ def inputs(*a, &block)
if block
capture(block){super}
else
- super
+ capture{super}
end
end
@@ -61,8 +61,8 @@ def tag(type, attr={}, children=[], &block)
end
end
- def capture(block)
- buf_was, @output = @output, eval("@_out_buf", block.binding) || @output
+ def capture(block='')
+ buf_was, @output = @output, block.is_a?(Proc) ? (eval("@_out_buf", block.binding) || @output) : block
yield
ret = @output
@output = buf_was
@@ -43,6 +43,8 @@ class FormeSinatraTest < Sinatra::Base
n3
<% end %>
n4
+ <%= f.inputs([:first, :last], :legend=>'Foo') %>
+ n5
END
erb <<END
0
@@ -103,7 +105,7 @@ def o.puts(*) end
end
specify "#form should correctly handle situation where multiple templates are used with same form object" do
- sin_get('/nest_sep').should == "0 <form action=\"/baz\"> 1 <p>FBB</p> 2 n1 <div> n2 <input id=\"first\" name=\"first\" type=\"text\" value=\"foo\"/> <input id=\"last\" name=\"last\" type=\"text\" value=\"bar\"/> n3 </div> n4 3 </form>4"
+ sin_get('/nest_sep').should == "0 <form action=\"/baz\"> 1 <p>FBB</p> 2 n1 <div> n2 <input id=\"first\" name=\"first\" type=\"text\" value=\"foo\"/> <input id=\"last\" name=\"last\" type=\"text\" value=\"bar\"/> n3 </div> n4 <fieldset class=\"inputs\"><legend>Foo</legend><input id=\"first\" name=\"first\" type=\"text\" value=\"foo\"/><input id=\"last\" name=\"last\" type=\"text\" value=\"bar\"/></fieldset> n5 3 </form>4"
end
specify "#form should accept two hashes instead of requiring obj as first argument" do

0 comments on commit 73a5e28

Please sign in to comment.