forme 1.12.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +54 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +228 -206
  5. data/Rakefile +1 -7
  6. data/lib/forme/bs3.rb +23 -9
  7. data/lib/forme/erb.rb +13 -25
  8. data/lib/forme/form.rb +146 -149
  9. data/lib/forme/input.rb +1 -1
  10. data/lib/forme/rails.rb +39 -83
  11. data/lib/forme/raw.rb +2 -2
  12. data/lib/forme/tag.rb +3 -12
  13. data/lib/forme/template.rb +110 -0
  14. data/lib/forme/transformers/error_handler.rb +10 -10
  15. data/lib/forme/transformers/formatter.rb +32 -34
  16. data/lib/forme/transformers/helper.rb +0 -1
  17. data/lib/forme/transformers/inputs_wrapper.rb +4 -4
  18. data/lib/forme/version.rb +2 -2
  19. data/lib/forme.rb +13 -2
  20. data/lib/roda/plugins/forme.rb +1 -1
  21. data/lib/roda/plugins/forme_erubi_capture.rb +57 -0
  22. data/lib/roda/plugins/forme_route_csrf.rb +16 -34
  23. data/lib/roda/plugins/forme_set.rb +39 -76
  24. data/lib/sequel/plugins/forme.rb +45 -54
  25. data/lib/sequel/plugins/forme_i18n.rb +3 -1
  26. data/lib/sequel/plugins/forme_set.rb +2 -4
  27. data/spec/all.rb +1 -1
  28. data/spec/bs3_reference_spec.rb +291 -314
  29. data/spec/bs3_sequel_plugin_spec.rb +155 -155
  30. data/spec/bs3_spec.rb +247 -206
  31. data/spec/erb_helper.rb +69 -58
  32. data/spec/erubi_capture_helper.rb +198 -0
  33. data/spec/forme_coverage.rb +1 -0
  34. data/spec/forme_spec.rb +438 -377
  35. data/spec/rails_integration_spec.rb +21 -11
  36. data/spec/roda_integration_spec.rb +136 -70
  37. data/spec/sequel_helper.rb +3 -2
  38. data/spec/sequel_i18n_helper.rb +1 -1
  39. data/spec/sequel_i18n_plugin_spec.rb +6 -6
  40. data/spec/sequel_plugin_spec.rb +262 -150
  41. data/spec/sequel_set_plugin_spec.rb +9 -3
  42. data/spec/shared_erb_specs.rb +71 -0
  43. data/spec/sinatra_integration_spec.rb +31 -6
  44. data/spec/spec_helper.rb +21 -8
  45. metadata +8 -6
  46. data/lib/forme/erb_form.rb +0 -74
  47. data/lib/forme/sinatra.rb +0 -17
@@ -1,5 +1,5 @@
1
- require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb')
2
- require File.join(File.dirname(File.expand_path(__FILE__)), 'sequel_helper.rb')
1
+ require_relative 'spec_helper'
2
+ require_relative 'sequel_helper'
3
3
 
4
4
  describe "Forme Sequel::Model forms" do
5
5
  before do
@@ -9,221 +9,291 @@ describe "Forme Sequel::Model forms" do
9
9
  @c = Forme::Form.new(@ac)
10
10
  end
11
11
 
12
+ it "should handle Forme::Form subclasses" do
13
+ c = Class.new(Forme::Form) do
14
+ def form(*)
15
+ super{|f| f.tag(:input, :type=>:hidden, :name=>'a', :value=>'b')}
16
+ end
17
+ end
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'
34
+ end
35
+
12
36
  it "should add appropriate attributes by default" do
13
- @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>'
14
38
  end
15
39
 
16
40
  it "should allow overriding of attributes" do
17
- @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>'
18
42
  end
19
43
 
20
44
  it "should handle invalid methods" do
21
45
  def @ab.db_schema
22
46
  super.merge(:foo=>{:type=>:bar})
23
47
  end
24
- @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>'
25
49
  end
26
50
 
27
51
  it "should allow an array of classes" do
28
- @b.form(:class=>[:foo, :bar]).to_s.must_equal '<form class="foo bar forme album" method="post"></form>'
29
- @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>'
30
54
  end
31
55
 
32
56
  it "should use a text field for strings" do
33
- @b.input(:name).to_s.must_equal '<label>Name: <input id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></label>'
34
- @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>'
35
59
  end
36
60
 
37
61
  it "should allow :as=>:textarea to use a textarea" do
