Skip to content

Commit

Permalink
Make table inputs_wrapper accept a :labels option and automatically s…
Browse files Browse the repository at this point in the history
…et up th tags with the labels for each column
  • Loading branch information
jeremyevans committed May 15, 2014
1 parent 7f2c62e commit 23f85bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD

* Make table inputs_wrapper accept a :labels option and automatically set up th tags with the labels for each column (jeremyevans)

* Form#inputs now accepts a :nested_inputs_wrapper option to set default inputs_wrapper transformer inside the block (jeremyevans)

* Add tr inputs_wrapper and td wrapper, for horizontal layout of inputs inside a table (jeremyevans)
Expand Down
13 changes: 8 additions & 5 deletions lib/forme.rb
Expand Up @@ -1265,13 +1265,16 @@ class InputsWrapper::Table
# Wrap the inputs in a table tag.
def call(form, opts, &block)
attr = opts[:attr] ? opts[:attr].dup : {}
if legend = opts[:legend]
form.tag(:table, attr) do
form.tag(:table, attr) do
if legend = opts[:legend]
form.emit(form.tag(:caption, opts[:legend_attr], legend))
yield
end
else
form.tag(:table, attr, &Proc.new)

if labels = opts[:labels]
form.emit(form.tag(:tr, {}, labels.map{|l| form._tag(:th, {}, l)}))
end

yield
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/forme_spec.rb
Expand Up @@ -711,6 +711,10 @@ def o.foo(t) t.form.tag(:input, :class=>t.attr[:class]) end
Forme::Form.new(:inputs_wrapper=>:table, :wrapper=>:trtd).inputs([:textarea], :legend=>'Inputs', :legend_attr=>{:class=>'foo'}).to_s.should == '<table><caption class="foo">Inputs</caption><tr><td><textarea></textarea></td><td></td></tr></table>'
end

specify "inputs_wrapper: table accepts a :labels option" do
Forme::Form.new(:inputs_wrapper=>:table).inputs(:labels=>%w'A B C').to_s.should == '<table><tr><th>A</th><th>B</th><th>C</th></tr></table>'
end

specify "serializer: html_usa formats dates and datetimes in American format without timezones" do
Forme::Form.new(:serializer=>:html_usa).tag(:div, :foo=>Date.new(2011, 6, 5)).to_s.should == '<div foo="06/05/2011"></div>'
Forme::Form.new(:serializer=>:html_usa).tag(:div, :foo=>DateTime.new(2011, 6, 5, 16, 3, 2)).to_s.should == '<div foo="06/05/2011 04:03:02PM"></div>'
Expand Down

0 comments on commit 23f85bb

Please sign in to comment.