forme 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,224 +15,285 @@ describe "Forme Sequel::Model forms" do
15
15
  super{|f| f.tag(:input, :type=>:hidden, :name=>'a', :value=>'b')}
16
16
  end
17
17
  end
18
- c.new(@ab).form.to_s.must_equal '<form class="forme album" method="post"><input name="a" type="hidden" value="b"/></form>'
18
+ c.new(@ab).form.must_equal '<form class="forme album" method="post"><input name="a" type="hidden" value="b"/></form>'
19
+ end
20
+
21
+ it "should have humanize handle objects that support #humanize" do
22
+ s = Object.new
23
+ def s.to_s; self end
24
+ def s.humanize; 'X' end
25
+ @b.humanize(s).must_equal 'X'
26
+ end
27
+
28
+ it "should have humanize handle objects that support #humanize" do
29
+ s = 'x_b_id'
30
+ class << s
31
+ undef :humanize if method_defined?(:humanize)
32
+ end
33
+ @b.humanize(s).must_equal 'X b'
19
34
  end
20
35
 
21
36
  it "should add appropriate attributes by default" do
22
- @b.form.to_s.must_equal '<form class="forme album" method="post"></form>'
37
+ @b.form.must_equal '<form class="forme album" method="post"></form>'
23
38
  end
24
39
 
25
40
  it "should allow overriding of attributes" do
26
- @b.form(:class=>:foo, :method=>:get).to_s.must_equal '<form class="foo forme album" method="get"></form>'
41
+ @b.form(:class=>:foo, :method=>:get).must_equal '<form class="foo forme album" method="get"></form>'
27
42
  end
28
43
 
29
44
  it "should handle invalid methods" do
30
45
  def @ab.db_schema
31
46
  super.merge(:foo=>{:type=>:bar})
32
47
  end
33
- @b.input(:foo, :value=>'baz').to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="text" value="baz"/></label>'
48
+ @b.input(:foo, :value=>'baz').must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="text" value="baz"/></label>'
34
49
  end
35
50
 
36
51
  it "should allow an array of classes" do
37
- @b.form(:class=>[:foo, :bar]).to_s.must_equal '<form class="foo bar forme album" method="post"></form>'
38
- @b.form(:class=>[:foo, [:bar, :baz]]).to_s.must_equal '<form class="foo bar baz forme album" method="post"></form>'
52
+ @b.form(:class=>[:foo, :bar]).must_equal '<form class="foo bar forme album" method="post"></form>'
53
+ @b.form(:class=>[:foo, [:bar, :baz]]).must_equal '<form class="foo bar baz forme album" method="post"></form>'
39
54
  end
40
55
 
41
56
  it "should use a text field for strings" do
42
- @b.input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label>'
43
- @c.input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="c"/></label>'
57
+ @b.input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label>'
58
+ @c.input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="c"/></label>'
44
59
  end
45
60
 
46
61
  it "should allow :as=>:textarea to use a textarea" do