38
- @b.input(:name, :as=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">b</textarea></label>'
39
- @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>'
40
64
  end
41
65
 
42
66
  it "should allow :type=>:textarea to use a textarea" do
43
- @b.input(:name, :type=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" maxlength="255" name="album[name]">b</textarea></label>'
44
- @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>'
45
73
  end
46
74
 
47
75
  it "should not include labels for hidden inputs" do
48
- @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"/>'
49
77
  end
50
78
 
51
- it "should use number inputs for integers" do
52
- @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>'
53
89
  end
54
90
 
55
91
  it "should use date inputs for Dates" do
56
- @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>'
57
93
  end
58
94
 
59
95
  it "should use datetime inputs for Time" do
60
- @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>}
61
97
  end
62
98
 
63
99
  it "should use datetime inputs for DateTimes" do
64
100
  @ab.values[:created_at] = DateTime.new(2011, 6, 5)
65
- @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>'
66
102
  end
67
103
 
68
104
  it "should use file inputs without value" do
69
- @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>'
70
106
  end
71
107
 
72
108
  it "should include type as wrapper class" do
73
109
  @ab.values[:created_at] = DateTime.new(2011, 6, 5)
74
110
  f = Forme::Form.new(@ab, :wrapper=>:li)
75
- 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>'
76
- 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>'
77
- 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>'
78
122
  end
79
123
 
80
124
  it "should include required * in label if required" do
81
- @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>'
82
126
  end
83
127
 
84
128
  it "should add required to label even if :label option specified" do
85
- @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>'
86
130
  end
87
131
 
88
132
  it "should not add required to label even if :label option is nil" do
89
- @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"/>'
90
134
  end
91
135
 
92
136
  it "should include required wrapper class if required" do
93
137
  f = Forme::Form.new(@ab, :wrapper=>:li)
94
- 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>'
95
139
  end
96
140
 
97
141
  it "should use a select box for tri-valued boolean fields" do
98
- @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>'
99
- @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>'
100
150
  end
101
151
 
102
152
  it "should respect :true_label and :false_label options for tri-valued boolean fields" do
103
- @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>'
104
154
  end
105
155
 
106
156
  it "should respect :true_value and :false_value options for tri-valued boolean fields" do
107
- @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>'
108
158
  end
109
159
 
110
160
  it "should respect :add_blank option for tri-valued boolean fields" do
111
- @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>'
112
162
  end
113
163
 
114
164
  it "should use a select box for dual-valued boolean fields where :required => false" do
115
- @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>'
116
- @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>'
117
167
  end
118
168
 
119
169
  it "should use a checkbox for dual-valued boolean fields" do
120
- @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>'
121
- @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>'
122
178
  end
123
179
 
124
180
  it "should use radio buttons for boolean fields if :as=>:radio is used" do
125
- @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>'
126
- @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>'
127
189
  end
128
190
 
129
191
  it "should wrap both inputs if :as=>:radio is used" do
130
192
  @b = Forme::Form.new(@ab, :wrapper=>:li)
131
- @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>'
132
- @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>'
133
195
  end
134
196
 
135
197
  it "should handle errors on radio buttons for boolean fields if :as=>:radio is used" do
136
198
  @ab.errors.add(:platinum, 'foo')
137
- @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>'
138
200
  end
139
201
 
140
202
  it "should handle Raw :label options if :as=>:radio is used" do
141
- @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>'
142
- @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>'
143
205
  end
144
206
 
145
207
  it "should respect :true_label and :false_label options for boolean fields if :as=>:radio is used" do
146
- @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>'
147
209
  end
148
210
 
149
211
  it "should respect :true_value and :false_value options for boolean fields if :as=>:radio is used" do
150
- @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>'
151
213
  end
152
214
 
153
215
  it "should respect :formatter=>:readonly option for boolean fields if :as=>:radio is used" do
154
- @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>'
155
217
  end
156
218
 
157
219
  it "should use a select box for many_to_one associations" do
158
- @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>'
159
- @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>'
160
230
  end
161
231
 
162
232
  it "should not add a blank option by default if there is a default value and it is required" do
163
- @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>'
164
234
  end
165
235
 
166
236
  it "should allow overriding default input type using a :type option" do
167
- @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>'
168
238
  end
169
239
 
170
240
  it "should automatically set :required for many_to_one assocations based on whether the field is required" do
171
241
  begin
172
242
  Album.db_schema[:artist_id][:allow_null] = false
173
- @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>'
174
- @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>'
175
245
  ensure
176
246
  Album.db_schema[:artist_id][:allow_null] = true
177
247
  end
178
248
  end
179
249
 
180
250
  it "should use a required wrapper tag for many_to_one required associations" do
181
- @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>'
182
252
  end
183
253
 
184
254
  it "should use a set of radio buttons for many_to_one associations with :as=>:radio option" do
185
- @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>'
186
- @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>'
187
257
  end
188
258
 
189
259
  it "should handle Raw label for many_to_one associations with :as=>:radio option" do
190
- @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>'
191
- @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>'
192
262
  end
193
263
 
194
264
  it "should correctly use the forms wrapper for wrapping radio buttons for many_to_one associations with :as=>:radio option" do
195
265
  @b = Forme::Form.new(@ab, :wrapper=>:li)
196
- @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>'
197
267
  end
198
268
 
199
269
  it "should support custom wrappers for many_to_one associations with :as=>:radio via :tag_wrapper option" do
200
270
  @b = Forme::Form.new(@ab, :wrapper=>:li)
201
- @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>'
202
272
  end
203
273
 
204
274
  it "should respect an :options entry" do
205
- @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>'
206
- @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>'
207
277
  end
208
278
 
209
279
  it "should support :name_method option for choosing name method" do
210
- @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>'
211
- @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>'
212
282
  end
213
283
 
214
284
  it "should support :name_method option being a callable object" do
215
- @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>'
216
- @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>'
217
287
  end
218
288
 
219
289
  it "should support :dataset option providing dataset to search" do
220
- @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>'
221
- @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>'
222
292
  end
223
293
 
224
294
  it "should support :dataset option being a callback proc returning modified dataset to search" do
225
- @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>'
226
- @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>'
227
297
  end
228
298
 
229
299
  it "should try a list of methods to get a suitable one for select box naming" do
@@ -234,16 +304,16 @@ describe "Forme Sequel::Model forms" do
234
304
  f = Forme::Form.new(al.new)
235
305
 
236
306
  ar.class_eval{def number() "#{self[:name]}1" end}
237
- 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>'
238
308
 
239
309
  ar.class_eval{def title() "#{self[:name]}2" end}
240
- 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>'
241
311
 
242
312
  ar.class_eval{def name() "#{self[:name]}3" end}
243
- 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>'
244
314
 
245
315
  ar.class_eval{def forme_name() "#{self[:name]}4" 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">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>'
247
317
  end
248
318
 
249
319
  it "should raise an error when using an association without a usable name method" do
@@ -255,95 +325,110 @@ describe "Forme Sequel::Model forms" do
255
325
  end
256
326
 
257
327
  it "should use a multiple select box for one_to_many associations" do
258
- @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>'
259
- @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>'
260
345
  end
261
346
 
262
347
  it "should use a multiple select box for many_to_many associations" do
263
- @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>'
264
- @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>'
265
350
  end
266
351
 
267
352
  it "should use a multiple select box for pg_array_to_many associations" do
268
- @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>'
269
354
  @c.obj.atag_ids.delete(1)
270
- @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>'
271
356
  end
272
357
 
273
358
  it "should handle an error message on the underlying column for pg_array_to_many associations" do
274
359
  @ab.errors.add(:atag_ids, 'tis not valid')
275
- @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>'
276
- @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>'
277
362
  end
278
363
 
279
364
  it "should use a regular select box for *_to_many associations if multiple if false" do
280
- @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>'
281
- @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>'
282
367
  end
283
368
 
284
369
  it "should handle a given :value for one_to_many associations" do
285
- @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>'
286
- @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>'
287
372
  end
288
373
 
289
374
  it "should use multiple checkboxes for one_to_many associations if :as=>:checkbox" do
290
- @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>'
291
- @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>'
292
377
  end
293
378
 
294
379
  it "should use multiple checkboxes for many_to_many associations if :as=>:checkbox" do
295
- @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>'
296
- @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>'
297
382
  end
298
383
 
299
384
  it "should handle Raw label for associations with :as=>:checkbox" do
300
- @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>'
301
- @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>'
302
387
  end
303
388
 
304
389
  it "should correctly use the forms wrapper for wrapping radio buttons for one_to_many associations with :as=>:checkbox option" do
305
390
  @b = Forme::Form.new(@ab, :wrapper=>:li)
306
- @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>'
307
392
  end
308
393
 
309
394
  it "should support custom wrappers for one_to_many associations with :as=>:checkbox via :tag_wrapper option" do
310
395
  @b = Forme::Form.new(@ab, :wrapper=>:li)
311
- @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>'
312
397
  end
313
398
 
314
399
  it "should use a text field methods not backed by columns" do
315
- @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>'
316
- @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>'
317
402
  end
318
403
 
319
404
  it "should handle errors on methods not backed by columns" do
320
405
  @ab.errors.add(:artist_name, 'foo')
321
- @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>'
322
407
  end
323
408
 
324
409
  it "should respect a :type option with a schema type as the input type for methods not backed by columns" do
325
410
  def @ab.foo; false end
326
- @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>'
327
412
  end
328
413
 
329
414
  it "should respect a :type option with an input type as the input type for methods not backed by columns" do
330
415
  def @ab.foo; "bar" end
331
- @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>'
332
417
  end
333
418
 
334
419
  it "should not override an explicit :error setting" do
335
420
  @ab.errors.add(:name, 'tis not valid')
336
- @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>'
337
422
  end
338
423
 
339
424
  it "should correctly show an error message if there is one" do
340
425
  @ab.errors.add(:name, 'tis not valid')
341
- @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>'
342
427
  end
343
428
 
344
429
  it "should correctly show an error message for many_to_one associations if there is one" do
345
430
  @ab.errors.add(:artist_id, 'tis not valid')
346
- @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>'
347
432
  end
348
433
 
349
434
  it "should raise an error for unhandled associations" do
@@ -358,108 +443,112 @@ describe "Forme Sequel::Model forms" do
358
443
  end
359
444
 
360
445
  it "should respect a :type option with a schema type as the input type for unhandled fields" do
361
- @b.input(:foo, :type=>:string).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="text"/></label>'
362
- @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>'
363
448
  end
364
449
 
365
450
  it "should respect a :type option with an input type as the input type for unhandled fields" do
366
- @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>'
367
452
  end
368
453
 
369
454
  it "should respect a :multiple option for the name attribute for unhandled fields" do
370
- @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>'
371
456
  end
372
457
 
373
458
  it "should add required attribute if the column doesn't support nil values" do
374
459
  def @ab.db_schema; h = super.dup; h[:name] = h[:name].merge(:allow_null=>false); h end
375
- @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>'
376
461
  end
377
462
 
378
463
  it "should use allow nested forms with many_to_one associations" do
379
- 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>'
380
465
  end
381
466
 
382
467
  it "should have #subform respect an :inputs option" do
383
- 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>'
384
469
  end
385
470
 
386
471
  it "should have #subform respect an :obj option overriding the object to use" do
387
- 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>'
388
473
  end
389
474
 
390
475
  it "should have #subform respect a :legend option if :inputs is used" do
391
- 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>'
392
477
  end
393
478
 
394
479
  it "should have #subform respect a callable :legend option if :inputs is used" do
395
- 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>'
396
481
  end
397
482
 
398
483
  it "should not add hidden primary key field for new many_to_one associated objects" do
399
484
  @ab.associations[:artist] = Artist.new(:name=>'a')
400
- 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>'
401
486
  end
402
487
 
403
488
  it "should use allow nested forms with one_to_one associations" do
404
- 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>'
405
490
  end
406
491
 
407
492
  it "should use allow nested forms with one_to_many associations" do
408
- 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"/><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><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><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>'
409
494
  end
410
495
 
411
496
  it "should support :obj option for *_to_many associations" do
412
- 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"><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><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="5"/><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>'
413
498
  end
414
499
 
415
500
  it "should auto number legends when using subform with inputs for *_to_many associations" do
416
- 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"/><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><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><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>'
417
502
  end
418
503
 
419
504
  it "should support callable :legend option when using subform with inputs for *_to_many associations" do
420
- 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"/><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><input id="album_tracks_attributes_1_id" name="album[tracks_attributes][1][id]" type="hidden" value="2"/><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>'
421
506
  end
422
507
 
423
508
  it "should not add hidden primary key field for nested forms with one_to_many associations with new objects" do
424
509
  @ab.associations[:tracks] = [Track.new(:name=>'m'), Track.new(:name=>'n')]
425
- 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>'
426
511
  end
427
512
 
428
513
  it "should use allow nested forms with many_to_many associations" do
429
- 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"/><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><input id="album_tags_attributes_1_id" name="album[tags_attributes][1][id]" type="hidden" value="2"/><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>'
430
515
  end
431
516
 
432
517
  it "should not add hidden primary key field for nested forms with many_to_many associations with new objects" do
433
518
  @ab.associations[:tags] = [Tag.new(:name=>'s'), Tag.new(:name=>'t')]
434
- 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>'
435
520
  end
436
521
 
437
522
  it "should handle multiple nested levels" do
438
- 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"/><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><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 #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>'
439
524
  end
440
525
 
441
526
  it "should have #subform :grid option create a grid" do
442
- Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true)}.to_s.must_equal '<form class="forme album" method="post"><table><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><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>'
443
528
  end
