Skip to content
Browse files

Fix wrapping for :as=>:radio boolean fields to handle them like assoc…

…iation :as=>:radio fields
  • Loading branch information...
1 parent f295640 commit a54c4e9c1d87782fc20ddd61c15d7cf90516cbfb @jeremyevans committed Jul 14, 2011
Showing with 29 additions and 13 deletions.
  1. +2 −0 CHANGELOG
  2. +17 −9 lib/sequel/plugins/forme.rb
  3. +4 −4 spec/forme_spec.rb
  4. +6 −0 spec/sequel_plugin_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Fix wrapping for :as=>:radio boolean fields to handle them like association :as=>:radio fields (jeremyevans)
+
* Fix handling of Raw :label option in for association fields with :as=>:radio and :as=>:checkbox in the Sequel plugin (jeremyevans)
* Use label text for :as=>:radio boolean fields in the Sequel plugin (jeremyevans)
View
26 lib/sequel/plugins/forme.rb
@@ -226,6 +226,16 @@ def add_label(label, array)
array
end
+ # Unset the wrapper and tag_wrapper options and return a
+ # array with the wrapper and tag_wrapper to use. The tag_wrapper
+ # is for wrapping each individual tag.
+ def get_wrappers
+ tag_wrapper = opts.delete(:tag_wrapper) || :default
+ wrapper = form.transformer(:wrapper, opts)
+ opts.delete(:wrapper)
+ [wrapper, tag_wrapper]
+ end
+
# Update the attributes and options for any recognized validations
def handle_validations(f)
m = obj.model
@@ -296,9 +306,7 @@ def association_many_to_one(ref)
handle_label(field)
label = opts.delete(:label)
val = opts.delete(:value)
- tag_wrapper = opts.delete(:tag_wrapper) || :default
- wrapper = form.transformer(:wrapper, opts)
- opts.delete(:wrapper)
+ wrapper, tag_wrapper = get_wrappers
radios = opts.delete(:options).map{|l, pk| _input(:radio, opts.merge(:value=>pk, :wrapper=>tag_wrapper, :label=>l, :checked=>(pk == val)))}
add_label(label, radios)
wrapper ? wrapper.call(radios, _input(:radio, opts)) : radios
@@ -329,9 +337,7 @@ def association_one_to_many(ref)
if opts.delete(:as) == :checkbox
label = opts.delete(:label)
val = opts.delete(:value)
- tag_wrapper = opts.delete(:tag_wrapper) || :default
- wrapper = form.transformer(:wrapper, opts)
- opts.delete(:wrapper)
+ wrapper, tag_wrapper = get_wrappers
cbs = opts.delete(:options).map{|l, pk| _input(:checkbox, opts.merge(:value=>pk, :wrapper=>tag_wrapper, :label=>l, :checked=>val.include?(pk), :no_hidden=>true))}
add_label(label, cbs)
wrapper ? wrapper.call(cbs, _input(:checkbox, opts)) : cbs
@@ -364,8 +370,9 @@ def input_boolean(sch)
case opts[:as]
when :radio
- 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')
+ wrapper, tag_wrapper = get_wrappers
+ true_opts = opts.merge(:value=>opts[:true_value]||'t', :label=>opts[:true_label]||'Yes', :error=>nil, :wrapper=>tag_wrapper, :wrapper_attr=>{})
+ false_opts = opts.merge(:value=>opts[:false_value]||'f', :label=>opts[:false_label]||'No', :wrapper=>tag_wrapper, :wrapper_attr=>{})
if i = opts[:id]
true_opts[:id] = "#{i}_yes"
false_opts[:id] = "#{i}_no"
@@ -374,7 +381,8 @@ def input_boolean(sch)
unless v.nil?
(v ? true_opts : false_opts)[:checked] = true
end
- add_label(opts[:label], [_input(:radio, true_opts), _input(:radio, false_opts)])
+ array = add_label(opts[:label], [_input(:radio, true_opts), _input(:radio, false_opts)])
+ wrapper ? wrapper.call(array, _input(:radio, opts)) : array
when :select
v = opts[:value] || obj.send(field)
opts[:value] = (v ? 't' : 'f') unless v.nil?
View
8 spec/forme_spec.rb
@@ -446,18 +446,18 @@ def sel(opts, s)
describe "Forme registering custom transformers" do
specify "should have #register_transformer register a transformer object for later use" do
- Forme.register_transformer(:wrapper, :div, proc{|t, i| t.tag(:div, {}, [t])})
- Forme::Form.new(:wrapper=>:div).input(:textarea).to_s.should == '<div><textarea></textarea></div>'
+ Forme.register_transformer(:wrapper, :div2, proc{|t, i| i.tag(:div2, {}, [t])})
+ Forme::Form.new(:wrapper=>:div2).input(:textarea).to_s.should == '<div2><textarea></textarea></div2>'
end
specify "should have #register_transformer register a transformer block for later use" do
- Forme.register_transformer(:wrapper, :div1){|t, i| t.tag(:div1, {}, [t])}
+ Forme.register_transformer(:wrapper, :div1){|t, i| i.tag(:div1, {}, [t])}
Forme::Form.new(:wrapper=>:div1).input(:textarea).to_s.should == '<div1><textarea></textarea></div1>'
end
specify "should have #register_transformer raise an error if given a block and an object" do
proc do
- Forme.register_transformer(:wrapper, :div1, proc{|t, i| t}){|t| t.tag(:div1, {}, [t])}
+ Forme.register_transformer(:wrapper, :div1, proc{|t, i| t}){|t, i| i.tag(:div1, {}, [t])}
end.should raise_error(Forme::Error)
end
end
View
6 spec/sequel_plugin_spec.rb
@@ -148,6 +148,12 @@ class AlbumInfo < Sequel::Model; end
@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 wrap both inputs if :as=>:radio is used" do
+ @b = Forme::Form.new(@ab, :wrapper=>:li)
+ @b.input(:platinum, :as=>:radio).to_s.should == '<li class="boolean">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></li>'
+ @b.input(:platinum, :as=>:radio, :wrapper=>:div, :tag_wrapper=>:span).to_s.should == '<div class="boolean">Platinum: <span><label><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></span><span><label><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></span></div>'
+ 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 == '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>'

0 comments on commit a54c4e9

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