47
- @b.input(:name, :as=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">b</textarea></label>'
48
- @c.input(:name, :as=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">c</textarea></label>'
62
+ @b.input(:name, :as=>:textarea).must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">b</textarea></label>'
63
+ @c.input(:name, :as=>:textarea).must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">c</textarea></label>'
49
64
  end
50
65
 
51
66
  it "should allow :type=>:textarea to use a textarea" do
52
- @b.input(:name, :type=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">b</textarea></label>'
53
- @c.input(:name, :type=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">c</textarea></label>'
67
+ @b.input(:name, :type=>:textarea).must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">b</textarea></label>'
68
+ @c.input(:name, :type=>:textarea).must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">c</textarea></label>'
69
+ end
70
+
71
+ it "should respect :value and :key options when using :type option" do
72
+ @b.input(:name, :type=>:textarea, :value=>'a', :key=>'f').must_equal '<label>Name: <textarea id="album_f" maxlength="255" name="album[f]">a</textarea></label>'
54
73
  end
55
74
 
56
75
  it "should not include labels for hidden inputs" do
57
- @b.input(:name, :type=>:hidden).to_s.must_equal '<input id="album_name" name="album[name]" type="hidden" value="b"/>'
76
+ @b.input(:name, :type=>:hidden).must_equal '<input id="album_name" name="album[name]" type="hidden" value="b"/>'
58
77
  end
59
78
 
60
- it "should use number inputs for integers" do
61
- @b.input(:copies_sold).to_s.must_equal '<label>Copies sold: <input id="album_copies_sold" name="album[copies_sold]" type="number" value="10"/></label>'
79
+ it "should use text input with inputmode and pattern for integer fields" do
80
+ @b.input(:copies_sold).must_equal '<label>Copies sold: <input id="album_copies_sold" inputmode="numeric" name="album[copies_sold]" pattern="-?[0-9]*" type="text" value="10"/></label>'
81
+ end
82
+
83
+ it "should use text input for numeric fields" do
84
+ @b.input(:bd).must_equal '<label>Bd: <input id="album_bd" name="album[bd]" type="text"/></label>'
85
+ end
86
+
87
+ it "should use text input float fields" do
88
+ @b.input(:fl).must_equal '<label>Fl: <input id="album_fl" name="album[fl]" type="text"/></label>'
62
89
  end
63
90
 
64
91
  it "should use date inputs for Dates" do
65
- @b.input(:release_date).to_s.must_equal '<label>Release date: <input id="album_release_date" name="album[release_date]" type="date" value="2011-06-05"/></label>'
92
+ @b.input(:release_date).must_equal '<label>Release date: <input id="album_release_date" name="album[release_date]" type="date" value="2011-06-05"/></label>'
66
93
  end
67
94
 
68
95
  it "should use datetime inputs for Time" do
69
- @b.input(:created_at).to_s.must_match %r{<label>Created at: <input id="album_created_at" name="album\[created_at\]" type="datetime-local" value="2011-06-05T00:00:00.000"/></label>}
96
+ @b.input(:created_at).must_match %r{<label>Created at: <input id="album_created_at" name="album\[created_at\]" type="datetime-local" value="2011-06-05T00:00:00.000"/></label>}
70
97
  end
71
98
 
72
99
  it "should use datetime inputs for DateTimes" do
73
100
  @ab.values[:created_at] = DateTime.new(2011, 6, 5)
74
- @b.input(:created_at).to_s.must_equal '<label>Created at: <input id="album_created_at" name="album[created_at]" type="datetime-local" value="2011-06-05T00:00:00.000"/></label>'
101
+ @b.input(:created_at).must_equal '<label>Created at: <input id="album_created_at" name="album[created_at]" type="datetime-local" value="2011-06-05T00:00:00.000"/></label>'
75
102
  end
76
103
 
77
104
  it "should use file inputs without value" do
78
- @b.input(:name, :type=>:file).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" type="file"/></label>'
105
+ @b.input(:name, :type=>:file).must_equal '<label>Name: <input id="album_name" name="album[name]" type="file"/></label>'
79
106
  end
80
107
 
81
108
  it "should include type as wrapper class" do
82
109
  @ab.values[:created_at] = DateTime.new(2011, 6, 5)
83
110
  f = Forme::Form.new(@ab, :wrapper=>:li)
84
- f.input(:name).to_s.must_equal '<li class="string"><label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label></li>'
85
- f.input(:release_date).to_s.must_equal '<li class="date"><label>Release date: <input id="album_release_date" name="album[release_date]" type="date" value="2011-06-05"/></label></li>'
86
- f.input(:created_at).to_s.must_equal '<li class="datetime"><label>Created at: <input id="album_created_at" name="album[created_at]" type="datetime-local" value="2011-06-05T00:00:00.000"/></label></li>'
111
+ f.input(:name).must_equal '<li class="string"><label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label></li>'
112
+ f.input(:release_date).must_equal '<li class="date"><label>Release date: <input id="album_release_date" name="album[release_date]" type="date" value="2011-06-05"/></label></li>'
113
+ f.input(:created_at).must_equal '<li class="datetime"><label>Created at: <input id="album_created_at" name="album[created_at]" type="datetime-local" value="2011-06-05T00:00:00.000"/></label></li>'
114
+ end
115
+
116
+ it "should handle :wrapper_attr to add attribues on the wrapper" do
117
+ @b.input(:name, :wrapper_attr=>{:foo=>'bar'}, :wrapper=>:div).must_equal '<div class="string" foo="bar"><label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label></div>'
118
+ end
119
+
120
+ it "should handle :key to override the key" do
121
+ @b.input(:name, :key=>:f).must_equal '<label>Name: <input id="album_f" maxlength="255" name="album[f]" type="text" value="b"/></label>'
87
122
  end
88
123
 
89
124
  it "should include required * in label if required" do
90
- @b.input(:name, :required=>true).to_s.must_equal '<label>Name<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label>'
125
+ @b.input(:name, :required=>true).must_equal '<label>Name<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label>'
91
126
  end
92
127
 
93
128
  it "should add required to label even if :label option specified" do
94
- @b.input(:name, :required=>true, :label=>'Foo').to_s.must_equal '<label>Foo<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label>'
129
+ @b.input(:name, :required=>true, :label=>'Foo').must_equal '<label>Foo<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label>'
95
130
  end
96
131
 
97
132
  it "should not add required to label even if :label option is nil" do
98
- @b.input(:name, :required=>true, :label=>nil).to_s.must_equal '<input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/>'
133
+ @b.input(:name, :required=>true, :label=>nil).must_equal '<input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/>'
99
134
  end
100
135
 
101
136
  it "should include required wrapper class if required" do
102
137
  f = Forme::Form.new(@ab, :wrapper=>:li)
103
- f.input(:name, :required=>true).to_s.must_equal '<li class="string required"><label>Name<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label></li>'
138
+ f.input(:name, :required=>true).must_equal '<li class="string required"><label>Name<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label></li>'
104
139
  end
105
140
 
106
141
  it "should use a select box for tri-valued boolean fields" do
107
- @b.input(:gold).to_s.must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
108
- @c.input(:gold).to_s.must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option selected="selected" value="t">True</option><option value="f">False</option></select></label>'
142
+ @b.input(:gold).must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
143
+ @c.input(:gold).must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option selected="selected" value="t">True</option><option value="f">False</option></select></label>'
144
+ end
145
+
146
+ it "should handle :value option for boolean fields" do
147
+ @b.input(:gold, :value=>nil).must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="t">True</option><option value="f">False</option></select></label>'
148
+ @b.input(:gold, :value=>true).must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option selected="selected" value="t">True</option><option value="f">False</option></select></label>'
149
+ @b.input(:gold, :value=>false).must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
109
150
  end
110
151
 
111
152
  it "should respect :true_label and :false_label options for tri-valued boolean fields" do
112
- @b.input(:gold, :true_label=>"Foo", :false_label=>"Bar").to_s.must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="t">Foo</option><option selected="selected" value="f">Bar</option></select></label>'
153
+ @b.input(:gold, :true_label=>"Foo", :false_label=>"Bar").must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="t">Foo</option><option selected="selected" value="f">Bar</option></select></label>'
113
154
  end
114
155
 
115
156
  it "should respect :true_value and :false_value options for tri-valued boolean fields" do
116
- @b.input(:gold, :true_value=>"Foo", :false_value=>"Bar").to_s.must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="Foo">True</option><option value="Bar">False</option></select></label>'
157
+ @b.input(:gold, :true_value=>"Foo", :false_value=>"Bar").must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value=""></option><option value="Foo">True</option><option value="Bar">False</option></select></label>'
117
158
  end
118
159
 
119
160
  it "should respect :add_blank option for tri-valued boolean fields" do
120
- @b.input(:gold, :add_blank=>'NULL').to_s.must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value="">NULL</option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
161
+ @b.input(:gold, :add_blank=>'NULL').must_equal '<label>Gold: <select id="album_gold" name="album[gold]"><option value="">NULL</option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
121
162
  end
122
163
 
123
164
  it "should use a select box for dual-valued boolean fields where :required => false" do
124
- @b.input(:platinum, :required=>false).to_s.must_equal '<label>Platinum: <select id="album_platinum" name="album[platinum]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
125
- @c.input(:platinum, :required=>false).to_s.must_equal '<label>Platinum: <select id="album_platinum" name="album[platinum]"><option value=""></option><option selected="selected" value="t">True</option><option value="f">False</option></select></label>'
165
+ @b.input(:platinum, :required=>false).must_equal '<label>Platinum: <select id="album_platinum" name="album[platinum]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
166
+ @c.input(:platinum, :required=>false).must_equal '<label>Platinum: <select id="album_platinum" name="album[platinum]"><option value=""></option><option selected="selected" value="t">True</option><option value="f">False</option></select></label>'
126
167
  end
127
168
 
128
169
  it "should use a checkbox for dual-valued boolean fields" do
129
- @b.input(:platinum).to_s.must_equal '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
130
- @c.input(:platinum).to_s.must_equal '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input checked="checked" id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
170
+ @b.input(:platinum).must_equal '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
171
+ @c.input(:platinum).must_equal '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input checked="checked" id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
172
+ end
173
+
174
+ it "should respect :value option for checkbox for dual-valued boolean fields" do
175
+ @b.input(:platinum, :value=>nil).must_equal '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
176
+ @b.input(:platinum, :value=>false).must_equal '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
177
+ @b.input(:platinum, :value=>true).must_equal '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input checked="checked" id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
131
178
  end
132
179
 
133
180
  it "should use radio buttons for boolean fields if :as=>:radio is used" do
134
- @b.input(:platinum, :as=>:radio).to_s.must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
135
- @c.input(:platinum, :as=>:radio).to_s.must_equal '<span class="label">Platinum</span><label class="option"><input checked="checked" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
181
+ @b.input(:platinum, :as=>:radio).must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
182
+ @c.input(:platinum, :as=>:radio).must_equal '<span class="label">Platinum</span><label class="option"><input checked="checked" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
183
+ end
184
+
185
+ it "should handle :value given if :as=>:radio is used" do
186
+ @b.input(:platinum, :as=>:radio, :value=>nil).must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
187
+ @b.input(:platinum, :as=>:radio, :value=>true).must_equal '<span class="label">Platinum</span><label class="option"><input checked="checked" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
188
+ @b.input(:platinum, :as=>:radio, :value=>false).must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
136
189
  end
137
190
 
138
191
  it "should wrap both inputs if :as=>:radio is used" do
139
192
  @b = Forme::Form.new(@ab, :wrapper=>:li)
140
- @b.input(:platinum, :as=>:radio).to_s.must_equal '<li class="boolean"><span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></li>'
141
- @b.input(:platinum, :as=>:radio, :wrapper=>:div, :tag_wrapper=>:span).to_s.must_equal '<div class="boolean"><span class="label">Platinum</span><span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></span><span><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></span></div>'
193
+ @b.input(:platinum, :as=>:radio).must_equal '<li class="boolean"><span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></li>'
194
+ @b.input(:platinum, :as=>:radio, :wrapper=>:div, :tag_wrapper=>:span).must_equal '<div class="boolean"><span class="label">Platinum</span><span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></span><span><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></span></div>'
142
195
  end
143
196
 
144
197
  it "should handle errors on radio buttons for boolean fields if :as=>:radio is used" do
145
198
  @ab.errors.add(:platinum, 'foo')
146
- @b.input(:platinum, :as=>:radio).to_s.must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input aria-describedby="album_platinum_no_error_message" aria-invalid="true" checked="checked" class="error" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label><span class="error_message" id="album_platinum_no_error_message">foo</span>'
199
+ @b.input(:platinum, :as=>:radio).must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input aria-describedby="album_platinum_no_error_message" aria-invalid="true" checked="checked" class="error" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label><span class="error_message" id="album_platinum_no_error_message">foo</span>'
147
200
  end
148
201
 
149
202
  it "should handle Raw :label options if :as=>:radio is used" do
150
- @b.input(:platinum, :as=>:radio, :label=>Forme.raw('Foo:<br />')).to_s.must_equal '<span class="label">Foo:<br /></span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
151
- @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />').to_s.must_equal '<span class="label">Foo:&lt;br /&gt;</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
203
+ @b.input(:platinum, :as=>:radio, :label=>Forme.raw('Foo:<br />')).must_equal '<span class="label">Foo:<br /></span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
204
+ @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />').must_equal '<span class="label">Foo:&lt;br /&gt;</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
152
205
  end
153
206
 
154
207
  it "should respect :true_label and :false_label options for boolean fields if :as=>:radio is used" do
155
- @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar").to_s.must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label>'
208
+ @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar").must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label>'
156
209
  end
157
210
 
158
211
  it "should respect :true_value and :false_value options for boolean fields if :as=>:radio is used" do
159
- @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar").to_s.must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label>'
212
+ @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar").must_equal '<span class="label">Platinum</span><label class="option"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label><label class="option"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label>'
160
213
  end
161
214
 
162
215
  it "should respect :formatter=>:readonly option for boolean fields if :as=>:radio is used" do
163
- @b.input(:platinum, :as=>:radio, :formatter=>:readonly).to_s.must_equal '<span class="label">Platinum</span><label class="option"><input disabled="disabled" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" disabled="disabled" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
216
+ @b.input(:platinum, :as=>:radio, :formatter=>:readonly).must_equal '<span class="label">Platinum</span><label class="option"><input disabled="disabled" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label><label class="option"><input checked="checked" disabled="disabled" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label>'
164
217
  end
165
218
 
166
219
  it "should use a select box for many_to_one associations" do
167
- @b.input(:artist).to_s.must_equal '<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>'
168
- @c.input(:artist).to_s.must_equal '<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>'
220
+ @b.input(:artist).must_equal '<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>'
221
+ @c.input(:artist).must_equal '<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>'
222
+ end
223
+
224
+ it "should respect given :key option for many_to_one associations" do
225
+ @b.input(:artist, :key=>'f').must_equal '<label>Artist: <select id="album_f" name="album[f]"><option value=""></option><option selected="selected" value="1">a</option><option value="2">d</option></select></label>'
226
+ end
227
+
228
+ it "should respect given :value option for many_to_one associations" do
229
+ @b.input(:artist, :value=>2).must_equal '<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>'
169
230
  end
170
231
 
171
232
  it "should not add a blank option by default if there is a default value and it is required" do
172
- @b.input(:artist, :required=>true).to_s.must_equal '<label>Artist<abbr title="required">*</abbr>: <select id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></label>'
233
+ @b.input(:artist, :required=>true).must_equal '<label>Artist<abbr title="required">*</abbr>: <select id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></label>'
173
234
  end
174
235
 
175
236
  it "should allow overriding default input type using a :type option" do
176
- @b.input(:artist, :type=>:string, :value=>nil).to_s.must_equal '<label>Artist: <input id="album_artist" name="album[artist]" type="text"/></label>'
237
+ @b.input(:artist, :type=>:string, :value=>nil).must_equal '<label>Artist: <input id="album_artist" name="album[artist]" type="text"/></label>'
177
238
  end
178
239
 
179
240
  it "should automatically set :required for many_to_one assocations based on whether the field is required" do
180
241
  begin
181
242
  Album.db_schema[:artist_id][:allow_null] = false
182
- @b.input(:artist).to_s.must_equal '<label>Artist<abbr title="required">*</abbr>: <select id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></label>'
183
- @b.input(:artist, :required=>false).to_s.must_equal '<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>'
243
+ @b.input(:artist).must_equal '<label>Artist<abbr title="required">*</abbr>: <select id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></label>'
244
+ @b.input(:artist, :required=>false).must_equal '<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>'
184
245
  ensure
185
246
  Album.db_schema[:artist_id][:allow_null] = true
186
247
  end
187
248
  end
188
249
 
189
250
  it "should use a required wrapper tag for many_to_one required associations" do
190
- @b.input(:artist, :required=>true, :wrapper=>:li).to_s.must_equal '<li class="many_to_one required"><label>Artist<abbr title="required">*</abbr>: <select id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></label></li>'
251
+ @b.input(:artist, :required=>true, :wrapper=>:li).must_equal '<li class="many_to_one required"><label>Artist<abbr title="required">*</abbr>: <select id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></label></li>'
191
252
  end
192
253
 
193
254
  it "should use a set of radio buttons for many_to_one associations with :as=>:radio option" do
194
- @b.input(:artist, :as=>:radio).to_s.must_equal '<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>'
195
- @c.input(:artist, :as=>:radio).to_s.must_equal '<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>'
255
+ @b.input(:artist, :as=>:radio).must_equal '<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>'
256
+ @c.input(:artist, :as=>:radio).must_equal '<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>'
196
257
  end
197
258
 
198
259
  it "should handle Raw label for many_to_one associations with :as=>:radio option" do
199
- @b.input(:artist, :as=>:radio, :label=>Forme.raw('Foo:<br />')).to_s.must_equal '<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>'
200
- @b.input(:artist, :as=>:radio, :label=>'Foo<br />').to_s.must_equal '<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>'
260
+ @b.input(:artist, :as=>:radio, :label=>Forme.raw('Foo:<br />')).must_equal '<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>'
261
+ @b.input(:artist, :as=>:radio, :label=>'Foo<br />').must_equal '<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>'
201
262
  end
202
263
 
203
264
  it "should correctly use the forms wrapper for wrapping radio buttons for many_to_one associations with :as=>:radio option" do
204
265
  @b = Forme::Form.new(@ab, :wrapper=>:li)
205
- @b.input(:artist, :as=>:radio).to_s.must_equal '<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>'
266
+ @b.input(:artist, :as=>:radio).must_equal '<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>'
206
267
  end
207
268
 
208
269
  it "should support custom wrappers for many_to_one associations with :as=>:radio via :tag_wrapper option" do
209
270
  @b = Forme::Form.new(@ab, :wrapper=>:li)
210
- @b.input(:artist, :as=>:radio, :wrapper=>proc{|t, i| i.tag(:div, {}, [t])}, :tag_wrapper=>proc{|t, i| i.tag(:span, {}, [t])}).to_s.must_equal '<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>'
271
+ @b.input(:artist, :as=>:radio, :wrapper=>proc{|t, i| i.tag(:div, {}, [t])}, :tag_wrapper=>proc{|t, i| i.tag(:span, {}, [t])}).must_equal '<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>'
211
272
  end
212
273
 
213
274
  it "should respect an :options entry" do
214
- @b.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">a</option><option value="3">d</option></select></label>'
215
- @c.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">a</option><option value="3">d</option></select></label>'
275
+ @b.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">a</option><option value="3">d</option></select></label>'
276
+ @c.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">a</option><option value="3">d</option></select></label>'
216
277
  end
217
278
 
218
279
  it "should support :name_method option for choosing name method" do
219
- @b.input(:artist, :name_method=>:idname).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">1a</option><option value="2">2d</option></select></label>'
220
- @c.input(:artist, :name_method=>:idname).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">1a</option><option selected="selected" value="2">2d</option></select></label>'
280
+ @b.input(:artist, :name_method=>:idname).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">1a</option><option value="2">2d</option></select></label>'
281
+ @c.input(:artist, :name_method=>:idname).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">1a</option><option selected="selected" value="2">2d</option></select></label>'
221
282
  end
222
283
 
223
284
  it "should support :name_method option being a callable object" do
224
- @b.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">1a1a</option><option value="2">2d2d</option></select></label>'
225
- @c.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">1a1a</option><option selected="selected" value="2">2d2d</option></select></label>'
285
+ @b.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">1a1a</option><option value="2">2d2d</option></select></label>'
286
+ @c.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">1a1a</option><option selected="selected" value="2">2d2d</option></select></label>'
226
287
  end
227
288
 
228
289
  it "should support :dataset option providing dataset to search" do
229
- @b.input(:artist, :dataset=>Artist.reverse_order(:name)).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">d</option><option selected="selected" value="1">a</option></select></label>'
230
- @c.input(:artist, :dataset=>Artist.reverse_order(:name)).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">d</option><option value="1">a</option></select></label>'
290
+ @b.input(:artist, :dataset=>Artist.reverse_order(:name)).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">d</option><option selected="selected" value="1">a</option></select></label>'
291
+ @c.input(:artist, :dataset=>Artist.reverse_order(:name)).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">d</option><option value="1">a</option></select></label>'
231
292
  end
232
293
 
233
294
  it "should support :dataset option being a callback proc returning modified dataset to search" do
234
- @b.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">d</option><option selected="selected" value="1">a</option></select></label>'
235
- @c.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">d</option><option value="1">a</option></select></label>'
295
+ @b.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">d</option><option selected="selected" value="1">a</option></select></label>'
296
+ @c.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">d</option><option value="1">a</option></select></label>'
236
297
  end
237
298
 
238
299
  it "should try a list of methods to get a suitable one for select box naming" do
@@ -243,16 +304,16 @@ describe "Forme Sequel::Model forms" do
243
304
  f = Forme::Form.new(al.new)
244
305
 
245
306
  ar.class_eval{def number() "#{self[:name]}1" end}
246
- f.input(:artist).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a1</option><option value="2">d1</option></select></label>'
307
+ f.input(:artist).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a1</option><option value="2">d1</option></select></label>'
247
308
 
248
309
  ar.class_eval{def title() "#{self[:name]}2" end}
249
- f.input(:artist).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a2</option><option value="2">d2</option></select></label>'
310
+ f.input(:artist).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a2</option><option value="2">d2</option></select></label>'
250
311
 
251
312
  ar.class_eval{def name() "#{self[:name]}3" end}
252
- f.input(:artist).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a3</option><option value="2">d3</option></select></label>'
313
+ f.input(:artist).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a3</option><option value="2">d3</option></select></label>'
253
314
 
254
315
  ar.class_eval{def forme_name() "#{self[:name]}4" end}
255
- f.input(:artist).to_s.must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a4</option><option value="2">d4</option></select></label>'
316
+ f.input(:artist).must_equal '<label>Artist: <select id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a4</option><option value="2">d4</option></select></label>'
256
317
  end
257
318
 
258
319
  it "should raise an error when using an association without a usable name method" do
@@ -264,95 +325,110 @@ describe "Forme Sequel::Model forms" do
264
325
  end
265
326
 
266
327
  it "should use a multiple select box for one_to_many associations" do
267
- @b.input(:tracks).to_s.must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option selected="selected" value="1">m</option><option selected="selected" value="2">n</option><option value="3">o</option></select></label>'
268
- @c.input(:tracks).to_s.must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option value="1">m</option><option value="2">n</option><option selected="selected" value="3">o</option></select></label>'
328
+ @b.input(:tracks).must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option selected="selected" value="1">m</option><option selected="selected" value="2">n</option><option value="3">o</option></select></label>'
329
+ @c.input(:tracks).must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option value="1">m</option><option value="2">n</option><option selected="selected" value="3">o</option></select></label>'
330
+ end
331
+
332
+ it "should :respect given :key option for one_to_many associations" do
333
+ @b.input(:tracks, :key=>:f).must_equal '<label>Tracks: <select id="album_f" multiple="multiple" name="album[f][]"><option selected="selected" value="1">m</option><option selected="selected" value="2">n</option><option value="3">o</option></select></label>'
334
+ end
335
+
336
+ it "should :respect given :array=>false, :multiple=>false options for one_to_many associations" do
337
+ @b.input(:tracks, :array=>false, :multiple=>false, :value=>1).must_equal '<label>Tracks: <select id="album_track_pks" name="album[track_pks]"><option selected="selected" value="1">m</option><option value="2">n</option><option value="3">o</option></select></label>'
338
+ end
339
+
340
+ it "should handle case where object doesn't respond to *_pks for one_to_many associations" do
341
+ class << @ab
342
+ undef track_pks
343
+ end
344
+ @b.input(:tracks).must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option selected="selected" value="1">m</option><option selected="selected" value="2">n</option><option value="3">o</option></select></label>'
269
345
  end
270
346
 
271
347
  it "should use a multiple select box for many_to_many associations" do
272
- @b.input(:tags).to_s.must_equal '<label>Tags: <select id="album_tag_pks" multiple="multiple" name="album[tag_pks][]"><option selected="selected" value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
273
- @c.input(:tags).to_s.must_equal '<label>Tags: <select id="album_tag_pks" multiple="multiple" name="album[tag_pks][]"><option value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
348
+ @b.input(:tags).must_equal '<label>Tags: <select id="album_tag_pks" multiple="multiple" name="album[tag_pks][]"><option selected="selected" value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
349
+ @c.input(:tags).must_equal '<label>Tags: <select id="album_tag_pks" multiple="multiple" name="album[tag_pks][]"><option value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
274
350
  end
275
351
 
276
352
  it "should use a multiple select box for pg_array_to_many associations" do
277
- @b.input(:atags).to_s.must_equal '<label>Atags: <select id="album_atag_ids" multiple="multiple" name="album[atag_ids][]"><option selected="selected" value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
353
+ @b.input(:atags).must_equal '<label>Atags: <select id="album_atag_ids" multiple="multiple" name="album[atag_ids][]"><option selected="selected" value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
278
354
  @c.obj.atag_ids.delete(1)
279
- @c.input(:atags).to_s.must_equal '<label>Atags: <select id="album_atag_ids" multiple="multiple" name="album[atag_ids][]"><option value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
355
+ @c.input(:atags).must_equal '<label>Atags: <select id="album_atag_ids" multiple="multiple" name="album[atag_ids][]"><option value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label>'
280
356
  end
281
357
 
282
358
  it "should handle an error message on the underlying column for pg_array_to_many associations" do
283
359
  @ab.errors.add(:atag_ids, 'tis not valid')
284
- @b.input(:atags).to_s.must_equal '<label>Atags: <select aria-describedby="album_atag_ids_error_message" aria-invalid="true" class="error" id="album_atag_ids" multiple="multiple" name="album[atag_ids][]"><option selected="selected" value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label><span class="error_message" id="album_atag_ids_error_message">tis not valid</span>'
285
- @b.input(:atags, :as=>:checkbox).to_s.must_equal '<span class="label">Atags</span><label class="option"><input checked="checked" id="album_atag_ids_1" name="album[atag_ids][]" type="checkbox" value="1"/> s</label><label class="option"><input checked="checked" id="album_atag_ids_2" name="album[atag_ids][]" type="checkbox" value="2"/> t</label><label class="option"><input aria-describedby="album_atag_ids_3_error_message" aria-invalid="true" class="error" id="album_atag_ids_3" name="album[atag_ids][]" type="checkbox" value="3"/> u</label><span class="error_message" id="album_atag_ids_3_error_message">tis not valid</span>'
360
+ @b.input(:atags).must_equal '<label>Atags: <select aria-describedby="album_atag_ids_error_message" aria-invalid="true" class="error" id="album_atag_ids" multiple="multiple" name="album[atag_ids][]"><option selected="selected" value="1">s</option><option selected="selected" value="2">t</option><option value="3">u</option></select></label><span class="error_message" id="album_atag_ids_error_message">tis not valid</span>'
361
+ @b.input(:atags, :as=>:checkbox).must_equal '<span class="label">Atags</span><label class="option"><input checked="checked" id="album_atag_ids_1" name="album[atag_ids][]" type="checkbox" value="1"/> s</label><label class="option"><input checked="checked" id="album_atag_ids_2" name="album[atag_ids][]" type="checkbox" value="2"/> t</label><label class="option"><input aria-describedby="album_atag_ids_3_error_message" aria-invalid="true" class="error" id="album_atag_ids_3" name="album[atag_ids][]" type="checkbox" value="3"/> u</label><span class="error_message" id="album_atag_ids_3_error_message">tis not valid</span>'
286
362
  end
287
363
 
288
364
  it "should use a regular select box for *_to_many associations if multiple if false" do
289
- @b.input(:tracks, :multiple=>false).to_s.must_equal '<label>Tracks: <select id="album_track_pks" name="album[track_pks][]"><option value="1">m</option><option value="2">n</option><option value="3">o</option></select></label>'
290
- @c.input(:tags, :multiple=>false).to_s.must_equal '<label>Tags: <select id="album_tag_pks" name="album[tag_pks][]"><option value="1">s</option><option value="2">t</option><option value="3">u</option></select></label>'
365
+ @b.input(:tracks, :multiple=>false).must_equal '<label>Tracks: <select id="album_track_pks" name="album[track_pks][]"><option value="1">m</option><option value="2">n</option><option value="3">o</option></select></label>'
366
+ @c.input(:tags, :multiple=>false).must_equal '<label>Tags: <select id="album_tag_pks" name="album[tag_pks][]"><option value="1">s</option><option value="2">t</option><option value="3">u</option></select></label>'
291
367
  end
292
368
 
293
369
  it "should handle a given :value for one_to_many associations" do
294
- @b.input(:tracks, :value=>[1,3]).to_s.must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option selected="selected" value="1">m</option><option value="2">n</option><option selected="selected" value="3">o</option></select></label>'
295
- @c.input(:tracks, :value=>nil).to_s.must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option value="1">m</option><option value="2">n</option><option value="3">o</option></select></label>'
370
+ @b.input(:tracks, :value=>[1,3]).must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option selected="selected" value="1">m</option><option value="2">n</option><option selected="selected" value="3">o</option></select></label>'
371
+ @c.input(:tracks, :value=>nil).must_equal '<label>Tracks: <select id="album_track_pks" multiple="multiple" name="album[track_pks][]"><option value="1">m</option><option value="2">n</option><option value="3">o</option></select></label>'
296
372
  end
297
373
 
298
374
  it "should use multiple checkboxes for one_to_many associations if :as=>:checkbox" do
299
- @b.input(:tracks, :as=>:checkbox).to_s.must_equal '<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>'
300
- @c.input(:tracks, :as=>:checkbox).to_s.must_equal '<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>'
375
+ @b.input(:tracks, :as=>:checkbox).must_equal '<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>'
376
+ @c.input(:tracks, :as=>:checkbox).must_equal '<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>'
301
377
  end
302
378
 
303
379
  it "should use multiple checkboxes for many_to_many associations if :as=>:checkbox" do
304
- @b.input(:tags, :as=>:checkbox).to_s.must_equal '<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>'
305
- @c.input(:tags, :as=>:checkbox).to_s.must_equal '<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>'
380
+ @b.input(:tags, :as=>:checkbox).must_equal '<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>'
381
+ @c.input(:tags, :as=>:checkbox).must_equal '<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>'
306
382
  end
307
383
 
308
384
  it "should handle Raw label for associations with :as=>:checkbox" do
309
- @b.input(:tracks, :as=>:checkbox, :label=>Forme.raw('Foo<br />:')).to_s.must_equal '<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>'
310
- @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />').to_s.must_equal '<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>'
385
+ @b.input(:tracks, :as=>:checkbox, :label=>Forme.raw('Foo<br />:')).must_equal '<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>'
386
+ @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />').must_equal '<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>'
311
387
  end
312
388
 
313
389
  it "should correctly use the forms wrapper for wrapping radio buttons for one_to_many associations with :as=>:checkbox option" do
314
390
  @b = Forme::Form.new(@ab, :wrapper=>:li)
315
- @b.input(:tracks, :as=>:checkbox).to_s.must_equal '<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>'
391
+ @b.input(:tracks, :as=>:checkbox).must_equal '<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>'
316
392
  end
317
393
 
318
394
  it "should support custom wrappers for one_to_many associations with :as=>:checkbox via :tag_wrapper option" do
319
395
  @b = Forme::Form.new(@ab, :wrapper=>:li)
320
- @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.must_equal '<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>'
396
+ @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])}).must_equal '<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>'
321
397
  end