444
529
 
445
530
  it "should have #subform :grid option respect :inputs_opts option to pass options to inputs" do
446
- 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"><table class="foo"><caption>Artist</caption><thead><tr><th>Name</th></tr></thead><tbody><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><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>'
447
532
  end
448
533
 
449
534
  it "should have #subform :grid option handle :legend and :labels options" do
450
- 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"><table><caption>Foo</caption><thead><tr><th>Bar</th></tr></thead><tbody><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><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>'
451
536
  end
452
537
 
453
538
  it "should have #subform :grid option handle :legend and :labels nil values" do
454
- 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"><table><tbody><input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="1"/><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>'
455
540
  end
456
541
 
457
542
  it "should have #subform :grid option handle :skip_primary_key option" do
458
- 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>'
459
548
  end
460
549
 
461
550
  it "should handle non-Sequel forms with Sequel inputs" do
462
- 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>'}
463
552
  end
464
553
  end
465
554
 
@@ -470,22 +559,22 @@ describe "Forme Sequel plugin default input types based on column names" do
470
559
  end
471
560
 
472
561
  it "should use password input with no value for string columns with name password" do
473
- 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>'
474
563
  end
475
564
 
476
565
  it "should use email input for string columns with name email" do
477
- 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>'
478
567
  end
479
568
 
