Skip to content
Browse files

Join attribute values specified as arrays with spaces instead of the …

…empty string

This mostly comes up when doing something like :class=>[:foo, :bar]
  • Loading branch information...
1 parent 2356239 commit 8b9451a8a8d01b254bc5a90d96c7d29c9bf56032 @jeremyevans committed
Showing with 23 additions and 1 deletion.
  1. +2 −0 CHANGELOG
  2. +11 −1 lib/forme.rb
  3. +5 −0 spec/forme_spec.rb
  4. +5 −0 spec/sequel_plugin_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Join attribute values specified as arrays with spaces instead of the empty string (jeremyevans)
+
* Make Sinatra ERB integration work with partials (jeremyevans)
* Add id attributes for association :as=>:radio or :as=>:checkbox fields (jeremyevans)
View
12 lib/forme.rb
@@ -1112,11 +1112,21 @@ def h(string)
string.to_s.gsub(ESCAPE_HTML_PATTERN){|c| ESCAPE_HTML[c] }
end
+ # Join attribute values that are arrays with spaces instead of an empty
+ # string.
+ def attr_value(v)
+ if v.is_a?(Array)
+ v.map{|c| attr_value(c)}.join(' ')
+ else
+ call(v)
+ end
+ end
+
# Transforms the +tag+'s attributes into an html string, sorting by the keys
# and quoting and html escaping the values.
def attr_html(tag)
attr = tag.attr.to_a.reject{|k,v| v.nil?}
- " #{attr.map{|k, v| "#{k}=\"#{call(v)}\""}.sort.join(' ')}" unless attr.empty?
+ " #{attr.map{|k, v| "#{k}=\"#{attr_value(v)}\""}.sort.join(' ')}" unless attr.empty?
end
end
View
5 spec/forme_spec.rb
@@ -547,6 +547,11 @@ def forme_input(form, field, opts={})
Forme.form{|f| f.should be_a_kind_of(Forme::Form)}
end
+ specify "should respect an array of classes" do
+ Forme.form(:class=>[:foo, :bar]).to_s.should == '<form class="foo bar"></form>'
+ Forme.form(:class=>[:foo, [:bar, :baz]]).to_s.should == '<form class="foo bar baz"></form>'
+ end
+
specify "should have inputs called instead the block be added to the existing form" do
Forme.form{|f| f.input(:text)}.to_s.should == '<form><input type="text"/></form>'
end
View
5 spec/sequel_plugin_spec.rb
@@ -17,6 +17,11 @@
@b.form(:class=>:foo, :method=>:get).to_s.should == '<form class="foo forme album" method="get"></form>'
end
+ specify "should allow an array of classes" do
+ @b.form(:class=>[:foo, :bar]).to_s.should == '<form class="foo bar forme album" method="post"></form>'
+ @b.form(:class=>[:foo, [:bar, :baz]]).to_s.should == '<form class="foo bar baz forme album" method="post"></form>'
+ end
+
specify "should use a text field for strings" do
@b.input(:name).to_s.should == '<label>Name: <input id="album_name" name="album[name]" type="text" value="b"/></label>'
@c.input(:name).to_s.should == '<label>Name: <input id="album_name" name="album[name]" type="text" value="c"/></label>'

0 comments on commit 8b9451a

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