322
398
 
323
399
  it "should use a text field methods not backed by columns" do
324
- @b.input(:artist_name).to_s.must_equal '<label>Artist name: <input id="album_artist_name" name="album[artist_name]" type="text" value="a"/></label>'
325
- @c.input(:artist_name).to_s.must_equal '<label>Artist name: <input id="album_artist_name" name="album[artist_name]" type="text" value="d"/></label>'
400
+ @b.input(:artist_name).must_equal '<label>Artist name: <input id="album_artist_name" name="album[artist_name]" type="text" value="a"/></label>'
401
+ @c.input(:artist_name).must_equal '<label>Artist name: <input id="album_artist_name" name="album[artist_name]" type="text" value="d"/></label>'
326
402
  end
327
403
 
328
404
  it "should handle errors on methods not backed by columns" do
329
405
  @ab.errors.add(:artist_name, 'foo')
330
- @b.input(:artist_name).to_s.must_equal '<label>Artist name: <input aria-describedby="album_artist_name_error_message" aria-invalid="true" class="error" id="album_artist_name" name="album[artist_name]" type="text" value="a"/></label><span class="error_message" id="album_artist_name_error_message">foo</span>'
406
+ @b.input(:artist_name).must_equal '<label>Artist name: <input aria-describedby="album_artist_name_error_message" aria-invalid="true" class="error" id="album_artist_name" name="album[artist_name]" type="text" value="a"/></label><span class="error_message" id="album_artist_name_error_message">foo</span>'
331
407
  end
