Skip to content
Browse files

Fix handling of Raw :label option in for association fields with :as=…

…>:radio and :as=>:checkbox in the Sequel plugin
  • Loading branch information...
1 parent e11473b commit f295640c1cca26ecd754fb562d934380cb6283c7 @jeremyevans committed Jul 14, 2011
Showing with 24 additions and 8 deletions.
  1. +2 −0 CHANGELOG
  2. +12 −8 lib/sequel/plugins/forme.rb
  3. +10 −0 spec/sequel_plugin_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* 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)
* Support overriding the true/false label and values for :as=>:radio boolean fields in the Sequel plugin (jeremyevans)
View
20 lib/sequel/plugins/forme.rb
@@ -217,6 +217,15 @@ def handle_label(f)
opts[:label] = [opts[:label], form._tag(:abbr, {:title=>'required'}, '*')] if opts[:required]
end
+ # Add the label to the start of the array, returning the array.
+ def add_label(label, array)
+ if label
+ array.unshift(": ") unless label.is_a?(::Forme::Raw)
+ array.unshift(label)
+ end
+ array
+ end
+
# Update the attributes and options for any recognized validations
def handle_validations(f)
m = obj.model
@@ -291,7 +300,7 @@ def association_many_to_one(ref)
wrapper = form.transformer(:wrapper, opts)
opts.delete(:wrapper)
radios = opts.delete(:options).map{|l, pk| _input(:radio, opts.merge(:value=>pk, :wrapper=>tag_wrapper, :label=>l, :checked=>(pk == val)))}
- radios.unshift("#{label}: ")
+ add_label(label, radios)
wrapper ? wrapper.call(radios, _input(:radio, opts)) : radios
else
opts[:id] = form.namespaced_id(key) unless opts.has_key?(:id)
@@ -324,7 +333,7 @@ def association_one_to_many(ref)
wrapper = form.transformer(:wrapper, opts)
opts.delete(:wrapper)
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))}
- cbs.unshift("#{label}: ")
+ add_label(label, cbs)
wrapper ? wrapper.call(cbs, _input(:checkbox, opts)) : cbs
else
opts[:id] = form.namespaced_id(field) unless opts.has_key?(:id)
@@ -365,12 +374,7 @@ def input_boolean(sch)
unless v.nil?
(v ? true_opts : false_opts)[:checked] = true
end
- 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
+ add_label(opts[:label], [_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
10 spec/sequel_plugin_spec.rb
@@ -184,6 +184,11 @@ class AlbumInfo < Sequel::Model; end
@c.input(:artist, :as=>:radio).to_s.should == 'Artist: <label><input name="album[artist_id]" type="radio" value="1"/> a</label><label><input checked="checked" name="album[artist_id]" type="radio" value="2"/> d</label>'
end
+ specify "should handle Raw label for many_to_one associations with :as=>:radio option" do
+ @b.input(:artist, :as=>:radio, :label=>'Foo:<br />'.extend(Forme::Raw)).to_s.should == 'Foo:<br /><label><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label><label><input name="album[artist_id]" type="radio" value="2"/> d</label>'
+ @b.input(:artist, :as=>:radio, :label=>'Foo<br />').to_s.should == 'Foo&lt;br /&gt;: <label><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label><label><input name="album[artist_id]" type="radio" value="2"/> d</label>'
+ end
+
specify "should correctly use the forms wrapper for wrapping radio buttons for many_to_one associations with :as=>:radio option" do
@b = Forme::Form.new(@ab, :wrapper=>:li)
@b.input(:artist, :as=>:radio).to_s.should == '<li class="many_to_one">Artist: <label><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label><label><input name="album[artist_id]" type="radio" value="2"/> d</label></li>'
@@ -252,6 +257,11 @@ class AlbumInfo < Sequel::Model; end
@c.input(:tags, :as=>:checkbox).to_s.should == 'Tags: <label><input name="album[tag_pks][]" type="checkbox" value="1"/> s</label><label><input checked="checked" name="album[tag_pks][]" type="checkbox" value="2"/> t</label><label><input name="album[tag_pks][]" type="checkbox" value="3"/> u</label>'
end
+ specify "should handle Raw label for associations with :as=>:checkbox" do
+ @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />:'.extend(Forme::Raw)).to_s.should == 'Foo<br />:<label><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label><input name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
+ @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />').to_s.should == 'Foo&lt;br /&gt;: <label><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label><input name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
+ end
+
specify "should correctly use the forms wrapper for wrapping radio buttons for one_to_many associations with :as=>:checkbox option" do
@b = Forme::Form.new(@ab, :wrapper=>:li)
@b.input(:tracks, :as=>:checkbox).to_s.should == '<li class="one_to_many">Tracks: <label><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label><input name="album[track_pks][]" type="checkbox" value="3"/> o</label></li>'

0 comments on commit f295640

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