forme 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +16 -0
- data/MIT-LICENSE +1 -1
- data/README.rdoc +9 -1
- data/Rakefile +10 -39
- data/lib/forme.rb +8 -4
- data/lib/forme/bs3.rb +387 -0
- data/lib/forme/erb.rb +4 -2
- data/lib/forme/form.rb +2 -0
- data/lib/forme/input.rb +2 -0
- data/lib/forme/rails.rb +2 -0
- data/lib/forme/raw.rb +2 -0
- data/lib/forme/sinatra.rb +2 -0
- data/lib/forme/tag.rb +2 -0
- data/lib/forme/transformers/error_handler.rb +2 -0
- data/lib/forme/transformers/formatter.rb +37 -13
- data/lib/forme/transformers/helper.rb +3 -1
- data/lib/forme/transformers/inputs_wrapper.rb +2 -0
- data/lib/forme/transformers/labeler.rb +2 -0
- data/lib/forme/transformers/serializer.rb +2 -0
- data/lib/forme/transformers/wrapper.rb +3 -1
- data/lib/forme/version.rb +3 -1
- data/lib/roda/plugins/forme.rb +2 -0
- data/lib/sequel/plugins/forme.rb +6 -2
- data/spec/bs3_reference_spec.rb +358 -0
- data/spec/bs3_sequel_plugin_spec.rb +523 -0
- data/spec/bs3_spec.rb +690 -0
- data/spec/erb_helper.rb +27 -25
- data/spec/forme_spec.rb +542 -526
- data/spec/rails_integration_spec.rb +26 -26
- data/spec/roda_integration_spec.rb +14 -5
- data/spec/sequel_plugin_spec.rb +247 -233
- data/spec/sinatra_integration_spec.rb +12 -3
- data/spec/spec_helper.rb +1 -14
- metadata +120 -4
@@ -193,56 +193,56 @@ describe "Forme Rails integration" do
|
|
193
193
|
@rack = {'rack.input'=>'', 'REQUEST_METHOD'=>'GET', 'rack.errors'=>o}
|
194
194
|
end
|
195
195
|
|
196
|
-
|
197
|
-
sin_get('/index').
|
196
|
+
it "#form should add start and end tags and yield Forme::Form instance" do
|
197
|
+
sin_get('/index').must_equal '<form action="/baz"> <p>FBB</p> <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> <input type="submit" value="Save"/> </form>'
|
198
198
|
end
|
199
199
|
|
200
|
-
|
201
|
-
sin_get('/inputs_block').
|
200
|
+
it "#form should have inputs work with a block" do
|
201
|
+
sin_get('/inputs_block').must_equal '<form action="/baz"> <fieldset class="inputs"><legend>FBB</legend> <input id="last" name="last" type="text" value="bar"/> </fieldset></form>'
|
202
202
|
end
|
203
203
|
|
204
|
-
|
205
|
-
sin_get('/inputs_block_wrapper').
|
204
|
+
it "#form should have inputs with fieldset_ol wrapper work with block" do
|
205
|
+
sin_get('/inputs_block_wrapper').must_equal '<form action="/baz"> <fieldset class="inputs"><legend>FBB</legend><ol> <input id="last" name="last" type="text" value="bar"/> </ol></fieldset></form>'
|
206
206
|
end
|
207
207
|
|
208
|
-
|
209
|
-
sin_get('/nest').
|
208
|
+
it "#form should add start and end tags and yield Forme::Form instance" do
|
209
|
+
sin_get('/nest').must_equal '<form action="/baz"> <p>FBB</p> <div> <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> </div> </form>'
|
210
210
|
end
|
211
211
|
|
212
|
-
|
213
|
-
sin_get('/nest_sep').
|
212
|
+
it "#form should correctly handle situation where multiple templates are used with same form object" do
|
213
|
+
sin_get('/nest_sep').must_equal "0 <form action=\"/baz\"> 1 <p>FBB</p> 2 n1 <div> n2 <input id=\"first\" name=\"first\" type=\"text\" value=\"foo\"/> <input id=\"last\" name=\"last\" type=\"text\" value=\"bar\"/> n3 </div> n4 <fieldset class=\"inputs\"><legend>Foo</legend><input id=\"first\" name=\"first\" type=\"text\" value=\"foo\"/><input id=\"last\" name=\"last\" type=\"text\" value=\"bar\"/></fieldset> n5 3 </form>4"
|
214
214
|
end
|
215
215
|
|
216
|
-
|
217
|
-
sin_get('/nest_inputs').
|
216
|
+
it "#form should correctly handle situation where multiple templates are used with same form object" do
|
217
|
+
sin_get('/nest_inputs').must_equal "0 <form action=\"/baz\"> 1 <p>FBB</p> 2 n1 <fieldset class=\"inputs\"> n2 <input id=\"first\" name=\"first\" type=\"text\" value=\"foo\"/> <input id=\"last\" name=\"last\" type=\"text\" value=\"bar\"/> n3 </fieldset> n4 <fieldset class=\"inputs\"><legend>Foo</legend><input id=\"first\" name=\"first\" type=\"text\" value=\"foo\"/><input id=\"last\" name=\"last\" type=\"text\" value=\"bar\"/></fieldset> n5 3 </form>4"
|
218
218
|
end
|
219
219
|
|
220
|
-
|
221
|
-
sin_get('/nest_seq').sub(%r{<input name=\"authenticity_token\" type=\"hidden\" value=\"([^\"]+)\"/>}, "<input name=\"authenticity_token\" type=\"hidden\" value=\"csrf\"/>").
|
220
|
+
it "#form should correctly handle situation Sequel integration with subforms where multiple templates are used with same form object" do
|
221
|
+
sin_get('/nest_seq').sub(%r{<input name=\"authenticity_token\" type=\"hidden\" value=\"([^\"]+)\"/>}, "<input name=\"authenticity_token\" type=\"hidden\" value=\"csrf\"/>").must_equal "0 <form action=\"/baz\" class=\"forme album\" method=\"post\"><input name=\"authenticity_token\" type=\"hidden\" value=\"csrf\"/> 1 <input id=\"album_artist_attributes_id\" name=\"album[artist_attributes][id]\" type=\"hidden\" value=\"2\"/><fieldset class=\"inputs\"><legend>Foo</legend><label>Name: <input id=\"album_artist_attributes_name\" name=\"album[artist_attributes][name]\" type=\"text\" value=\"A\"/></label></fieldset> 2 n1 <input id=\"album_artist_attributes_id\" name=\"album[artist_attributes][id]\" type=\"hidden\" value=\"2\"/><fieldset class=\"inputs\"><legend>Artist</legend> n2 <label>Name2: <input id=\"album_artist_attributes_name2\" name=\"album[artist_attributes][name2]\" type=\"text\" value=\"A2\"/></label> n3 </fieldset> n4 <input id=\"album_artist_attributes_id\" name=\"album[artist_attributes][id]\" type=\"hidden\" value=\"2\"/><fieldset class=\"inputs\"><legend>Bar</legend><label>Name3: <input id=\"album_artist_attributes_name3\" name=\"album[artist_attributes][name3]\" type=\"text\" value=\"A3\"/></label></fieldset> n5 3 </form>4"
|
222
222
|
end
|
223
223
|
|
224
|
-
|
225
|
-
sin_get('/hash').
|
224
|
+
it "#form should accept two hashes instead of requiring obj as first argument" do
|
225
|
+
sin_get('/hash').must_equal '<form action="/baz"> <input id="first" name="first" type="text" value="foo"/> </form>'
|
226
226
|
end
|
227
227
|
|
228
|
-
|
229
|
-
sin_get('/legend').
|
228
|
+
it "#form should deal with emitted code" do
|
229
|
+
sin_get('/legend').must_equal '<form action="/baz"> <p>FBB</p> <fieldset class="inputs"><legend>Foo</legend><input id="first" name="first" type="text" value="foo"/><input id="last" name="last" type="text" value="bar"/></fieldset> <p>FBB2</p> </form>'
|
230
230
|
end
|
231
231
|
|
232
|
-
|
233
|
-
sin_get('/combined').
|
232
|
+
it "#form should work with :inputs, :button, and :legend options" do
|
233
|
+
sin_get('/combined').must_equal '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset> <p>FBB</p> <input id="last" name="last" type="text" value="bar"/> <input type="submit" value="xyz"/></form>'
|
234
234
|
end
|
235
235
|
|
236
|
-
|
237
|
-
sin_get('/noblock').
|
236
|
+
it "#form should work without a block" do
|
237
|
+
sin_get('/noblock').must_equal '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
|
238
238
|
end
|
239
239
|
|
240
|
-
|
241
|
-
sin_get('/noblock_post').sub(%r{<input name=\"authenticity_token\" type=\"hidden\" value=\"([^\"]+)\"/>}, "<input name=\"authenticity_token\" type=\"hidden\" value=\"csrf\"/>").
|
240
|
+
it "#form should work without a block with hidden tags" do
|
241
|
+
sin_get('/noblock_post').sub(%r{<input name=\"authenticity_token\" type=\"hidden\" value=\"([^\"]+)\"/>}, "<input name=\"authenticity_token\" type=\"hidden\" value=\"csrf\"/>").must_equal '<form method="post"><input name="authenticity_token" type="hidden" value="csrf"/><input type="submit" value="xyz"/></form>'
|
242
242
|
end
|
243
243
|
|
244
|
-
|
245
|
-
sin_get('/safe_buffer').
|
244
|
+
it "#form should handle Rails SafeBuffers" do
|
245
|
+
sin_get('/safe_buffer').must_equal '<form action="/baz"><fieldset class="inputs"><legend><b>foo</b></legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
|
246
246
|
end
|
247
247
|
end
|
248
248
|
end
|
@@ -4,12 +4,21 @@ require File.join(File.dirname(File.expand_path(__FILE__)), 'erb_helper.rb')
|
|
4
4
|
|
5
5
|
require 'rubygems'
|
6
6
|
begin
|
7
|
-
require 'roda'
|
8
|
-
require
|
9
|
-
require 'rack/csrf'
|
7
|
+
require 'roda'
|
8
|
+
require 'rack/csrf'
|
10
9
|
rescue LoadError
|
11
10
|
warn "unable to load roda or rack/csrf, skipping roda spec"
|
12
11
|
else
|
12
|
+
begin
|
13
|
+
require 'tilt/erubis'
|
14
|
+
rescue LoadError
|
15
|
+
require 'tilt/erb'
|
16
|
+
begin
|
17
|
+
require 'erubis'
|
18
|
+
rescue LoadError
|
19
|
+
require 'erb'
|
20
|
+
end
|
21
|
+
end
|
13
22
|
class FormeRodaTest < Roda
|
14
23
|
plugin :forme
|
15
24
|
use Rack::Session::Cookie, :secret => "__a_very_long_string__"
|
@@ -26,11 +35,11 @@ end
|
|
26
35
|
|
27
36
|
describe "Forme Roda ERB integration" do
|
28
37
|
def sin_get(path)
|
29
|
-
s =
|
38
|
+
s = String.new
|
30
39
|
FormeRodaTest.app.call(@rack.merge('PATH_INFO'=>path))[2].each{|str| s << str}
|
31
40
|
s.gsub(/\s+/, ' ').strip
|
32
41
|
end
|
33
42
|
|
34
|
-
|
43
|
+
include FormeErbSpecs
|
35
44
|
end
|
36
45
|
end
|
data/spec/sequel_plugin_spec.rb
CHANGED
@@ -9,198 +9,206 @@ describe "Forme Sequel::Model forms" do
|
|
9
9
|
@c = Forme::Form.new(@ac)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
@b.form.to_s.
|
12
|
+
it "should add appropriate attributes by default" do
|
13
|
+
@b.form.to_s.must_equal '<form class="forme album" method="post"></form>'
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
@b.form(:class=>:foo, :method=>:get).to_s.
|
16
|
+
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>'
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
it "should handle invalid methods" do
|
21
21
|
def @ab.db_schema
|
22
22
|
super.merge(:foo=>{:type=>:bar})
|
23
23
|
end
|
24
|
-
@b.input(:foo, :value=>'baz').to_s.
|
24
|
+
@b.input(:foo, :value=>'baz').to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="text" value="baz"/></label>'
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
@b.form(:class=>[:foo, :bar]).to_s.
|
29
|
-
@b.form(:class=>[:foo, [:bar, :baz]]).to_s.
|
27
|
+
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>'
|
30
30
|
end
|
31
31
|
|
32
|
-
|
33
|
-
@b.input(:name).to_s.
|
34
|
-
@c.input(:name).to_s.
|
32
|
+
it "should use a text field for strings" do
|
33
|
+
@b.input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" type="text" value="b"/></label>'
|
34
|
+
@c.input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" type="text" value="c"/></label>'
|
35
35
|
end
|
36
36
|
|
37
|
-
|
38
|
-
@b.input(:name, :as=>:textarea).to_s.
|
39
|
-
@c.input(:name, :as=>:textarea).to_s.
|
37
|
+
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" name="album[name]">b</textarea></label>'
|
39
|
+
@c.input(:name, :as=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" name="album[name]">c</textarea></label>'
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
@b.input(:name, :type=>:textarea).to_s.
|
44
|
-
@c.input(:name, :type=>:textarea).to_s.
|
42
|
+
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" name="album[name]">b</textarea></label>'
|
44
|
+
@c.input(:name, :type=>:textarea).to_s.must_equal '<label>Name: <textarea id="album_name" name="album[name]">c</textarea></label>'
|
45
45
|
end
|
46
46
|
|
47
|
-
|
48
|
-
@b.input(:copies_sold).to_s.
|
47
|
+
it "should use number inputs for integers" do
|
48
|
+
@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>'
|
49
49
|
end
|
50
50
|
|
51
|
-
|
52
|
-
@b.input(:release_date).to_s.
|
51
|
+
it "should use date inputs for Dates" do
|
52
|
+
@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>'
|
53
53
|
end
|
54
54
|
|
55
|
-
|
56
|
-
@b.input(:created_at).to_s.
|
55
|
+
it "should use datetime inputs for Time" do
|
56
|
+
@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.000000"/></label>}
|
57
57
|
end
|
58
58
|
|
59
|
-
|
59
|
+
it "should use datetime inputs for DateTimes" do
|
60
60
|
@ab.values[:created_at] = DateTime.new(2011, 6, 5)
|
61
|
-
@b.input(:created_at).to_s.
|
61
|
+
@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.000000"/></label>'
|
62
62
|
end
|
63
63
|
|
64
|
-
|
64
|
+
it "should include type as wrapper class" do
|
65
65
|
@ab.values[:created_at] = DateTime.new(2011, 6, 5)
|
66
66
|
f = Forme::Form.new(@ab, :wrapper=>:li)
|
67
|
-
f.input(:name).to_s.
|
68
|
-
f.input(:release_date).to_s.
|
69
|
-
f.input(:created_at).to_s.
|
67
|
+
f.input(:name).to_s.must_equal '<li class="string"><label>Name: <input id="album_name" name="album[name]" type="text" value="b"/></label></li>'
|
68
|
+
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>'
|
69
|
+
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.000000"/></label></li>'
|
70
70
|
end
|
71
71
|
|
72
|
-
|
73
|
-
@b.input(:name, :required=>true).to_s.
|
72
|
+
it "should include required * in label if required" do
|
73
|
+
@b.input(:name, :required=>true).to_s.must_equal '<label>Name<abbr title="required">*</abbr>: <input id="album_name" name="album[name]" required="required" type="text" value="b"/></label>'
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
|
-
@b.input(:name, :required=>true, :label=>'Foo').to_s.
|
76
|
+
it "should add required to label even if :label option specified" do
|
77
|
+
@b.input(:name, :required=>true, :label=>'Foo').to_s.must_equal '<label>Foo<abbr title="required">*</abbr>: <input id="album_name" name="album[name]" required="required" type="text" value="b"/></label>'
|
78
78
|
end
|
79
79
|
|
80
|
-
|
80
|
+
it "should not add required to label even if :label option is nil" do
|
81
|
+
@b.input(:name, :required=>true, :label=>nil).to_s.must_equal '<input id="album_name" name="album[name]" required="required" type="text" value="b"/>'
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should include required wrapper class if required" do
|
81
85
|
f = Forme::Form.new(@ab, :wrapper=>:li)
|
82
|
-
f.input(:name, :required=>true).to_s.
|
86
|
+
f.input(:name, :required=>true).to_s.must_equal '<li class="string required"><label>Name<abbr title="required">*</abbr>: <input id="album_name" name="album[name]" required="required" type="text" value="b"/></label></li>'
|
83
87
|
end
|
84
88
|
|
85
|
-
|
86
|
-
@b.input(:gold).to_s.
|
87
|
-
@c.input(:gold).to_s.
|
89
|
+
it "should use a select box for tri-valued boolean fields" do
|
90
|
+
@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>'
|
91
|
+
@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>'
|
88
92
|
end
|
89
93
|
|
90
|
-
|
91
|
-
@b.input(:gold, :true_label=>"Foo", :false_label=>"Bar").to_s.
|
94
|
+
it "should respect :true_label and :false_label options for tri-valued boolean fields" do
|
95
|
+
@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>'
|
92
96
|
end
|
93
97
|
|
94
|
-
|
95
|
-
@b.input(:gold, :true_value=>"Foo", :false_value=>"Bar").to_s.
|
98
|
+
it "should respect :true_value and :false_value options for tri-valued boolean fields" do
|
99
|
+
@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>'
|
96
100
|
end
|
97
101
|
|
98
|
-
|
99
|
-
@b.input(:gold, :add_blank=>'NULL').to_s.
|
102
|
+
it "should respect :add_blank option for tri-valued boolean fields" do
|
103
|
+
@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>'
|
100
104
|
end
|
101
105
|
|
102
|
-
|
103
|
-
@b.input(:platinum, :required=>false).to_s.
|
104
|
-
@c.input(:platinum, :required=>false).to_s.
|
106
|
+
it "should use a select box for dual-valued boolean fields where :required => false" do
|
107
|
+
@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>'
|
108
|
+
@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>'
|
105
109
|
end
|
106
110
|
|
107
|
-
|
108
|
-
@b.input(:platinum).to_s.
|
109
|
-
@c.input(:platinum).to_s.
|
111
|
+
it "should use a checkbox for dual-valued boolean fields" do
|
112
|
+
@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>'
|
113
|
+
@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>'
|
110
114
|
end
|
111
115
|
|
112
|
-
|
113
|
-
@b.input(:platinum, :as=>:radio).to_s.
|
114
|
-
@c.input(:platinum, :as=>:radio).to_s.
|
116
|
+
it "should use radio buttons for boolean fields if :as=>:radio is used" do
|
117
|
+
@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>'
|
118
|
+
@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>'
|
115
119
|
end
|
116
120
|
|
117
|
-
|
121
|
+
it "should wrap both inputs if :as=>:radio is used" do
|
118
122
|
@b = Forme::Form.new(@ab, :wrapper=>:li)
|
119
|
-
@b.input(:platinum, :as=>:radio).to_s.
|
120
|
-
@b.input(:platinum, :as=>:radio, :wrapper=>:div, :tag_wrapper=>:span).to_s.
|
123
|
+
@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>'
|
124
|
+
@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>'
|
121
125
|
end
|
122
126
|
|
123
|
-
|
127
|
+
it "should handle errors on radio buttons for boolean fields if :as=>:radio is used" do
|
124
128
|
@ab.errors.add(:platinum, 'foo')
|
125
|
-
@b.input(:platinum, :as=>:radio).to_s.
|
129
|
+
@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" class="error" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label><span class="error_message">foo</span>'
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should handle Raw :label options if :as=>:radio is used" do
|
133
|
+
@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>'
|
134
|
+
@b.input(:platinum, :as=>:radio, :label=>'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>'
|
126
135
|
end
|
127
136
|
|
128
|
-
|
129
|
-
@b.input(:platinum, :as=>:radio, :
|
130
|
-
@b.input(:platinum, :as=>:radio, :label=>'Foo:<br />').to_s.should == '<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>'
|
137
|
+
it "should respect :true_label and :false_label options for boolean fields if :as=>:radio is used" do
|
138
|
+
@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>'
|
131
139
|
end
|
132
140
|
|
133
|
-
|
134
|
-
@b.input(:platinum, :as=>:radio, :
|
141
|
+
it "should respect :true_value and :false_value options for boolean fields if :as=>:radio is used" do
|
142
|
+
@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>'
|
135
143
|
end
|
136
144
|
|
137
|
-
|
138
|
-
@b.input(:platinum, :as=>:radio, :
|
145
|
+
it "should respect :formatter=>:readonly option for boolean fields if :as=>:radio is used" do
|
146
|
+
@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>'
|
139
147
|
end
|
140
148
|
|
141
|
-
|
142
|
-
@b.input(:artist).to_s.
|
143
|
-
@c.input(:artist).to_s.
|
149
|
+
it "should use a select box for many_to_one associations" do
|
150
|
+
@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>'
|
151
|
+
@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>'
|
144
152
|
end
|
145
153
|
|
146
|
-
|
147
|
-
@b.input(:artist, :required=>true).to_s.
|
154
|
+
it "should not add a blank option by default if there is a default value and it is required" do
|
155
|
+
@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>'
|
148
156
|
end
|
149
157
|
|
150
|
-
|
151
|
-
@b.input(:artist, :type=>:string, :value=>nil).to_s.
|
158
|
+
it "should allow overriding default input type using a :type option" do
|
159
|
+
@b.input(:artist, :type=>:string, :value=>nil).to_s.must_equal '<label>Artist: <input id="album_artist" name="album[artist]" type="text"/></label>'
|
152
160
|
end
|
153
161
|
|
154
|
-
|
155
|
-
@b.input(:artist, :required=>true, :wrapper=>:li).to_s.
|
162
|
+
it "should use a required wrapper tag for many_to_one required associations" do
|
163
|
+
@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>'
|
156
164
|
end
|
157
165
|
|
158
|
-
|
159
|
-
@b.input(:artist, :as=>:radio).to_s.
|
160
|
-
@c.input(:artist, :as=>:radio).to_s.
|
166
|
+
it "should use a set of radio buttons for many_to_one associations with :as=>:radio option" do
|
167
|
+
@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>'
|
168
|
+
@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>'
|
161
169
|
end
|
162
170
|
|
163
|
-
|
164
|
-
@b.input(:artist, :as=>:radio, :label=>'Foo:<br />'
|
165
|
-
@b.input(:artist, :as=>:radio, :label=>'Foo<br />').to_s.
|
171
|
+
it "should handle Raw label for many_to_one associations with :as=>:radio option" do
|
172
|
+
@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>'
|
173
|
+
@b.input(:artist, :as=>:radio, :label=>'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>'
|
166
174
|
end
|
167
175
|
|
168
|
-
|
176
|
+
it "should correctly use the forms wrapper for wrapping radio buttons for many_to_one associations with :as=>:radio option" do
|
169
177
|
@b = Forme::Form.new(@ab, :wrapper=>:li)
|
170
|
-
@b.input(:artist, :as=>:radio).to_s.
|
178
|
+
@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>'
|
171
179
|
end
|
172
180
|
|
173
|
-
|
181
|
+
it "should support custom wrappers for many_to_one associations with :as=>:radio via :tag_wrapper option" do
|
174
182
|
@b = Forme::Form.new(@ab, :wrapper=>:li)
|
175
|
-
@b.input(:artist, :as=>:radio, :wrapper=>proc{|t, i| i.tag(:div, {}, [t])}, :tag_wrapper=>proc{|t, i| i.tag(:span, {}, [t])}).to_s.
|
183
|
+
@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>'
|
176
184
|
end
|
177
185
|
|
178
|
-
|
179
|
-
@b.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).to_s.
|
180
|
-
@c.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).to_s.
|
186
|
+
it "should respect an :options entry" do
|
187
|
+
@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>'
|
188
|
+
@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>'
|
181
189
|
end
|
182
190
|
|
183
|
-
|
184
|
-
@b.input(:artist, :name_method=>:idname).to_s.
|
185
|
-
@c.input(:artist, :name_method=>:idname).to_s.
|
191
|
+
it "should support :name_method option for choosing name method" do
|
192
|
+
@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>'
|
193
|
+
@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>'
|
186
194
|
end
|
187
195
|
|
188
|
-
|
189
|
-
@b.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).to_s.
|
190
|
-
@c.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).to_s.
|
196
|
+
it "should support :name_method option being a callable object" do
|
197
|
+
@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>'
|
198
|
+
@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>'
|
191
199
|
end
|
192
200
|
|
193
|
-
|
194
|
-
@b.input(:artist, :dataset=>Artist.reverse_order(:name)).to_s.
|
195
|
-
@c.input(:artist, :dataset=>Artist.reverse_order(:name)).to_s.
|
201
|
+
it "should support :dataset option providing dataset to search" do
|
202
|
+
@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>'
|
203
|
+
@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>'
|
196
204
|
end
|
197
205
|
|
198
|
-
|
199
|
-
@b.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).to_s.
|
200
|
-
@c.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).to_s.
|
206
|
+
it "should support :dataset option being a callback proc returning modified dataset to search" do
|
207
|
+
@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>'
|
208
|
+
@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>'
|
201
209
|
end
|
202
210
|
|
203
|
-
|
211
|
+
it "should try a list of methods to get a suitable one for select box naming" do
|
204
212
|
al = Class.new(Album){def self.name() 'Album' end}
|
205
213
|
ar = Class.new(Artist)
|
206
214
|
al.many_to_one :artist, :class=>ar
|
@@ -208,217 +216,223 @@ describe "Forme Sequel::Model forms" do
|
|
208
216
|
f = Forme::Form.new(al.new)
|
209
217
|
|
210
218
|
ar.class_eval{def number() "#{self[:name]}1" end}
|
211
|
-
f.input(:artist).to_s.
|
219
|
+
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>'
|
212
220
|
|
213
221
|
ar.class_eval{def title() "#{self[:name]}2" end}
|
214
|
-
f.input(:artist).to_s.
|
222
|
+
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>'
|
215
223
|
|
216
224
|
ar.class_eval{def name() "#{self[:name]}3" end}
|
217
|
-
f.input(:artist).to_s.
|
225
|
+
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>'
|
218
226
|
|
219
227
|
ar.class_eval{def forme_name() "#{self[:name]}4" end}
|
220
|
-
f.input(:artist).to_s.
|
228
|
+
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>'
|
221
229
|
end
|
222
230
|
|
223
|
-
|
231
|
+
it "should raise an error when using an association without a usable name method" do
|
224
232
|
al = Class.new(Album)
|
225
233
|
ar = Class.new(Artist)
|
226
234
|
al.many_to_one :artist, :class=>ar
|
227
235
|
ar.class_eval{undef_method(:name)}
|
228
|
-
proc{Forme::Form.new(al.new).input(:artist)}.
|
236
|
+
proc{Forme::Form.new(al.new).input(:artist)}.must_raise(Sequel::Plugins::Forme::Error)
|
229
237
|
end
|
230
238
|
|
231
|
-
|
232
|
-
@b.input(:tracks).to_s.
|
233
|
-
@c.input(:tracks).to_s.
|
239
|
+
it "should use a multiple select box for one_to_many associations" do
|
240
|
+
@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>'
|
241
|
+
@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>'
|
234
242
|
end
|
235
243
|
|
236
|
-
|
237
|
-
@b.input(:tags).to_s.
|
238
|
-
@c.input(:tags).to_s.
|
244
|
+
it "should use a multiple select box for many_to_many associations" do
|
245
|
+
@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>'
|
246
|
+
@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>'
|
239
247
|
end
|
240
248
|
|
241
|
-
|
242
|
-
@b.input(:atags).to_s.
|
249
|
+
it "should use a multiple select box for pg_array_to_many associations" do
|
250
|
+
@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>'
|
243
251
|
@c.obj.atag_ids.delete(1)
|
244
|
-
@c.input(:atags).to_s.
|
252
|
+
@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>'
|
245
253
|
end
|
246
254
|
|
247
|
-
|
255
|
+
it "should handle an error message on the underlying column for pg_array_to_many associations" do
|
248
256
|
@ab.errors.add(:atag_ids, 'tis not valid')
|
249
|
-
@b.input(:atags).to_s.
|
250
|
-
@b.input(:atags, :as=>:checkbox).to_s.
|
257
|
+
@b.input(:atags).to_s.must_equal '<label>Atags: <select 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">tis not valid</span>'
|
258
|
+
@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 class="error" id="album_atag_ids_3" name="album[atag_ids][]" type="checkbox" value="3"/> u</label><span class="error_message">tis not valid</span>'
|
251
259
|
end
|
252
260
|
|
253
|
-
|
254
|
-
@b.input(:tracks, :multiple=>false).to_s.
|
255
|
-
@c.input(:tags, :multiple=>false).to_s.
|
261
|
+
it "should use a regular select box for *_to_many associations if multiple if false" do
|
262
|
+
@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>'
|
263
|
+
@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>'
|
256
264
|
end
|
257
265
|
|
258
|
-
|
259
|
-
@b.input(:tracks, :
|
260
|
-
@c.input(:tracks, :
|
266
|
+
it "should handle a given :value for one_to_many associations" do
|
267
|
+
@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>'
|
268
|
+
@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>'
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should use multiple checkboxes for one_to_many associations if :as=>:checkbox" do
|
272
|
+
@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>'
|
273
|
+
@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>'
|
261
274
|
end
|
262
275
|
|
263
|
-
|
264
|
-
@b.input(:tags, :as=>:checkbox).to_s.
|
265
|
-
@c.input(:tags, :as=>:checkbox).to_s.
|
276
|
+
it "should use multiple checkboxes for many_to_many associations if :as=>:checkbox" do
|
277
|
+
@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>'
|
278
|
+
@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>'
|
266
279
|
end
|
267
280
|
|
268
|
-
|
269
|
-
@b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />:'
|
270
|
-
@b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />').to_s.
|
281
|
+
it "should handle Raw label for associations with :as=>:checkbox" do
|
282
|
+
@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>'
|
283
|
+
@b.input(:tracks, :as=>:checkbox, :label=>'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>'
|
271
284
|
end
|
272
285
|
|
273
|
-
|
286
|
+
it "should correctly use the forms wrapper for wrapping radio buttons for one_to_many associations with :as=>:checkbox option" do
|
274
287
|
@b = Forme::Form.new(@ab, :wrapper=>:li)
|
275
|
-
@b.input(:tracks, :as=>:checkbox).to_s.
|
288
|
+
@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>'
|
276
289
|
end
|
277
290
|
|
278
|
-
|
291
|
+
it "should support custom wrappers for one_to_many associations with :as=>:checkbox via :tag_wrapper option" do
|
279
292
|
@b = Forme::Form.new(@ab, :wrapper=>:li)
|
280
|
-
@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.
|
293
|
+
@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>'
|
281
294
|
end
|
282
295
|
|
283
|
-
|
284
|
-
@b.input(:artist_name).to_s.
|
285
|
-
@c.input(:artist_name).to_s.
|
296
|
+
it "should use a text field methods not backed by columns" do
|
297
|
+
@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>'
|
298
|
+
@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>'
|
286
299
|
end
|
287
300
|
|
288
|
-
|
301
|
+
it "should handle errors on methods not backed by columns" do
|
289
302
|
@ab.errors.add(:artist_name, 'foo')
|
290
|
-
@b.input(:artist_name).to_s.
|
303
|
+
@b.input(:artist_name).to_s.must_equal '<label>Artist name: <input class="error" id="album_artist_name" name="album[artist_name]" type="text" value="a"/></label><span class="error_message">foo</span>'
|
291
304
|
end
|
292
305
|
|
293
|
-
|
306
|
+
it "should respect a :type option with a schema type as the input type for methods not backed by columns" do
|
294
307
|
def @ab.foo; false end
|
295
|
-
@b.input(:foo, :type=>:boolean, :as=>:select).to_s.
|
308
|
+
@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>'
|
296
309
|
end
|
297
310
|
|
298
|
-
|
311
|
+
it "should respect a :type option with an input type as the input type for methods not backed by columns" do
|
299
312
|
def @ab.foo; "bar" end
|
300
|
-
@b.input(:foo, :type=>:phone).to_s.
|
313
|
+
@b.input(:foo, :type=>:phone).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="phone" value="bar"/></label>'
|
301
314
|
end
|
302
315
|
|
303
|
-
|
316
|
+
it "should correctly show an error message if there is one" do
|
304
317
|
@ab.errors.add(:name, 'tis not valid')
|
305
|
-
@b.input(:name).to_s.
|
318
|
+
@b.input(:name).to_s.must_equal '<label>Name: <input class="error" id="album_name" name="album[name]" type="text" value="b"/></label><span class="error_message">tis not valid</span>'
|
306
319
|
end
|
307
320
|
|
308
|
-
|
321
|
+
it "should correctly show an error message for many_to_one associations if there is one" do
|
309
322
|
@ab.errors.add(:artist_id, 'tis not valid')
|
310
|
-
@b.input(:artist).to_s.
|
323
|
+
@b.input(:artist).to_s.must_equal '<label>Artist: <select 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">tis not valid</span>'
|
311
324
|
end
|
312
325
|
|
313
|
-
|
326
|
+
it "should raise an error for unhandled associations" do
|
314
327
|
al = Class.new(Album)
|
328
|
+
al.one_to_many :tags
|
315
329
|
al.association_reflection(:tags)[:type] = :foo
|
316
|
-
proc{Forme::Form.new(al.new).input(:tags)}.
|
330
|
+
proc{Forme::Form.new(al.new).input(:tags)}.must_raise(Sequel::Plugins::Forme::Error)
|
317
331
|
end
|
318
332
|
|
319
|
-
|
320
|
-
proc{@b.input(:foo)}.
|
333
|
+
it "should raise an error for unhandled fields with no :type option" do
|
334
|
+
proc{@b.input(:foo)}.must_raise(Sequel::Plugins::Forme::Error)
|
321
335
|
end
|
322
336
|
|
323
|
-
|
324
|
-
@b.input(:foo, :type=>:string).to_s.
|
325
|
-
@b.input(:password, :type=>:string).to_s.
|
337
|
+
it "should respect a :type option with a schema type as the input type for unhandled fields" do
|
338
|
+
@b.input(:foo, :type=>:string).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="text"/></label>'
|
339
|
+
@b.input(:password, :type=>:string).to_s.must_equal '<label>Password: <input id="album_password" name="album[password]" type="password"/></label>'
|
326
340
|
end
|
327
341
|
|
328
|
-
|
329
|
-
@b.input(:foo, :type=>:phone).to_s.
|
342
|
+
it "should respect a :type option with an input type as the input type for unhandled fields" do
|
343
|
+
@b.input(:foo, :type=>:phone).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo]" type="phone"/></label>'
|
330
344
|
end
|
331
345
|
|
332
|
-
|
333
|
-
@b.input(:foo, :type=>:phone, :multiple=>true).to_s.
|
346
|
+
it "should respect a :multiple option for the name attribute for unhandled fields" do
|
347
|
+
@b.input(:foo, :type=>:phone, :multiple=>true).to_s.must_equal '<label>Foo: <input id="album_foo" name="album[foo][]" type="phone"/></label>'
|
334
348
|
end
|
335
349
|
|
336
|
-
|
350
|
+
it "should add required attribute if the column doesn't support nil values" do
|
337
351
|
def @ab.db_schema; h = super.dup; h[:name] = h[:name].merge(:allow_null=>false); h end
|
338
|
-
@b.input(:name).to_s.
|
352
|
+
@b.input(:name).to_s.must_equal '<label>Name<abbr title="required">*</abbr>: <input id="album_name" name="album[name]" required="required" type="text" value="b"/></label>'
|
339
353
|
end
|
340
354
|
|
341
|
-
|
342
|
-
Forme.form(@ab){|f| f.subform(:artist){f.input(:name)}}.to_s.
|
355
|
+
it "should use allow nested forms with many_to_one associations" do
|
356
|
+
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" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
|
343
357
|
end
|
344
358
|
|
345
|
-
|
346
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name])}.to_s.
|
359
|
+
it "should have #subform respect an :inputs option" do
|
360
|
+
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" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
|
347
361
|
end
|
348
362
|
|
349
|
-
|
350
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :obj=>Artist.new(:name=>'b'))}.to_s.
|
363
|
+
it "should have #subform respect an :obj option overriding the object to use" do
|
364
|
+
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" name="album[artist_attributes][name]" type="text" value="b"/></label></fieldset></form>'
|
351
365
|
end
|
352
366
|
|
353
|
-
|
354
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :legend=>'Foo')}.to_s.
|
367
|
+
it "should have #subform respect a :legend option if :inputs is used" do
|
368
|
+
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" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
|
355
369
|
end
|
356
370
|
|
357
|
-
|
358
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :legend=>proc{|o| "Foo - #{o.name}"})}.to_s.
|
371
|
+
it "should have #subform respect a callable :legend option if :inputs is used" do
|
372
|
+
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" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
|
359
373
|
end
|
360
374
|
|
361
|
-
|
375
|
+
it "should not add hidden primary key field for new many_to_one associated objects" do
|
362
376
|
@ab.associations[:artist] = Artist.new(:name=>'a')
|
363
|
-
Forme.form(@ab){|f| f.subform(:artist){f.input(:name)}}.to_s.
|
377
|
+
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" name="album[artist_attributes][name]" type="text" value="a"/></label></fieldset></form>'
|
364
378
|
end
|
365
379
|
|
366
|
-
|
367
|
-
Forme.form(@ab){|f| f.subform(:album_info, :obj=>AlbumInfo.new(:info=>'a')){f.input(:info)}}.to_s.
|
380
|
+
it "should use allow nested forms with one_to_one associations" do
|
381
|
+
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" name="album[album_info_attributes][info]" type="text" value="a"/></label></fieldset></form>'
|
368
382
|
end
|
369
383
|
|
370
|
-
|
371
|
-
Forme.form(@ab){|f| f.subform(:tracks){f.input(:name)}}.to_s.
|
384
|
+
it "should use allow nested forms with one_to_many associations" do
|
385
|
+
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" 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" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
|
372
386
|
end
|
373
387
|
|
374
|
-
|
375
|
-
Forme.form(@ab){|f| f.subform(:tracks, :obj=>[Track.new(:name=>'x'), Track.load(:id=>5, :name=>'y')]){f.input(:name)}}.to_s.
|
388
|
+
it "should support :obj option for *_to_many associations" do
|
389
|
+
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" 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" name="album[tracks_attributes][1][name]" type="text" value="y"/></label></fieldset></form>'
|
376
390
|
end
|
377
391
|
|
378
|
-
|
379
|
-
Forme.form(@ab){|f| f.subform(:tracks, :inputs=>[:name])}.to_s.
|
392
|
+
it "should auto number legends when using subform with inputs for *_to_many associations" do
|
393
|
+
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" 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" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
|
380
394
|
end
|
381
395
|
|
382
|
-
|
383
|
-
Forme.form(@ab){|f| f.subform(:tracks, :inputs=>[:name], :legend=>proc{|o, i| "Track #{i} (#{o.name})"})}.to_s.
|
396
|
+
it "should support callable :legend option when using subform with inputs for *_to_many associations" do
|
397
|
+
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" 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" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
|
384
398
|
end
|
385
399
|
|
386
|
-
|
400
|
+
it "should not add hidden primary key field for nested forms with one_to_many associations with new objects" do
|
387
401
|
@ab.associations[:tracks] = [Track.new(:name=>'m'), Track.new(:name=>'n')]
|
388
|
-
Forme.form(@ab){|f| f.subform(:tracks){f.input(:name)}}.to_s.
|
402
|
+
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" 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" name="album[tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></form>'
|
389
403
|
end
|
390
404
|
|
391
|
-
|
392
|
-
Forme.form(@ab){|f| f.subform(:tags){f.input(:name)}}.to_s.
|
405
|
+
it "should use allow nested forms with many_to_many associations" do
|
406
|
+
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" 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" name="album[tags_attributes][1][name]" type="text" value="t"/></label></fieldset></form>'
|
393
407
|
end
|
394
408
|
|
395
|
-
|
409
|
+
it "should not add hidden primary key field for nested forms with many_to_many associations with new objects" do
|
396
410
|
@ab.associations[:tags] = [Tag.new(:name=>'s'), Tag.new(:name=>'t')]
|
397
|
-
Forme.form(@ab){|f| f.subform(:tags){f.input(:name)}}.to_s.
|
411
|
+
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" 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" name="album[tags_attributes][1][name]" type="text" value="t"/></label></fieldset></form>'
|
398
412
|
end
|
399
413
|
|
400
|
-
|
401
|
-
Forme.form(Artist[1]){|f| f.subform(:albums){f.input(:name); f.subform(:tracks){f.input(:name)}}}.to_s.to_s.
|
414
|
+
it "should handle multiple nested levels" do
|
415
|
+
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" 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" 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" name="artist[albums_attributes][0][tracks_attributes][1][name]" type="text" value="n"/></label></fieldset></fieldset></form>'
|
402
416
|
end
|
403
417
|
|
404
|
-
|
405
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true)}.to_s.
|
418
|
+
it "should have #subform :grid option create a grid" do
|
419
|
+
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><tr><th>Name</th></tr><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" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></table></form>'
|
406
420
|
end
|
407
421
|
|
408
|
-
|
409
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :inputs_opts=>{:attr=>{:class=>'foo'}})}.to_s.
|
422
|
+
it "should have #subform :grid option respect :inputs_opts option to pass options to inputs" do
|
423
|
+
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><tr><th>Name</th></tr><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" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></table></form>'
|
410
424
|
end
|
411
425
|
|
412
|
-
|
413
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :legend=>'Foo', :labels=>%w'Bar')}.to_s.
|
426
|
+
it "should have #subform :grid option handle :legend and :labels options" do
|
427
|
+
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><tr><th>Bar</th></tr><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" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></table></form>'
|
414
428
|
end
|
415
429
|
|
416
|
-
|
417
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :legend=>nil, :labels=>nil)}.to_s.
|
430
|
+
it "should have #subform :grid option handle :legend and :labels nil values" do
|
431
|
+
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><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" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></table></form>'
|
418
432
|
end
|
419
433
|
|
420
|
-
|
421
|
-
Forme.form(@ab){|f| f.subform(:artist, :inputs=>[:name], :grid=>true, :skip_primary_key=>true)}.to_s.
|
434
|
+
it "should have #subform :grid option handle :skip_primary_key option" do
|
435
|
+
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><tr><th>Name</th></tr><tr><td class="string"><input id="album_artist_attributes_name" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></table></form>'
|
422
436
|
end
|
423
437
|
|
424
438
|
end
|
@@ -426,37 +440,37 @@ end
|
|
426
440
|
describe "Forme Sequel plugin default input types based on column names" do
|
427
441
|
def f(name)
|
428
442
|
DB.create_table!(:test){String name}
|
429
|
-
Forme::Form.new(Class.new(Sequel::Model){def self.name; 'Test' end; set_dataset :test}.new).input(name, :value=>'foo')
|
443
|
+
Forme::Form.new(Class.new(Sequel::Model).class_eval{def self.name; 'Test' end; set_dataset :test}.new).input(name, :value=>'foo')
|
430
444
|
end
|
431
445
|
|
432
|
-
|
433
|
-
f(:password).to_s.
|
446
|
+
it "should use password input with no value for string columns with name password" do
|
447
|
+
f(:password).to_s.must_equal '<label>Password: <input id="test_password" name="test[password]" type="password"/></label>'
|
434
448
|
end
|
435
449
|
|
436
|
-
|
437
|
-
f(:email).to_s.
|
450
|
+
it "should use email input for string columns with name email" do
|
451
|
+
f(:email).to_s.must_equal '<label>Email: <input id="test_email" name="test[email]" type="email" value="foo"/></label>'
|
438
452
|
end
|
439
453
|
|
440
|
-
|
441
|
-
f(:phone).to_s.
|
442
|
-
f(:fax).to_s.
|
454
|
+
it "should use tel input for string columns with name phone or fax" do
|
455
|
+
f(:phone).to_s.must_equal '<label>Phone: <input id="test_phone" name="test[phone]" type="tel" value="foo"/></label>'
|
456
|
+
f(:fax).to_s.must_equal '<label>Fax: <input id="test_fax" name="test[fax]" type="tel" value="foo"/></label>'
|
443
457
|
end
|
444
458
|
|
445
|
-
|
446
|
-
f(:url).to_s.
|
447
|
-
f(:uri).to_s.
|
448
|
-
f(:website).to_s.
|
459
|
+
it "should use url input for string columns with name url, uri, or website" do
|
460
|
+
f(:url).to_s.must_equal '<label>Url: <input id="test_url" name="test[url]" type="url" value="foo"/></label>'
|
461
|
+
f(:uri).to_s.must_equal '<label>Uri: <input id="test_uri" name="test[uri]" type="url" value="foo"/></label>'
|
462
|
+
f(:website).to_s.must_equal '<label>Website: <input id="test_website" name="test[website]" type="url" value="foo"/></label>'
|
449
463
|
end
|
450
464
|
end
|
451
465
|
|
452
466
|
describe "Forme Sequel plugin default input types based on column type" do
|
453
467
|
def f(type)
|
454
468
|
DB.create_table!(:test){column :foo, type}
|
455
|
-
Forme::Form.new(Class.new(Sequel::Model){def self.name; 'Test' end; set_dataset :test}.new).input(:foo, :value=>'foo')
|
469
|
+
Forme::Form.new(Class.new(Sequel::Model).class_eval{def self.name; 'Test' end; set_dataset :test}.new).input(:foo, :value=>'foo')
|
456
470
|
end
|
457
471
|
|
458
|
-
|
459
|
-
f(File).to_s.
|
472
|
+
it "should use a file input for blob types" do
|
473
|
+
f(File).to_s.must_equal '<label>Foo: <input id="test_foo" name="test[foo]" type="file"/></label>'
|
460
474
|
end
|
461
475
|
end
|
462
476
|
|
@@ -468,35 +482,35 @@ describe "Forme Sequel::Model validation parsing" do
|
|
468
482
|
Forme::Form.new(c.new)
|
469
483
|
end
|
470
484
|
|
471
|
-
|
472
|
-
f(:validates_format_of, :name, :with=>/[A-z]+/).input(:name).to_s.
|
485
|
+
it "should turn format into a pattern" do
|
486
|
+
f(:validates_format_of, :name, :with=>/[A-z]+/).input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" pattern="[A-z]+" type="text"/></label>'
|
473
487
|
end
|
474
488
|
|
475
|
-
|
476
|
-
f(:validates_format_of, :name, :with=>/[A-z]+/, :title=>'Foo').input(:name).to_s.
|
489
|
+
it "should respect :title option for format" do
|
490
|
+
f(:validates_format_of, :name, :with=>/[A-z]+/, :title=>'Foo').input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" pattern="[A-z]+" title="Foo" type="text"/></label>'
|
477
491
|
end
|
478
492
|
|
479
|
-
|
480
|
-
f(:validates_length_of, :name, :maximum=>10).input(:name).to_s.
|
481
|
-
f(:validates_length_of, :name, :is=>10).input(:name).to_s.
|
482
|
-
f(:validates_length_of, :name, :within=>2..10).input(:name).to_s.
|
483
|
-
f(:validates_length_of, :name, :within=>2...11).input(:name).to_s.
|
493
|
+
it "should use maxlength for length :maximum, :is, and :within" do
|
494
|
+
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>'
|
495
|
+
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>'
|
496
|
+
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>'
|
497
|
+
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>'
|
484
498
|
end
|
485
499
|
|
486
|
-
|
487
|
-
f(:validates_numericality_of, :name).input(:name).to_s.
|
500
|
+
it "should turn numericality into a pattern" do
|
501
|
+
f(:validates_numericality_of, :name).input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="must be a number" type="text"/></label>'
|
488
502
|
end
|
489
503
|
|
490
|
-
|
491
|
-
f(:validates_numericality_of, :name, :only_integer=>true).input(:name).to_s.
|
504
|
+
it "should turn numericality :only_integer into a pattern" do
|
505
|
+
f(:validates_numericality_of, :name, :only_integer=>true).input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" pattern="^[+\-]?\d+$" title="must be a number" type="text"/></label>'
|
492
506
|
end
|
493
507
|
|
494
|
-
|
495
|
-
f(:validates_numericality_of, :name, :title=>'Foo').input(:name).to_s.
|
508
|
+
it "should respect :title option for numericality" do
|
509
|
+
f(:validates_numericality_of, :name, :title=>'Foo').input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="Foo" type="text"/></label>'
|
496
510
|
end
|
497
511
|
|
498
|
-
|
499
|
-
f(:validates_uniqueness_of, :name, :placeholder=>'must be unique').input(:name).to_s.
|
512
|
+
it "should respect :placeholder option for any validation" do
|
513
|
+
f(:validates_uniqueness_of, :name, :placeholder=>'must be unique').input(:name).to_s.must_equal '<label>Name: <input id="album_name" name="album[name]" placeholder="must be unique" type="text"/></label>'
|
500
514
|
end
|
501
515
|
|
502
516
|
end
|