332
408
 
333
409
  it "should respect a :type option with a schema type as the input type for methods not backed by columns" do
334
410
  def @ab.foo; false end
335
- @b.input(:foo, :type=>:boolean, :as=>:select).to_s.must_equal '<label>Foo: <select id="album_foo" name="album[foo]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
411
+ @b.input(:foo, :type=>:boolean, :as=>:select).must_equal '<label>Foo: <select id="album_foo" name="album[foo]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></label>'
336
412
  end
337
413
 
338
414
  it "should respect a :type option with an input type as the input type for methods not backed by columns" do
339
415
  def @ab.foo; "bar" end
340
- @b.input(:foo, :type=>:phone).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="phone" value="bar"/></label>'
416
+ @b.input(:foo, :type=>:phone).must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="phone" value="bar"/></label>'
341
417
  end
342
418
 
343
419
  it "should not override an explicit :error setting" do
344
420
  @ab.errors.add(:name, 'tis not valid')
345
- @b.input(:name, :error=>nil).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label>'
421
+ @b.input(:name, :error=>nil).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label>'
346
422
  end
347
423
 
348
424
  it "should correctly show an error message if there is one" do
349
425
  @ab.errors.add(:name, 'tis not valid')
350
- @b.input(:name).to_s.must_equal '<label>Name: <input aria-describedby="album_name_error_message" aria-invalid="true" class="error" id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label><span class="error_message" id="album_name_error_message">tis not valid</span>'
426
+ @b.input(:name).must_equal '<label>Name: <input aria-describedby="album_name_error_message" aria-invalid="true" class="error" id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label><span class="error_message" id="album_name_error_message">tis not valid</span>'
351
427
  end