480
569
  it "should use tel input for string columns with name phone or fax" do
481
- f(:phone).to_s.must_equal '<label>Phone: <input id="test_phone" maxlength="255" name="test[phone]" type="tel" value="foo"/></label>'
482
- 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>'
483
572
  end
484
573
 
485
574
  it "should use url input for string columns with name url, uri, or website" do
486
- f(:url).to_s.must_equal '<label>Url: <input id="test_url" maxlength="255" name="test[url]" type="url" value="foo"/></label>'
487
- f(:uri).to_s.must_equal '<label>Uri: <input id="test_uri" maxlength="255" name="test[uri]" type="url" value="foo"/></label>'
488
- 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>'
489
578
  end
490
579
  end
491
580
 
@@ -496,7 +585,7 @@ describe "Forme Sequel plugin default input types based on column type" do
496
585
  end
497
586
 
498
587
  it "should use a file input for blob types" do
499
- 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>'
500
589
  end
501
590
  end
502
591
 
@@ -509,36 +598,59 @@ describe "Forme Sequel::Model validation parsing" do
509
598
  end
510
599
 
511
600
  it "should turn format into a pattern" do
512
- 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>'
513
606
  end
514
607
 
515
608
  it "should respect :title option for format" do
516
- 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>'
517
614
  end
