Permalink
Browse files

Use label text for :as=>:radio boolean fields in the Sequel plugin

  • Loading branch information...
1 parent 7bdcb11 commit e11473b8ffd3f0eca66987ca98684748956a8550 @jeremyevans committed Jul 14, 2011
Showing with 18 additions and 6 deletions.
  1. +2 −0 CHANGELOG
  2. +6 −1 lib/sequel/plugins/forme.rb
  3. +10 −5 spec/sequel_plugin_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Use label text for :as=>:radio boolean fields in the Sequel plugin (jeremyevans)
+
* Support overriding the true/false label and values for :as=>:radio boolean fields in the Sequel plugin (jeremyevans)
* Allow overriding the type of input for schema columns and associations in the Sequel plugin (jeremyevans)
@@ -365,7 +365,12 @@ def input_boolean(sch)
unless v.nil?
(v ? true_opts : false_opts)[:checked] = true
end
- [_input(:radio, true_opts), _input(:radio, false_opts)]
+ r = [_input(:radio, true_opts), _input(:radio, false_opts)]
+ if l = opts[:label]
+ r.unshift(": ") unless l.is_a?(::Forme::Raw)
+ r.unshift(l)
+ end
+ r
when :select
v = opts[:value] || obj.send(field)
opts[:value] = (v ? 't' : 'f') unless v.nil?
View
@@ -144,21 +144,26 @@ class AlbumInfo < Sequel::Model; end
end
specify "should use radio buttons for boolean fields if :as=>:radio is used" do
- @b.input(:platinum, :as=>:radio).to_s.should == '<label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
- @c.input(:platinum, :as=>:radio).to_s.should == '<label><input checked="checked" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
+ @b.input(:platinum, :as=>:radio).to_s.should == 'Platinum: <label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
+ @c.input(:platinum, :as=>:radio).to_s.should == 'Platinum: <label><input checked="checked" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
end
specify "should handle errors on radio buttons for boolean fields if :as=>:radio is used" do
@ab.errors.add(:platinum, 'foo')
- @b.input(:platinum, :as=>:radio).to_s.should == '<label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input checked="checked" class="error" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label><span class="error_message">foo</span>'
+ @b.input(:platinum, :as=>:radio).to_s.should == 'Platinum: <label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input checked="checked" class="error" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label><span class="error_message">foo</span>'
+ end
+
+ specify "should handle Raw :label options if :as=>:radio is used" do
+ @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />'.extend(Forme::Raw)).to_s.should == 'Foo:<br /><label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
+ @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />').to_s.should == 'Foo:&lt;br /&gt;: <label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
end
specify "should respect :true_label and :false_label options for boolean fields if :as=>:radio is used" do
- @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar").to_s.should == '<label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label>'
+ @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar").to_s.should == 'Platinum: <label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label>'
end
specify "should respect :true_value and :false_value options for boolean fields if :as=>:radio is used" do
- @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar").to_s.should == '<label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label>'
+ @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar").to_s.should == 'Platinum: <label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label>'
end
specify "should use a select box for many_to_one associations" do

0 comments on commit e11473b

Please sign in to comment.