352
428
 
353
429
  it "should correctly show an error message for many_to_one associations if there is one" do
354
430
  @ab.errors.add(:artist_id, 'tis not valid')
355
- @b.input(:artist).to_s.must_equal '<label>Artist: <select aria-describedby="album_artist_id_error_message" aria-invalid="true" class="error" 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><span class="error_message" id="album_artist_id_error_message">tis not valid</span>'
431
+ @b.input(:artist).must_equal '<label>Artist: <select aria-describedby="album_artist_id_error_message" aria-invalid="true" class="error" 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><span class="error_message" id="album_artist_id_error_message">tis not valid</span>'
356
432
  end
357
433
 
358
434
  it "should raise an error for unhandled associations" do
@@ -367,108 +443,112 @@ describe "Forme Sequel::Model forms" do
367
443
  end
368
444
 
369
445
  it "should respect a :type option with a schema type as the input type for unhandled fields" do
370
- @b.input(:foo, :type=>:string).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="text"/></label>'
371
- @b.input(:password, :type=>:string).to_s.must_equal '<label>Password: <input id="album_password" name="album[password]" type="password"/></label>'
446
+ @b.input(:foo, :type=>:string).must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="text"/></label>'
447
+ @b.input(:password, :type=>:string).must_equal '<label>Password: <input id="album_password" name="album[password]" type="password"/></label>'
372
448
  end
