Skip to content
Browse files

Support overriding the true/false label and values for :as=>:radio bo…

…olean fields in the Sequel plugin
  • Loading branch information...
1 parent 3ddcf37 commit 7bdcb1155249afcb454c54b84fd782d1390169ab @jeremyevans committed Jul 14, 2011
Showing with 17 additions and 7 deletions.
  1. +2 −0 CHANGELOG
  2. +7 −7 lib/sequel/plugins/forme.rb
  3. +8 −0 spec/sequel_plugin_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* 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)
* Add the label before the error option, so the error always comes after the label (jeremyevans)
View
14 lib/sequel/plugins/forme.rb
@@ -355,17 +355,17 @@ def input_boolean(sch)
case opts[:as]
when :radio
- yes_opts = opts.merge(:value=>'t', :label=>'Yes', :error=>nil)
- no_opts = opts.merge(:value=>'f', :label=>'No')
+ true_opts = opts.merge(:value=>opts[:true_value]||'t', :label=>opts[:true_label]||'Yes', :error=>nil)
+ false_opts = opts.merge(:value=>opts[:false_value]||'f', :label=>opts[:false_label]||'No')
if i = opts[:id]
- yes_opts[:id] = "#{i}_yes"
- no_opts[:id] = "#{i}_no"
+ true_opts[:id] = "#{i}_yes"
+ false_opts[:id] = "#{i}_no"
end
- v = opts[:value] || obj.send(field)
+ v = opts.has_key?(:value) ? opts[:value] : obj.send(field)
unless v.nil?
- (v ? yes_opts : no_opts)[:checked] = true
+ (v ? true_opts : false_opts)[:checked] = true
end
- [_input(:radio, yes_opts), _input(:radio, no_opts)]
+ [_input(:radio, true_opts), _input(:radio, false_opts)]
when :select
v = opts[:value] || obj.send(field)
opts[:value] = (v ? 't' : 'f') unless v.nil?
View
8 spec/sequel_plugin_spec.rb
@@ -153,6 +153,14 @@ class AlbumInfo < Sequel::Model; end
@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>'
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>'
+ 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>'
+ end
+
specify "should use a select box for many_to_one associations" do
@b.input(:artist).to_s.should == '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">a</option><option value="2">d</option></select></label>'
@c.input(:artist).to_s.should == '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a</option><option selected="selected" value="2">d</option></select></label>'

0 comments on commit 7bdcb11

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