518
615
 
519
616
  it "should use maxlength for length :maximum, :is, and :within" do
520
- 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>'
521
- 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>'
522
- 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>'
523
- 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>'
524
629
  end
525
630
 
526
631
  it "should turn numericality into a pattern" do
527
- 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>'
528
633
  end
529
634
 
530
635
  it "should turn numericality :only_integer into a pattern" do
531
- 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>'
532
641
  end
533
642
 
534
643
  it "should respect :title option for numericality" do
535
- 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>'
536
645
  end
537
646
 
538
- it "should respect :placeholder option for any validation" do
539
- 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>'
540
649
  end
541
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
542
654
  end
543
655
 
544
656
  describe "Forme Sequel::Model default namespacing" do
@@ -556,11 +668,11 @@ describe "Forme Sequel::Model default namespacing" do
556
668
  end
557
669
 
558
670
  it "namespaces the form class" do
559
- @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>'
560
672
  end
561
673
 
562
674
  it "namespaces the input id and name" do
563
- @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>'
564
676
  end
565
677
  end
566
678
 
@@ -582,10 +694,10 @@ describe "Forme Sequel::Model custom namespacing" do
582
694
  end
583
695
 
584
696
  it "namespaces the form class" do
585
- @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>'
586
698
  end
587
699
 
588
700
  it "namespaces the form input and name" do
589
- @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>'
590
702
  end
591
703
  end