373
449
 
374
450
  it "should respect a :type option with an input type as the input type for unhandled fields" do
375
- @b.input(:foo, :type=>:phone).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="phone"/></label>'
451
+ @b.input(:foo, :type=>:phone).must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="phone"/></label>'
376
452
  end
377
453
 
378
454
  it "should respect a :multiple option for the name attribute for unhandled fields" do
379
- @b.input(:foo, :type=>:phone, :multiple=>true).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo][]" type="phone"/></label>'
455
+ @b.input(:foo, :type=>:phone, :multiple=>true).must_equal '<label>Foo: <input id="album_foo" name="album[foo][]" type="phone"/></label>'
380
456
  end
381
457
 
382
458
  it "should add required attribute if the column doesn't support nil values" do
383
459
  def @ab.db_schema; h = super.dup; h[:name] = h[:name].merge(:allow_null=>false); h end
384
- @b.input(:name).to_s.must_equal '<label>Name<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label>'
460
+ @b.input(:name).must_equal '<label>Name<abbr title="required">*</abbr>: <input id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></label>'
385
461
  end
386
462
 
387
463
  it "should use allow nested forms with many_to_one associations" do
388
- Forme.form(@ab){|f| f.subform(:artist){f.input(:name)}}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
464
+ Forme.form(@ab){|f| f.subform(:artist){f.input(:name)}}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
389
465
  end
390
466
 
391
467
  it "should have #subform respect an :inputs option" do
392
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name])}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
468
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name])}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
393
469
  end
394
470
 
395
471
  it "should have #subform respect an :obj option overriding the object to use" do
396
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :obj=>Artist.new(:name=>'b'))}.to_s.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="b"/></label></fieldset></form>'
472
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :obj=>Artist.new(:name=>'b'))}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="b"/></label></fieldset></form>'
397
473
  end
398
474
 
399
475
  it "should have #subform respect a :legend option if :inputs is used" do
400
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :legend=>'Foo')}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Foo</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
476
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :legend=>'Foo')}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Foo</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
401
477
  end
402
478
 
403
479
  it "should have #subform respect a callable :legend option if :inputs is used" do
404
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :legend=>proc{|o| "Foo - #{o.name}"})}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Foo - a</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
480
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :legend=>proc{|o| "Foo - #{o.name}"})}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Foo - a</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
405
481
  end
406
482
 
407
483
  it "should not add hidden primary key field for new many_to_one associated objects" do
408
484
  @ab.associations[:artist] = Artist.new(:name=>'a')
409
- Forme.form(@ab){|f| f.subform(:artist){f.input(:name)}}.to_s.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
485
+ Forme.form(@ab){|f| f.subform(:artist){f.input(:name)}}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Artist</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
410
486
  end
411
487
 
412
488
  it "should use allow nested forms with one_to_one associations" do
413
- Forme.form(@ab){|f| f.subform(:album_info, :obj=>AlbumInfo.new(:info=>'a')){f.input(:info)}}.to_s.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Album info</legend><label>Info: <input id="album_album_info_attributes_info" maxlength="255" name="album[album_info_attributes][info]" type="text" value="a"/></label></fieldset></form>'
489
+ Forme.form(@ab){|f| f.subform(:album_info, :obj=>AlbumInfo.new(:info=>'a')){f.input(:info)}}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Album info</legend><label>Info: <input id="album_album_info_attributes_info" maxlength="255" name="album[album_info_attributes][info]" type="text" value="a"/></label></fieldset></form>'
414
490
  end
415
491
 
416
492
  it "should use allow nested forms with one_to_many associations" do
417
- Forme.form(@ab){|f| f.subform(:tracks){f.input(:name)}}.to_s.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_0_id" name="album[tracks_attributes][0][id]" type="hidden" value="1"/><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
493
+ Forme.form(@ab){|f| f.subform(:tracks){f.input(:name)}}.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_0_id" name="album[tracks_attributes][0][id]" type="hidden" value="1"/><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
418
494
  end
419
495
 
420
496
  it "should support :obj option for *_to_many associations" do
421
- Forme.form(@ab){|f| f.subform(:tracks, :obj=>[Track.new(:name=>'x'), Track.load(:id=>5, :name=>'y')]){f.input(:name)}}.to_s.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="5"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="x"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="y"/></label></fieldset></form>'
497
+ Forme.form(@ab){|f| f.subform(:tracks, :obj=>[Track.new(:name=>'x'), Track.load(:id=>5, :name=>'y')]){f.input(:name)}}.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="5"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="x"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="y"/></label></fieldset></form>'
422
498
  end
423
499
 
424
500
  it "should auto number legends when using subform with inputs for *_to_many associations" do
425
- Forme.form(@ab){|f| f.subform(:tracks, :inputs=>[:name])}.to_s.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_0_id" name="album[tracks_attributes][0][id]" type="hidden" value="1"/><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
501
+ Forme.form(@ab){|f| f.subform(:tracks, :inputs=>[:name])}.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_0_id" name="album[tracks_attributes][0][id]" type="hidden" value="1"/><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
426
502
  end
427
503
 
428
504
  it "should support callable :legend option when using subform with inputs for *_to_many associations" do
429
- Forme.form(@ab){|f| f.subform(:tracks, :inputs=>[:name], :legend=>proc{|o, i| "Track #{i} (#{o.name})"})}.to_s.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_0_id" name="album[tracks_attributes][0][id]" type="hidden" value="1"/><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track 0 (m)</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track 1 (n)</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
505
+ Forme.form(@ab){|f| f.subform(:tracks, :inputs=>[:name], :legend=>proc{|o, i| "Track #{i} (#{o.name})"})}.must_equal '<form class="forme album" method="post"><input id="album_tracks_attributes_0_id" name="album[tracks_attributes][0][id]" type="hidden" value="1"/><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track 0 (m)</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track 1 (n)</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
430
506
  end
431
507
 
432
508
  it "should not add hidden primary key field for nested forms with one_to_many associations with new objects" do
433
509
  @ab.associations[:tracks] = [Track.new(:name=>'m'), Track.new(:name=>'n')]
434
- Forme.form(@ab){|f| f.subform(:tracks){f.input(:name)}}.to_s.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
510
+ Forme.form(@ab){|f| f.subform(:tracks){f.input(:name)}}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
435
511
  end
436
512
 
437
513
  it "should use allow nested forms with many_to_many associations" do
