Permalink
Browse files

Add id attributes for association :as=>:radio or :as=>:checkbox fields

  • Loading branch information...
1 parent c57d3f6 commit efb125d0e694ea81b61314d924863d5114793a82 @jeremyevans committed Sep 1, 2011
Showing with 18 additions and 16 deletions.
  1. +2 −0 CHANGELOG
  2. +2 −2 lib/sequel/plugins/forme.rb
  3. +14 −14 spec/sequel_plugin_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Add id attributes for association :as=>:radio or :as=>:checkbox fields (jeremyevans)
+
* Add an html class attribute for radio/checkbox labels in :as=>:radio or :as=>:checkbox fields (jeremyevans)
* Wrap text acting as a label in a span with class label for :as=>:radio or :as=>:checkbox fields (jeremyevans)
@@ -304,7 +304,7 @@ def association_many_to_one(ref)
label = opts.delete(:label)
val = opts.delete(:value)
wrapper, tag_wrapper = get_wrappers
- radios = opts.delete(:options).map{|l, pk| _input(:radio, opts.merge(:value=>pk, :wrapper=>tag_wrapper, :label=>l, :label_attr=>{:class=>:option}, :checked=>(pk == val)))}
+ radios = opts.delete(:options).map{|l, pk| _input(:radio, opts.merge(:value=>pk, :id=>"#{form.namespaced_id(key)}_#{pk}", :wrapper=>tag_wrapper, :label=>l, :label_attr=>{:class=>:option}, :checked=>(pk == val)))}
add_label(label, radios)
wrapper ? wrapper.call(radios, _input(:radio, opts)) : radios
else
@@ -335,7 +335,7 @@ def association_one_to_many(ref)
label = opts.delete(:label)
val = opts.delete(:value)
wrapper, tag_wrapper = get_wrappers
- cbs = opts.delete(:options).map{|l, pk| _input(:checkbox, opts.merge(:value=>pk, :wrapper=>tag_wrapper, :label=>l, :label_attr=>{:class=>:option}, :checked=>val.include?(pk), :no_hidden=>true))}
+ cbs = opts.delete(:options).map{|l, pk| _input(:checkbox, opts.merge(:value=>pk, :id=>"#{form.namespaced_id(field)}_#{pk}", :wrapper=>tag_wrapper, :label=>l, :label_attr=>{:class=>:option}, :checked=>val.include?(pk), :no_hidden=>true))}
add_label(label, cbs)
wrapper ? wrapper.call(cbs, _input(:checkbox, opts)) : cbs
else
View
@@ -194,23 +194,23 @@ class AlbumInfo < Sequel::Model; end
end
specify "should use a set of radio buttons for many_to_one associations with :as=>:radio option" do
- @b.input(:artist, :as=>:radio).to_s.should == '<span class="label">Artist</span><label class="option"><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input name="album[artist_id]" type="radio" value="2"/> d</label>'
- @c.input(:artist, :as=>:radio).to_s.should == '<span class="label">Artist</span><label class="option"><input name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input checked="checked" name="album[artist_id]" type="radio" value="2"/> d</label>'
+ @b.input(:artist, :as=>:radio).to_s.should == '<span class="label">Artist</span><label class="option"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label>'
+ @c.input(:artist, :as=>:radio).to_s.should == '<span class="label">Artist</span><label class="option"><input id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input checked="checked" id="album_artist_id_2" 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 == '<span class="label">Foo:<br /></span><label class="option"><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input name="album[artist_id]" type="radio" value="2"/> d</label>'
- @b.input(:artist, :as=>:radio, :label=>'Foo<br />').to_s.should == '<span class="label">Foo&lt;br /&gt;</span><label class="option"><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input name="album[artist_id]" type="radio" value="2"/> d</label>'
+ @b.input(:artist, :as=>:radio, :label=>'Foo:<br />'.extend(Forme::Raw)).to_s.should == '<span class="label">Foo:<br /></span><label class="option"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label>'
+ @b.input(:artist, :as=>:radio, :label=>'Foo<br />').to_s.should == '<span class="label">Foo&lt;br /&gt;</span><label class="option"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input id="album_artist_id_2" 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"><span class="label">Artist</span><label class="option"><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input name="album[artist_id]" type="radio" value="2"/> d</label></li>'
+ @b.input(:artist, :as=>:radio).to_s.should == '<li class="many_to_one"><span class="label">Artist</span><label class="option"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label><label class="option"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></li>'
end
specify "should support custom wrappers for many_to_one associations with :as=>:radio via :tag_wrapper option" do
@b = Forme::Form.new(@ab, :wrapper=>:li)
- @b.input(:artist, :as=>:radio, :wrapper=>proc{|t, i| i.tag(:div, {}, [t])}, :tag_wrapper=>proc{|t, i| i.tag(:span, {}, [t])}).to_s.should == '<div><span class="label">Artist</span><span><label class="option"><input checked="checked" name="album[artist_id]" type="radio" value="1"/> a</label></span><span><label class="option"><input name="album[artist_id]" type="radio" value="2"/> d</label></span></div>'
+ @b.input(:artist, :as=>:radio, :wrapper=>proc{|t, i| i.tag(:div, {}, [t])}, :tag_wrapper=>proc{|t, i| i.tag(:span, {}, [t])}).to_s.should == '<div><span class="label">Artist</span><span><label class="option"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></span><span><label class="option"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></span></div>'
end
specify "should respect an :options entry" do
@@ -262,28 +262,28 @@ class AlbumInfo < Sequel::Model; end
end
specify "should use multiple checkboxes for one_to_many associations if :as=>:checkbox" do
- @b.input(:tracks, :as=>:checkbox).to_s.should == '<span class="label">Tracks</span><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
- @c.input(:tracks, :as=>:checkbox).to_s.should == '<span class="label">Tracks</span><label class="option"><input name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
+ @b.input(:tracks, :as=>:checkbox).to_s.should == '<span class="label">Tracks</span><label class="option"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
+ @c.input(:tracks, :as=>:checkbox).to_s.should == '<span class="label">Tracks</span><label class="option"><input id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input checked="checked" id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
end
specify "should use multiple checkboxes for many_to_many associations if :as=>:checkbox" do
- @b.input(:tags, :as=>:checkbox).to_s.should == '<span class="label">Tags</span><label class="option"><input checked="checked" name="album[tag_pks][]" type="checkbox" value="1"/> s</label><label class="option"><input checked="checked" name="album[tag_pks][]" type="checkbox" value="2"/> t</label><label class="option"><input name="album[tag_pks][]" type="checkbox" value="3"/> u</label>'
- @c.input(:tags, :as=>:checkbox).to_s.should == '<span class="label">Tags</span><label class="option"><input name="album[tag_pks][]" type="checkbox" value="1"/> s</label><label class="option"><input checked="checked" name="album[tag_pks][]" type="checkbox" value="2"/> t</label><label class="option"><input name="album[tag_pks][]" type="checkbox" value="3"/> u</label>'
+ @b.input(:tags, :as=>:checkbox).to_s.should == '<span class="label">Tags</span><label class="option"><input checked="checked" id="album_tag_pks_1" name="album[tag_pks][]" type="checkbox" value="1"/> s</label><label class="option"><input checked="checked" id="album_tag_pks_2" name="album[tag_pks][]" type="checkbox" value="2"/> t</label><label class="option"><input id="album_tag_pks_3" name="album[tag_pks][]" type="checkbox" value="3"/> u</label>'
+ @c.input(:tags, :as=>:checkbox).to_s.should == '<span class="label">Tags</span><label class="option"><input id="album_tag_pks_1" name="album[tag_pks][]" type="checkbox" value="1"/> s</label><label class="option"><input checked="checked" id="album_tag_pks_2" name="album[tag_pks][]" type="checkbox" value="2"/> t</label><label class="option"><input id="album_tag_pks_3" 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 == '<span class="label">Foo<br />:</span><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
- @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />').to_s.should == '<span class="label">Foo&lt;br /&gt;</span><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
+ @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />:'.extend(Forme::Raw)).to_s.should == '<span class="label">Foo<br />:</span><label class="option"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label>'
+ @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />').to_s.should == '<span class="label">Foo&lt;br /&gt;</span><label class="option"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input id="album_track_pks_3" 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"><span class="label">Tracks</span><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input name="album[track_pks][]" type="checkbox" value="3"/> o</label></li>'
+ @b.input(:tracks, :as=>:checkbox).to_s.should == '<li class="one_to_many"><span class="label">Tracks</span><label class="option"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label><label class="option"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label><label class="option"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></li>'
end
specify "should support custom wrappers for one_to_many associations with :as=>:checkbox via :tag_wrapper option" do
@b = Forme::Form.new(@ab, :wrapper=>:li)
- @b.input(:tracks, :as=>:checkbox, :wrapper=>proc{|t, i| i.tag(:div, i.opts[:wrapper_attr], [t])}, :tag_wrapper=>proc{|t, i| i.tag(:span, {}, [t])}).to_s.should == '<div class="one_to_many"><span class="label">Tracks</span><span><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="1"/> m</label></span><span><label class="option"><input checked="checked" name="album[track_pks][]" type="checkbox" value="2"/> n</label></span><span><label class="option"><input name="album[track_pks][]" type="checkbox" value="3"/> o</label></span></div>'
+ @b.input(:tracks, :as=>:checkbox, :wrapper=>proc{|t, i| i.tag(:div, i.opts[:wrapper_attr], [t])}, :tag_wrapper=>proc{|t, i| i.tag(:span, {}, [t])}).to_s.should == '<div class="one_to_many"><span class="label">Tracks</span><span><label class="option"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></span><span><label class="option"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></span><span><label class="option"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></span></div>'
end
specify "should use a text field methods not backed by columns" do

0 comments on commit efb125d

Please sign in to comment.