438
- Forme.form(@ab){|f| f.subform(:tags){f.input(:name)}}.to_s.must_equal '<form class="forme album" method="post"><input id="album_tags_attributes_0_id" name="album[tags_attributes][0][id]" type="hidden" value="1"/><input id="album_tags_attributes_1_id" name="album[tags_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Tag #1</legend><label>Name: <input id="album_tags_attributes_0_name" maxlength="255" name="album[tags_attributes][0][name]" type="text" value="s"/></label></fieldset><fieldset class="inputs"><legend>Tag #2</legend><label>Name: <input id="album_tags_attributes_1_name" maxlength="255" name="album[tags_attributes][1][name]" type="text" value="t"/></label></fieldset></form>'
514
+ Forme.form(@ab){|f| f.subform(:tags){f.input(:name)}}.must_equal '<form class="forme album" method="post"><input id="album_tags_attributes_0_id" name="album[tags_attributes][0][id]" type="hidden" value="1"/><input id="album_tags_attributes_1_id" name="album[tags_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Tag #1</legend><label>Name: <input id="album_tags_attributes_0_name" maxlength="255" name="album[tags_attributes][0][name]" type="text" value="s"/></label></fieldset><fieldset class="inputs"><legend>Tag #2</legend><label>Name: <input id="album_tags_attributes_1_name" maxlength="255" name="album[tags_attributes][1][name]" type="text" value="t"/></label></fieldset></form>'
439
515
  end
440
516
 
441
517
  it "should not add hidden primary key field for nested forms with many_to_many associations with new objects" do
442
518
  @ab.associations[:tags] = [Tag.new(:name=>'s'), Tag.new(:name=>'t')]
443
- Forme.form(@ab){|f| f.subform(:tags){f.input(:name)}}.to_s.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Tag #1</legend><label>Name: <input id="album_tags_attributes_0_name" maxlength="255" name="album[tags_attributes][0][name]" type="text" value="s"/></label></fieldset><fieldset class="inputs"><legend>Tag #2</legend><label>Name: <input id="album_tags_attributes_1_name" maxlength="255" name="album[tags_attributes][1][name]" type="text" value="t"/></label></fieldset></form>'
519
+ Forme.form(@ab){|f| f.subform(:tags){f.input(:name)}}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Tag #1</legend><label>Name: <input id="album_tags_attributes_0_name" maxlength="255" name="album[tags_attributes][0][name]" type="text" value="s"/></label></fieldset><fieldset class="inputs"><legend>Tag #2</legend><label>Name: <input id="album_tags_attributes_1_name" maxlength="255" name="album[tags_attributes][1][name]" type="text" value="t"/></label></fieldset></form>'
444
520
  end
445
521
 
446
522
  it "should handle multiple nested levels" do
447
- Forme.form(Artist[1]){|f| f.subform(:albums){f.input(:name); f.subform(:tracks){f.input(:name)}}}.to_s.to_s.must_equal '<form class="forme artist" method="post"><input id="artist_albums_attributes_0_id" name="artist[albums_attributes][0][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Album #1</legend><label>Name: <input id="artist_albums_attributes_0_name" maxlength="255" name="artist[albums_attributes][0][name]" type="text" value="b"/></label><input id="artist_albums_attributes_0_tracks_attributes_0_id" name="artist[albums_attributes][0][tracks_attributes][0][id]" type="hidden" value="1"/><input id="artist_albums_attributes_0_tracks_attributes_1_id" name="artist[albums_attributes][0][tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="artist_albums_attributes_0_tracks_attributes_0_name" maxlength="255" name="artist[albums_attributes][0][tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="artist_albums_attributes_0_tracks_attributes_1_name" maxlength="255" name="artist[albums_attributes][0][tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></fieldset></form>'
523
+ Forme.form(Artist[1]){|f| f.subform(:albums){f.input(:name); f.subform(:tracks){f.input(:name)}}}.to_s.must_equal '<form class="forme artist" method="post"><input id="artist_albums_attributes_0_id" name="artist[albums_attributes][0][id]" type="hidden" value="1"/><fieldset class="inputs"><legend>Album #1</legend><label>Name: <input id="artist_albums_attributes_0_name" maxlength="255" name="artist[albums_attributes][0][name]" type="text" value="b"/></label><input id="artist_albums_attributes_0_tracks_attributes_0_id" name="artist[albums_attributes][0][tracks_attributes][0][id]" type="hidden" value="1"/><input id="artist_albums_attributes_0_tracks_attributes_1_id" name="artist[albums_attributes][0][tracks_attributes][1][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Track #1</legend><label>Name: <input id="artist_albums_attributes_0_tracks_attributes_0_name" maxlength="255" name="artist[albums_attributes][0][tracks_attributes][0][name]" type="text" value="m"/></label></fieldset><fieldset class="inputs"><legend>Track #2</legend><label>Name: <input id="artist_albums_attributes_0_tracks_attributes_1_name" maxlength="255" name="artist[albums_attributes][0][tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></fieldset></form>'
448
524
  end
449
525
 
450
526
  it "should have #subform :grid option create a grid" do
451
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true)}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
527
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true)}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
452
528
  end
453
529
 
454
530
  it "should have #subform :grid option respect :inputs_opts option to pass options to inputs" do
455
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :inputs_opts=>{:attr=>{:class=>'foo'}})}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table class="foo"><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
531
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :inputs_opts=>{:attr=>{:class=>'foo'}})}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table class="foo"><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
456
532
  end
457
533
 
458
534
  it "should have #subform :grid option handle :legend and :labels options" do
459
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :legend=>'Foo', :labels=>%w'Bar')}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table><caption>Foo</caption><thead><tr><th>Bar</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
535
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :legend=>'Foo', :labels=>%w'Bar')}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table><caption>Foo</caption><thead><tr><th>Bar</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
460
536
  end
461
537
 
462
538
  it "should have #subform :grid option handle :legend and :labels nil values" do
463
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :legend=>nil, :labels=>nil)}.to_s.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
539
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :legend=>nil, :labels=>nil)}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
464
540
  end
465
541
 
466
542
  it "should have #subform :grid option handle :skip_primary_key option" do
467
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :skip_primary_key=>true)}.to_s.must_equal '<form class="forme album" method="post"><table><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
543
+ Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :skip_primary_key=>true)}.must_equal '<form class="forme album" method="post"><table><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
544
+ end
545
+
546
+ it "should have #subform :grid option handle no :inputs or :labels" do
547
+ Forme.form(@ab){|f| f.subform(:artist, :grid=>true){f.input(:name)}}.must_equal '<form class="forme album" method="post"><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><table><caption>Artist</caption><tbody><tr><td class="string"><input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
468
548
  end
469
549
 
470
550
  it "should handle non-Sequel forms with Sequel inputs" do
471
- Forme.form{|f| f.input(:name, :obj=>@ab).to_s.must_equal '<label>Name: <input id="name" maxlength="255" name="name" type="text" value="b"/></label>'}
551
+ Forme.form{|f| f.input(:name, :obj=>@ab).must_equal '<label>Name: <input id="name" maxlength="255" name="name" type="text" value="b"/></label>'}
472
552
  end
473
553
  end
474
554
 
@@ -479,22 +559,22 @@ describe "Forme Sequel plugin default input types based on column names" do
479
559
  end
480
560
 
481
561
  it "should use password input with no value for string columns with name password" do
482
- f(:password).to_s.must_equal '<label>Password: <input id="test_password" maxlength="255" name="test[password]" type="password"/></label>'
562
+ f(:password).must_equal '<label>Password: <input id="test_password" maxlength="255" name="test[password]" type="password"/></label>'
483
563
  end
484
564
 
485
565
  it "should use email input for string columns with name email" do
486
- f(:email).to_s.must_equal '<label>Email: <input id="test_email" maxlength="255" name="test[email]" type="email" value="foo"/></label>'
566
+ f(:email).must_equal '<label>Email: <input id="test_email" maxlength="255" name="test[email]" type="email" value="foo"/></label>'
487
567
  end
488
568
 
489
569
  it "should use tel input for string columns with name phone or fax" do
490
- f(:phone).to_s.must_equal '<label>Phone: <input id="test_phone" maxlength="255" name="test[phone]" type="tel" value="foo"/></label>'
491
- f(:fax).to_s.must_equal '<label>Fax: <input id="test_fax" maxlength="255" name="test[fax]" type="tel" value="foo"/></label>'
570
+ f(:phone).must_equal '<label>Phone: <input id="test_phone" maxlength="255" name="test[phone]" type="tel" value="foo"/></label>'
571
+ f(:fax).must_equal '<label>Fax: <input id="test_fax" maxlength="255" name="test[fax]" type="tel" value="foo"/></label>'
492
572
  end
493
573
 
494
574
  it "should use url input for string columns with name url, uri, or website" do
495
- f(:url).to_s.must_equal '<label>Url: <input id="test_url" maxlength="255" name="test[url]" type="url" value="foo"/></label>'
496
- f(:uri).to_s.must_equal '<label>Uri: <input id="test_uri" maxlength="255" name="test[uri]" type="url" value="foo"/></label>'
497
- f(:website).to_s.must_equal '<label>Website: <input id="test_website" maxlength="255" name="test[website]" type="url" value="foo"/></label>'
575
+ f(:url).must_equal '<label>Url: <input id="test_url" maxlength="255" name="test[url]" type="url" value="foo"/></label>'
576
+ f(:uri).must_equal '<label>Uri: <input id="test_uri" maxlength="255" name="test[uri]" type="url" value="foo"/></label>'
577
+ f(:website).must_equal '<label>Website: <input id="test_website" maxlength="255" name="test[website]" type="url" value="foo"/></label>'
498
578
  end
499
579
  end
500
580
 
@@ -505,7 +585,7 @@ describe "Forme Sequel plugin default input types based on column type" do
505
585
  end
506
586
 
507
587
  it "should use a file input for blob types" do
508
- f(File).to_s.must_equal '<label>Foo: <input id="test_foo" name="test[foo]" type="file"/></label>'
588
+ f(File).must_equal '<label>Foo: <input id="test_foo" name="test[foo]" type="file"/></label>'
509
589
  end
510
590
  end
511
591
 
@@ -518,36 +598,59 @@ describe "Forme Sequel::Model validation parsing" do
518
598
  end
519
599
 
520
600
  it "should turn format into a pattern" do
521
- f(:validates_format_of, :name, :with=>/[A-z]+/).input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="[A-z]+" type="text"/></label>'
601
+ f(:validates_format_of, :name, :with=>/[A-z]+/).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="[A-z]+" type="text"/></label>'
602
+ end
603
+
604
+ it "should not override given :pattern for format validation" do
605
+ f(:validates_format_of, :name, :with=>/[A-z]+/).input(:name, :attr=>{:pattern=>'[A-Z]+'}).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="[A-Z]+" type="text"/></label>'
522
606
  end
523
607
 
524
608
  it "should respect :title option for format" do
525
- f(:validates_format_of, :name, :with=>/[A-z]+/, :title=>'Foo').input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="[A-z]+" title="Foo" type="text"/></label>'
609
+ f(:validates_format_of, :name, :with=>/[A-z]+/, :title=>'Foo').input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="[A-z]+" title="Foo" type="text"/></label>'
610
+ end
611
+
612
+ it "should not override given :title for format validation" do
613
+ f(:validates_format_of, :name, :with=>/[A-z]+/, :title=>'Foo').input(:name, :attr=>{:title=>'Bar'}).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="[A-z]+" title="Bar" type="text"/></label>'
526
614
  end
527
615
 
528
616
  it "should use maxlength for length :maximum, :is, and :within" do
529
- f(:validates_length_of, :name, :maximum=>10).input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
530
- f(:validates_length_of, :name, :is=>10).input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
531
- f(:validates_length_of, :name, :within=>2..10).input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
532
- f(:validates_length_of, :name, :within=>2...11).input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
617
+ f(:validates_length_of, :name, :maximum=>10).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
618
+ f(:validates_length_of, :name, :is=>10).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
619
+ f(:validates_length_of, :name, :within=>2..10).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
620
+ f(:validates_length_of, :name, :within=>2...11).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="10" name="album[name]" type="text"/></label>'
621
+ end
622
+
623
+ it "should not override given :maxlength" do
624
+ f(:validates_length_of, :name, :maximum=>10).input(:name, :attr=>{:maxlength=>20}).must_equal '<label>Name: <input id="album_name" maxlength="20" name="album[name]" type="text"/></label>'
625
+ end
626
+
627
+ it "should not use maxlength for :within that is not a range" do
628
+ f(:validates_length_of, :name, :within=>(2..10).to_a).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text"/></label>'
533
629
  end
534
630
 
535
631
  it "should turn numericality into a pattern" do
536
- f(:validates_numericality_of, :name).input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="must be a number" type="text"/></label>'
632
+ f(:validates_numericality_of, :name).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="must be a number" type="text"/></label>'
537
633
  end
538
634
 
539
635
  it "should turn numericality :only_integer into a pattern" do
540
- f(:validates_numericality_of, :name, :only_integer=>true).input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+$" title="must be a number" type="text"/></label>'
636
+ f(:validates_numericality_of, :name, :only_integer=>true).input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+$" title="must be a number" type="text"/></label>'
637
+ end
638
+
639
+ it "should not override given :pattern for numericality validation" do
640
+ f(:validates_numericality_of, :name).input(:name, :attr=>{:pattern=>'[0-9]+'}).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="[0-9]+" title="must be a number" type="text"/></label>'
541
641
  end
542
642
 
543
643
  it "should respect :title option for numericality" do
544
- f(:validates_numericality_of, :name, :title=>'Foo').input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="Foo" type="text"/></label>'
644
+ f(:validates_numericality_of, :name, :title=>'Foo').input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="Foo" type="text"/></label>'
545
645
  end
546
646
 
547
- it "should respect :placeholder option for any validation" do
548
- f(:validates_uniqueness_of, :name, :placeholder=>'must be unique').input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" placeholder="must be unique" type="text"/></label>'
647
+ it "should not override given :title for numericality validation" do
648
+ f(:validates_numericality_of, :name, :title=>'Foo').input(:name, :attr=>{:title=>'Bar'}).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="Bar" type="text"/></label>'
549
649
  end
550
650
 
651
+ it "should respect :placeholder option for any validation" do
652
+ f(:validates_uniqueness_of, :name, :placeholder=>'must be unique').input(:name).must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" placeholder="must be unique" type="text"/></label>'
653
+ end
551
654
  end
552
655
 
553
656
  describe "Forme Sequel::Model default namespacing" do
@@ -565,11 +668,11 @@ describe "Forme Sequel::Model default namespacing" do
565
668
  end
566
669
 
567
670
  it "namespaces the form class" do
568
- @b.form.to_s.must_equal '<form class="forme foo/album" method="post"></form>'
671
+ @b.form.must_equal '<form class="forme foo/album" method="post"></form>'
569
672
  end
570
673
 
571
674
  it "namespaces the input id and name" do
572
- @b.input(:name).to_s.must_equal '<label>Name: <input id="foo/album_name" maxlength="255" name="foo/album[name]" type="text" value="b"/></label>'
675
+ @b.input(:name).must_equal '<label>Name: <input id="foo/album_name" maxlength="255" name="foo/album[name]" type="text" value="b"/></label>'
573
676
  end
574
677
  end
575
678
 
@@ -591,10 +694,10 @@ describe "Forme Sequel::Model custom namespacing" do
591
694
  end
592
695
 
593
696
  it "namespaces the form class" do
594
- @b.form.to_s.must_equal '<form class="forme bar_album" method="post"></form>'
697
+ @b.form.must_equal '<form class="forme bar_album" method="post"></form>'
595
698
  end
596
699
 
597
700
  it "namespaces the form input and name" do
598
- @b.input(:name).to_s.must_equal '<label>Name: <input id="bar_album_name" maxlength="255" name="bar_album[name]" type="text" value="b"/></label>'
701
+ @b.input(:name).must_equal '<label>Name: <input id="bar_album_name" maxlength="255" name="bar_album[name]" type="text" value="b"/></label>'
599
702
  end
600
703
  end