forme 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,523 +0,0 @@
1
- require_relative 'spec_helper'
2
- require_relative 'sequel_helper'
3
- require_relative '../lib/forme/bs3'
4
-
5
- describe "Forme Sequel::Model BS3 forms" do
6
- before do
7
- @ab = Album[1]
8
- @b = Forme::Form.new(@ab, :config=>:bs3)
9
- @ac = Album[2]
10
- @c = Forme::Form.new(@ac, :config=>:bs3)
11
- end
12
-
13
- it "should add appropriate attributes by default" do
14
- @b.form.must_equal '<form class="forme album" method="post"></form>'
15
- end
16
-
17
- it "should allow overriding of attributes" do
18
- @b.form(:class=>:foo, :method=>:get).must_equal '<form class="foo forme album" method="get"></form>'
19
- end
20
-
21
- it "should handle invalid methods" do
22
- def @ab.db_schema
23
- super.merge(:foo=>{:type=>:bar})
24
- end
25
- @b.input(:foo, :value=>'baz').must_equal '<div class="bar form-group"><label for="album_foo">Foo</label> <input class="form-control" id="album_foo" name="album[foo]" type="text" value="baz"/></div>'
26
- end
27
-
28
- it "should allow an array of classes" do
29
- @b.form(:class=>[:foo, :bar]).must_equal '<form class="foo bar forme album" method="post"></form>'
30
- @b.form(:class=>[:foo, [:bar, :baz]]).must_equal '<form class="foo bar baz forme album" method="post"></form>'
31
- end
32
-
33
- it "should use a text field for strings" do
34
- @b.input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></div>'
35
- @c.input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" type="text" value="c"/></div>'
36
- end
37
-
38
- it "should allow :as=>:textarea to use a textarea" do
39
- @b.input(:name, :as=>:textarea).must_equal '<div class="form-group string"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" maxlength="255" name="album[name]">b</textarea></div>'
40
- @c.input(:name, :as=>:textarea).must_equal '<div class="form-group string"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" maxlength="255" name="album[name]">c</textarea></div>'
41
- end
42
-
43
- it "should allow :type=>:textarea to use a textarea" do
44
- @b.input(:name, :type=>:textarea).must_equal '<div class="form-group"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" maxlength="255" name="album[name]">b</textarea></div>'
45
- @c.input(:name, :type=>:textarea).must_equal '<div class="form-group"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" maxlength="255" name="album[name]">c</textarea></div>'
46
- end
47
-
48
- it "should use number inputs for integers" do
49
- @b.input(:copies_sold).must_equal '<div class="form-group integer"><label for="album_copies_sold">Copies sold</label> <input class="form-control" id="album_copies_sold" inputmode="numeric" name="album[copies_sold]" pattern="-?[0-9]*" type="text" value="10"/></div>'
50
- end
51
-
52
- it "should use date inputs for Dates" do
53
- @b.input(:release_date).must_equal '<div class="date form-group"><label for="album_release_date">Release date</label> <input class="form-control" id="album_release_date" name="album[release_date]" type="date" value="2011-06-05"/></div>'
54
- end
55
-
56
- it "should use datetime inputs for Time" do
57
- @b.input(:created_at).must_match %r{<div class="datetime form-group"><label for="album_created_at">Created at</label> <input class="form-control" id="album_created_at" name="album\[created_at\]" type="datetime-local" value="2011-06-05T00:00:00.000"/></div>}
58
- end
59
-
60
- it "should use datetime inputs for DateTimes" do
61
- @ab.values[:created_at] = DateTime.new(2011, 6, 5)
62
- @b.input(:created_at).must_equal '<div class="datetime form-group"><label for="album_created_at">Created at</label> <input class="form-control" id="album_created_at" name="album[created_at]" type="datetime-local" value="2011-06-05T00:00:00.000"/></div>'
63
- end
64
-
65
- it "should include type as wrapper class" do
66
- @ab.values[:created_at] = DateTime.new(2011, 6, 5)
67
- f = Forme::Form.new(@ab, :config=>:bs3)
68
- f.input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" type="text" value="b"/></div>'
69
- f.input(:release_date).must_equal '<div class="date form-group"><label for="album_release_date">Release date</label> <input class="form-control" id="album_release_date" name="album[release_date]" type="date" value="2011-06-05"/></div>'
70
- f.input(:created_at).must_equal '<div class="datetime form-group"><label for="album_created_at">Created at</label> <input class="form-control" id="album_created_at" name="album[created_at]" type="datetime-local" value="2011-06-05T00:00:00.000"/></div>'
71
- end
72
-
73
- it "should include required * in label if required" do
74
- @b.input(:name, :required=>true).must_equal '<div class="form-group required string"><label for="album_name">Name<abbr title="required">*</abbr></label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></div>'
75
- end
76
-
77
- it "should add required to label even if :label option specified" do
78
- @b.input(:name, :required=>true, :label=>'Foo').must_equal '<div class="form-group required string"><label for="album_name">Foo<abbr title="required">*</abbr></label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></div>'
79
- end
80
-
81
- it "should include required wrapper class if required" do
82
- f = Forme::Form.new(@ab, :config=>:bs3, :wrapper=>:li)
83
- f.input(:name, :required=>true).must_equal '<li class="string required"><label for="album_name">Name<abbr title="required">*</abbr></label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></li>'
84
- end
85
-
86
- it "should use a select box for tri-valued boolean fields" do
87
- @b.input(:gold).must_equal '<div class="boolean form-group"><label for="album_gold">Gold</label> <select class="form-control" id="album_gold" name="album[gold]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></div>'
88
- @c.input(:gold).must_equal '<div class="boolean form-group"><label for="album_gold">Gold</label> <select class="form-control" id="album_gold" name="album[gold]"><option value=""></option><option selected="selected" value="t">True</option><option value="f">False</option></select></div>'
89
- end
90
-
91
- it "should respect :true_label and :false_label options for tri-valued boolean fields" do
92
- @b.input(:gold, :true_label=>"Foo", :false_label=>"Bar").must_equal '<div class="boolean form-group"><label for="album_gold">Gold</label> <select class="form-control" id="album_gold" name="album[gold]"><option value=""></option><option value="t">Foo</option><option selected="selected" value="f">Bar</option></select></div>'
93
- end
94
-
95
- it "should respect :true_value and :false_value options for tri-valued boolean fields" do
96
- @b.input(:gold, :true_value=>"Foo", :false_value=>"Bar").must_equal '<div class="boolean form-group"><label for="album_gold">Gold</label> <select class="form-control" id="album_gold" name="album[gold]"><option value=""></option><option value="Foo">True</option><option value="Bar">False</option></select></div>'
97
- end
98
-
99
- it "should respect :add_blank option for tri-valued boolean fields" do
100
- @b.input(:gold, :add_blank=>'NULL').must_equal '<div class="boolean form-group"><label for="album_gold">Gold</label> <select class="form-control" id="album_gold" name="album[gold]"><option value="">NULL</option><option value="t">True</option><option selected="selected" value="f">False</option></select></div>'
101
- end
102
-
103
- it "should use a select box for dual-valued boolean fields where :required => false" do
104
- @b.input(:platinum, :required=>false).must_equal '<div class="boolean form-group"><label for="album_platinum">Platinum</label> <select class="form-control" id="album_platinum" name="album[platinum]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></div>'
105
- @c.input(:platinum, :required=>false).must_equal '<div class="boolean form-group"><label for="album_platinum">Platinum</label> <select class="form-control" id="album_platinum" name="album[platinum]"><option value=""></option><option selected="selected" value="t">True</option><option value="f">False</option></select></div>'
106
- end
107
-
108
- it "should use a checkbox for dual-valued boolean fields" do
109
- @b.input(:platinum).must_equal '<div class="boolean checkbox"><label for="album_platinum"><input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><input id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label></div>'
110
- @c.input(:platinum).must_equal '<div class="boolean checkbox"><label for="album_platinum"><input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><input checked="checked" id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label></div>'
111
- end
112
-
113
- it "should use radio buttons for boolean fields if :as=>:radio is used" do
114
- @b.input(:platinum, :as=>:radio).must_equal '<div class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></div>'
115
- @c.input(:platinum, :as=>:radio).must_equal '<div class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input checked="checked" id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></div>'
116
- end
117
-
118
- it "should wrap both inputs if :as=>:radio is used" do
119
- @b = Forme::Form.new(@ab, :config=>:bs3, :wrapper=>:li)
120
- @b.input(:platinum, :as=>:radio).must_equal '<div class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></div>'
121
- @b.input(:platinum, :as=>:radio,:wrapper=>:li).must_equal '<li class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></li>'
122
- @b.input(:platinum, :as=>:radio, :wrapper=>:div, :tag_wrapper=>:span).must_equal '<div class="boolean radioset"><label>Platinum</label><span><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></span><span><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></span></div>'
123
- end
124
-
125
- it "should handle errors on radio buttons for boolean fields if :as=>:radio is used" do
126
- @ab.errors.add(:platinum, 'foo')
127
- @b.input(:platinum, :as=>:radio).must_equal '<div class="boolean radioset has-error"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div><span class="help-block with-errors">foo</span></div>'
128
- @b.input(:platinum, :as=>:radio, :wrapper=>:li).must_equal '<li class="boolean radioset has-error"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div><span class="help-block with-errors">foo</span></li>'
129
- @b.input(:platinum, :as=>:radio, :wrapper=>:bs3).must_equal '<div class="boolean form-group has-error radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div><span class="help-block with-errors">foo</span></div>'
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 />')).must_equal '<div class="boolean radioset"><label>Foo:<br /></label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></div>'
134
- @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />').must_equal '<div class="boolean radioset"><label>Foo:&lt;br /&gt;</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></div>'
135
- @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />', :wrapper=>nil).must_equal '<label>Foo:&lt;br /&gt;</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div>'
136
- @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />', :wrapper=>:li).must_equal '<li class="boolean radioset"><label>Foo:&lt;br /&gt;</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></li>'
137
- @b.input(:platinum, :as=>:radio, :label=>'Foo:<br />', :wrapper=>:bs3).must_equal '<div class="boolean form-group radioset"><label>Foo:&lt;br /&gt;</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> No</label></div></div>'
138
- end
139
-
140
- it "should respect :true_label and :false_label options for boolean fields if :as=>:radio is used" do
141
- @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar").must_equal '<div class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label></div></div>'
142
- @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar",:wrapper=>nil).must_equal '<label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label></div>'
143
- @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar",:wrapper=>:li).must_equal '<li class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label></div></li>'
144
- @b.input(:platinum, :as=>:radio, :true_label=>"Foo", :false_label=>"Bar",:wrapper=>:bs3).must_equal '<div class="boolean form-group radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="t"/> Foo</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="f"/> Bar</label></div></div>'
145
- end
146
-
147
- it "should respect :true_value and :false_value options for boolean fields if :as=>:radio is used" do
148
- @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar").must_equal '<div class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label></div></div>'
149
- @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar", :wrapper=>nil).must_equal '<label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label></div>'
150
- @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar", :wrapper=>:li).must_equal '<li class="boolean radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label></div></li>'
151
- @b.input(:platinum, :as=>:radio, :true_value=>"Foo", :false_value=>"Bar", :wrapper=>:bs3).must_equal '<div class="boolean form-group radioset"><label>Platinum</label><div class="radio"><label class="option" for="album_platinum_yes"><input id="album_platinum_yes" name="album[platinum]" type="radio" value="Foo"/> Yes</label></div><div class="radio"><label class="option" for="album_platinum_no"><input checked="checked" id="album_platinum_no" name="album[platinum]" type="radio" value="Bar"/> No</label></div></div>'
152
- end
153
-
154
- it "should use a select box for many_to_one associations" do
155
- @b.input(:artist).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">a</option><option value="2">d</option></select></div>'
156
- @c.input(:artist).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a</option><option selected="selected" value="2">d</option></select></div>'
157
- end
158
-
159
- it "should not add a blank option by default if there is a default value and it is required" do
160
- @b.input(:artist, :required=>true).must_equal '<div class="form-group many_to_one required"><label for="album_artist_id">Artist<abbr title="required">*</abbr></label> <select class="form-control" id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></div>'
161
- end
162
-
163
- it "should allow overriding default input type using a :type option" do
164
- @b.input(:artist, :type=>:string, :value=>nil).must_equal '<div class="form-group"><label for="album_artist">Artist</label> <input class="form-control" id="album_artist" name="album[artist]" type="text"/></div>'
165
- end
166
-
167
- it "should use a required wrapper tag for many_to_one required associations" do
168
- @b.input(:artist, :required=>true, :wrapper=>:li).must_equal '<li class="many_to_one required"><label for="album_artist_id">Artist<abbr title="required">*</abbr></label> <select class="form-control" id="album_artist_id" name="album[artist_id]" required="required"><option selected="selected" value="1">a</option><option value="2">d</option></select></li>'
169
- end
170
-
171
- it "should use a set of radio buttons for many_to_one associations with :as=>:radio option" do
172
- @b.input(:artist, :as=>:radio).must_equal '<div class="many_to_one radioset"><label>Artist</label><div class="radio"><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div></div>'
173
- @b.input(:artist, :as=>:radio, :wrapper=>nil).must_equal '<label>Artist</label><div class="radio"><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div>'
174
- @c.input(:artist, :as=>:radio).must_equal '<div class="many_to_one radioset"><label>Artist</label><div class="radio"><label class="option" for="album_artist_id_1"><input id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input checked="checked" id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div></div>'
175
- @c.input(:artist, :as=>:radio, :wrapper=>:div).must_equal '<div class="many_to_one radioset"><label>Artist</label><div class="radio"><label class="option" for="album_artist_id_1"><input id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input checked="checked" id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div></div>'
176
- end
177
-
178
- it "should handle Raw label for many_to_one associations with :as=>:radio option" do
179
- @b.input(:artist, :as=>:radio, :label=>Forme.raw('Foo:<br />')).must_equal '<div class="many_to_one radioset"><label>Foo:<br /></label><div class="radio"><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div></div>'
180
- @b.input(:artist, :as=>:radio, :label=>'Foo<br />').must_equal '<div class="many_to_one radioset"><label>Foo&lt;br /&gt;</label><div class="radio"><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div></div>'
181
- end
182
-
183
- it "should correctly use the forms wrapper for wrapping radio buttons for many_to_one associations with :as=>:radio option" do
184
- @b = Forme::Form.new(@ab, :config=>:bs3, :wrapper=>:li)
185
- @b.input(:artist, :as=>:radio).must_equal '<div class="many_to_one radioset"><label>Artist</label><div class="radio"><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div></div>'
186
- @b.input(:artist, :as=>:radio,:wrapper=>:li).must_equal '<li class="many_to_one radioset"><label>Artist</label><div class="radio"><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div></li>'
187
- @b.input(:artist, :as=>:radio,:wrapper=>nil).must_equal '<label>Artist</label><div class="radio"><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></div><div class="radio"><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></div>'
188
- end
189
-
190
- it "should support custom wrappers for many_to_one associations with :as=>:radio via :tag_wrapper option" do
191
- @b = Forme::Form.new(@ab, :config=>:bs3, :wrapper=>:li)
192
- @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><label>Artist</label><span><label class="option" for="album_artist_id_1"><input checked="checked" id="album_artist_id_1" name="album[artist_id]" type="radio" value="1"/> a</label></span><span><label class="option" for="album_artist_id_2"><input id="album_artist_id_2" name="album[artist_id]" type="radio" value="2"/> d</label></span></div>'
193
- end
194
-
195
- it "should respect an :options entry" do
196
- @b.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">a</option><option value="3">d</option></select></div>'
197
- @c.input(:artist, :options=>Artist.order(:name).map{|a| [a.name, a.id+1]}).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">a</option><option value="3">d</option></select></div>'
198
- end
199
-
200
- it "should support :name_method option for choosing name method" do
201
- @b.input(:artist, :name_method=>:idname).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">1a</option><option value="2">2d</option></select></div>'
202
- @c.input(:artist, :name_method=>:idname).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">1a</option><option selected="selected" value="2">2d</option></select></div>'
203
- end
204
-
205
- it "should support :name_method option being a callable object" do
206
- @b.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">1a1a</option><option value="2">2d2d</option></select></div>'
207
- @c.input(:artist, :name_method=>lambda{|obj| obj.idname * 2}).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">1a1a</option><option selected="selected" value="2">2d2d</option></select></div>'
208
- end
209
-
210
- it "should support :dataset option providing dataset to search" do
211
- @b.input(:artist, :dataset=>Artist.reverse_order(:name)).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">d</option><option selected="selected" value="1">a</option></select></div>'
212
- @c.input(:artist, :dataset=>Artist.reverse_order(:name)).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">d</option><option value="1">a</option></select></div>'
213
- end
214
-
215
- it "should support :dataset option being a callback proc returning modified dataset to search" do
216
- @b.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="2">d</option><option selected="selected" value="1">a</option></select></div>'
217
- @c.input(:artist, :dataset=>proc{|ds| ds.reverse_order(:name)}).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="2">d</option><option value="1">a</option></select></div>'
218
- end
219
-
220
- it "should try a list of methods to get a suitable one for select box naming" do
221
- al = Class.new(Album){def self.name() 'Album' end}
222
- ar = Class.new(Artist)
223
- al.many_to_one :artist, :class=>ar
224
- ar.class_eval{undef_method(:name)}
225
- f = Forme::Form.new(al.new, { :config=>:bs3 })
226
-
227
- ar.class_eval{def number() "#{self[:name]}1" end}
228
- f.input(:artist).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a1</option><option value="2">d1</option></select></div>'
229
-
230
- ar.class_eval{def title() "#{self[:name]}2" end}
231
- f.input(:artist).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a2</option><option value="2">d2</option></select></div>'
232
-
233
- ar.class_eval{def name() "#{self[:name]}3" end}
234
- f.input(:artist).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a3</option><option value="2">d3</option></select></div>'
235
-
236
- ar.class_eval{def forme_name() "#{self[:name]}4" end}
237
- f.input(:artist).must_equal '<div class="form-group many_to_one"><label for="album_artist_id">Artist</label> <select class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option value="1">a4</option><option value="2">d4</option></select></div>'
238
- end
239
-
240
- it "should raise an error when using an association without a usable name method" do
241
- al = Class.new(Album)
242
- ar = Class.new(Artist)
243
- al.many_to_one :artist, :class=>ar
244
- ar.class_eval{undef_method(:name)}
245
- proc{Forme::Form.new(al.new, :config=>:bs3).input(:artist)}.must_raise(Sequel::Plugins::Forme::Error)
246
- end
247
-
248
- it "should use a multiple select box for one_to_many associations" do
249
- @b.input(:tracks).must_equal '<div class="form-group one_to_many"><label for="album_track_pks">Tracks</label> <select class="form-control" 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></div>'
250
- @c.input(:tracks).must_equal '<div class="form-group one_to_many"><label for="album_track_pks">Tracks</label> <select class="form-control" 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></div>'
251
- end
252
-
253
- it "should use a multiple select box for many_to_many associations" do
254
- @b.input(:tags).must_equal '<div class="form-group many_to_many"><label for="album_tag_pks">Tags</label> <select class="form-control" 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></div>'
255
- @c.input(:tags).must_equal '<div class="form-group many_to_many"><label for="album_tag_pks">Tags</label> <select class="form-control" 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></div>'
256
- end
257
-
258
- it "should use a multiple select box for pg_array_to_many associations" do
259
- @b.input(:atags).must_equal '<div class="form-group pg_array_to_many"><label for="album_atag_ids">Atags</label> <select class="form-control" 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></div>'
260
- @c.obj.atag_ids.delete(1)
261
- @c.input(:atags).must_equal '<div class="form-group pg_array_to_many"><label for="album_atag_ids">Atags</label> <select class="form-control" 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></div>'
262
- end
263
-
264
- it "should handle an error message on the underlying column for pg_array_to_many associations" do
265
- @ab.errors.add(:atag_ids, 'tis not valid')
266
- @b.input(:atags).must_equal '<div class="form-group has-error pg_array_to_many"><label for="album_atag_ids">Atags</label> <select aria-describedby="album_atag_ids_error_message" aria-invalid="true" class="form-control" 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><span class="help-block with-errors" id="album_atag_ids_error_message">tis not valid</span></div>'
267
- @b.input(:atags, :as=>:checkbox).must_equal '<div class="pg_array_to_many checkboxset has-error"><label>Atags</label><div class="checkbox"><label class="option" for="album_atag_ids_1"><input checked="checked" id="album_atag_ids_1" name="album[atag_ids][]" type="checkbox" value="1"/> s</label></div><div class="checkbox"><label class="option" for="album_atag_ids_2"><input checked="checked" id="album_atag_ids_2" name="album[atag_ids][]" type="checkbox" value="2"/> t</label></div><div class="checkbox"><label class="option" for="album_atag_ids_3"><input id="album_atag_ids_3" name="album[atag_ids][]" type="checkbox" value="3"/> u</label></div><span class="help-block with-errors">tis not valid</span></div>'
268
- @b.input(:atags, :as=>:checkbox,:wrapper=>:li).must_equal '<li class="pg_array_to_many checkboxset has-error"><label>Atags</label><div class="checkbox"><label class="option" for="album_atag_ids_1"><input checked="checked" id="album_atag_ids_1" name="album[atag_ids][]" type="checkbox" value="1"/> s</label></div><div class="checkbox"><label class="option" for="album_atag_ids_2"><input checked="checked" id="album_atag_ids_2" name="album[atag_ids][]" type="checkbox" value="2"/> t</label></div><div class="checkbox"><label class="option" for="album_atag_ids_3"><input id="album_atag_ids_3" name="album[atag_ids][]" type="checkbox" value="3"/> u</label></div><span class="help-block with-errors">tis not valid</span></li>'
269
- end
270
-
271
- it "should use a regular select box for *_to_many associations if multiple if false" do
272
- @b.input(:tracks, :multiple=>false).must_equal '<div class="form-group one_to_many"><label for="album_track_pks">Tracks</label> <select class="form-control" id="album_track_pks" name="album[track_pks][]"><option value="1">m</option><option value="2">n</option><option value="3">o</option></select></div>'
273
- @c.input(:tags, :multiple=>false).must_equal '<div class="form-group many_to_many"><label for="album_tag_pks">Tags</label> <select class="form-control" id="album_tag_pks" name="album[tag_pks][]"><option value="1">s</option><option value="2">t</option><option value="3">u</option></select></div>'
274
- end
275
-
276
- it "should use multiple checkboxes for one_to_many associations if :as=>:checkbox" do
277
- @b.input(:tracks).must_equal '<div class="form-group one_to_many"><label for="album_track_pks">Tracks</label> <select class="form-control" 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></div>'
278
- @b.input(:tracks, :as=>:checkbox).must_equal '<div class="one_to_many checkboxset"><label>Tracks</label><div class="checkbox"><label class="option" for="album_track_pks_1"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></div><div class="checkbox"><label class="option" for="album_track_pks_2"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></div><div class="checkbox"><label class="option" for="album_track_pks_3"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></div></div>'
279
- @c.input(:tracks, :as=>:checkbox,:wrapper=>:li).must_equal '<li class="one_to_many checkboxset"><label>Tracks</label><div class="checkbox"><label class="option" for="album_track_pks_1"><input id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></div><div class="checkbox"><label class="option" for="album_track_pks_2"><input id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></div><div class="checkbox"><label class="option" for="album_track_pks_3"><input checked="checked" id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></div></li>'
280
- end
281
-
282
- it "should use multiple checkboxes for many_to_many associations if :as=>:checkbox" do
283
- @b.input(:tags, :as=>:checkbox).must_equal '<div class="many_to_many checkboxset"><label>Tags</label><div class="checkbox"><label class="option" for="album_tag_pks_1"><input checked="checked" id="album_tag_pks_1" name="album[tag_pks][]" type="checkbox" value="1"/> s</label></div><div class="checkbox"><label class="option" for="album_tag_pks_2"><input checked="checked" id="album_tag_pks_2" name="album[tag_pks][]" type="checkbox" value="2"/> t</label></div><div class="checkbox"><label class="option" for="album_tag_pks_3"><input id="album_tag_pks_3" name="album[tag_pks][]" type="checkbox" value="3"/> u</label></div></div>'
284
- @c.input(:tags, :as=>:checkbox).must_equal '<div class="many_to_many checkboxset"><label>Tags</label><div class="checkbox"><label class="option" for="album_tag_pks_1"><input id="album_tag_pks_1" name="album[tag_pks][]" type="checkbox" value="1"/> s</label></div><div class="checkbox"><label class="option" for="album_tag_pks_2"><input checked="checked" id="album_tag_pks_2" name="album[tag_pks][]" type="checkbox" value="2"/> t</label></div><div class="checkbox"><label class="option" for="album_tag_pks_3"><input id="album_tag_pks_3" name="album[tag_pks][]" type="checkbox" value="3"/> u</label></div></div>'
285
- @c.input(:tags, :as=>:checkbox, :wrapper=>:div).must_equal '<div class="many_to_many checkboxset"><label>Tags</label><div class="checkbox"><label class="option" for="album_tag_pks_1"><input id="album_tag_pks_1" name="album[tag_pks][]" type="checkbox" value="1"/> s</label></div><div class="checkbox"><label class="option" for="album_tag_pks_2"><input checked="checked" id="album_tag_pks_2" name="album[tag_pks][]" type="checkbox" value="2"/> t</label></div><div class="checkbox"><label class="option" for="album_tag_pks_3"><input id="album_tag_pks_3" name="album[tag_pks][]" type="checkbox" value="3"/> u</label></div></div>'
286
- end
287
-
288
- it "should handle Raw label for associations with :as=>:checkbox" do
289
- @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />').must_equal '<div class="one_to_many checkboxset"><label>Foo&lt;br /&gt;</label><div class="checkbox"><label class="option" for="album_track_pks_1"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></div><div class="checkbox"><label class="option" for="album_track_pks_2"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></div><div class="checkbox"><label class="option" for="album_track_pks_3"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></div></div>'
290
- @b.input(:tracks, :as=>:checkbox, :label=>'Foo<br />',:wrapper=>:div).must_equal '<div class="one_to_many checkboxset"><label>Foo&lt;br /&gt;</label><div class="checkbox"><label class="option" for="album_track_pks_1"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></div><div class="checkbox"><label class="option" for="album_track_pks_2"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></div><div class="checkbox"><label class="option" for="album_track_pks_3"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></div></div>'
291
- end
292
-
293
- it "should correctly use the forms wrapper for wrapping radio buttons for one_to_many associations with :as=>:checkbox option" do
294
- @b = Forme::Form.new(@ab, :config=>:bs3, :wrapper=>:li)
295
- @b.input(:tracks, :as=>:checkbox).must_equal '<div class="one_to_many checkboxset"><label>Tracks</label><div class="checkbox"><label class="option" for="album_track_pks_1"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></div><div class="checkbox"><label class="option" for="album_track_pks_2"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></div><div class="checkbox"><label class="option" for="album_track_pks_3"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></div></div>'
296
- @b.input(:tracks, :as=>:checkbox, :wrapper=>:li).must_equal '<li class="one_to_many checkboxset"><label>Tracks</label><div class="checkbox"><label class="option" for="album_track_pks_1"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></div><div class="checkbox"><label class="option" for="album_track_pks_2"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></div><div class="checkbox"><label class="option" for="album_track_pks_3"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></div></li>'
297
- end
298
-
299
- it "should support custom wrappers for one_to_many associations with :as=>:checkbox via :tag_wrapper option" do
300
- @b = Forme::Form.new(@ab, :config=>:bs3, :wrapper=>:li)
301
- @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 checkboxset"><label>Tracks</label><span><label class="option" for="album_track_pks_1"><input checked="checked" id="album_track_pks_1" name="album[track_pks][]" type="checkbox" value="1"/> m</label></span><span><label class="option" for="album_track_pks_2"><input checked="checked" id="album_track_pks_2" name="album[track_pks][]" type="checkbox" value="2"/> n</label></span><span><label class="option" for="album_track_pks_3"><input id="album_track_pks_3" name="album[track_pks][]" type="checkbox" value="3"/> o</label></span></div>'
302
- end
303
-
304
- it "should use a text field methods not backed by columns" do
305
- @b.input(:artist_name).must_equal '<div class="form-group"><label for="album_artist_name">Artist name</label> <input class="form-control" id="album_artist_name" name="album[artist_name]" type="text" value="a"/></div>'
306
- @c.input(:artist_name).must_equal '<div class="form-group"><label for="album_artist_name">Artist name</label> <input class="form-control" id="album_artist_name" name="album[artist_name]" type="text" value="d"/></div>'
307
- end
308
-
309
- it "should handle errors on methods not backed by columns" do
310
- @ab.errors.add(:artist_name, 'foo')
311
- @b.input(:artist_name).must_equal '<div class="form-group has-error"><label for="album_artist_name">Artist name</label> <input aria-describedby="album_artist_name_error_message" aria-invalid="true" class="form-control" id="album_artist_name" name="album[artist_name]" type="text" value="a"/><span class="help-block with-errors" id="album_artist_name_error_message">foo</span></div>'
312
- end
313
-
314
- it "should respect a :type option with a schema type as the input type for methods not backed by columns" do
315
- def @ab.foo; false end
316
- @b.input(:foo, :type=>:boolean, :as=>:select).must_equal '<div class="form-group"><label for="album_foo">Foo</label> <select class="form-control" id="album_foo" name="album[foo]"><option value=""></option><option value="t">True</option><option selected="selected" value="f">False</option></select></div>'
317
- end
318
-
319
- it "should respect a :type option with an input type as the input type for methods not backed by columns" do
320
- def @ab.foo; "bar" end
321
- @b.input(:foo, :type=>:phone).must_equal '<div class="form-group"><label for="album_foo">Foo</label> <input class="form-control" id="album_foo" name="album[foo]" type="phone" value="bar"/></div>'
322
- end
323
-
324
- it "should correctly show an error message if there is one" do
325
- @ab.errors.add(:name, 'tis not valid')
326
- @b.input(:name).must_equal '<div class="form-group has-error string"><label for="album_name">Name</label> <input aria-describedby="album_name_error_message" aria-invalid="true" class="form-control" id="album_name" maxlength="255" name="album[name]" type="text" value="b"/><span class="help-block with-errors" id="album_name_error_message">tis not valid</span></div>'
327
- end
328
-
329
- it "should correctly show an error message for many_to_one associations if there is one" do
330
- @ab.errors.add(:artist_id, 'tis not valid')
331
- @b.input(:artist).must_equal '<div class="form-group has-error many_to_one"><label for="album_artist_id">Artist</label> <select aria-describedby="album_artist_id_error_message" aria-invalid="true" class="form-control" id="album_artist_id" name="album[artist_id]"><option value=""></option><option selected="selected" value="1">a</option><option value="2">d</option></select><span class="help-block with-errors" id="album_artist_id_error_message">tis not valid</span></div>'
332
- end
333
-
334
- it "should raise an error for unhandled associations" do
335
- al = Class.new(Album)
336
- al.one_to_many :tags
337
- al.association_reflection(:tags)[:type] = :foo
338
- proc{Forme::Form.new(al.new).input(:tags)}.must_raise(Sequel::Plugins::Forme::Error)
339
- end
340
-
341
- it "should raise an error for unhandled fields with no :type option" do
342
- proc{@b.input(:foo)}.must_raise(Sequel::Plugins::Forme::Error)
343
- end
344
-
345
- it "should respect a :type option with a schema type as the input type for unhandled fields" do
346
- @b.input(:foo, :type=>:string).must_equal '<div class="form-group"><label for="album_foo">Foo</label> <input class="form-control" id="album_foo" name="album[foo]" type="text"/></div>'
347
- @b.input(:password, :type=>:string).must_equal '<div class="form-group"><label for="album_password">Password</label> <input class="form-control" id="album_password" name="album[password]" type="password"/></div>'
348
- end
349
-
350
- it "should respect a :type option with an input type as the input type for unhandled fields" do
351
- @b.input(:foo, :type=>:phone).must_equal '<div class="form-group"><label for="album_foo">Foo</label> <input class="form-control" id="album_foo" name="album[foo]" type="phone"/></div>'
352
- end
353
-
354
- it "should respect a :multiple option for the name attribute for unhandled fields" do
355
- @b.input(:foo, :type=>:phone, :multiple=>true).must_equal '<div class="form-group"><label for="album_foo">Foo</label> <input class="form-control" id="album_foo" name="album[foo][]" type="phone"/></div>'
356
- end
357
-
358
- it "should add required attribute if the column doesn't support nil values" do
359
- def @ab.db_schema; h = super.dup; h[:name] = h[:name].merge(:allow_null=>false); h end
360
- @b.input(:name).must_equal '<div class="form-group required string"><label for="album_name">Name<abbr title="required">*</abbr></label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" required="required" type="text" value="b"/></div>'
361
- end
362
-
363
- it "should use allow nested forms with many_to_one associations" do
364
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></div></fieldset></form>'
365
- end
366
-
367
- it "should have #subform respect an :inputs option" do
368
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></div></fieldset></form>'
369
- end
370
-
371
- it "should have #subform respect an :obj option overriding the object to use" do
372
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="b"/></div></fieldset></form>'
373
- end
374
-
375
- it "should have #subform respect a :legend option if :inputs is used" do
376
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></div></fieldset></form>'
377
- end
378
-
379
- it "should have #subform respect a callable :legend option if :inputs is used" do
380
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></div></fieldset></form>'
381
- end
382
-
383
- it "should not add hidden primary key field for new many_to_one associated objects" do
384
- @ab.associations[:artist] = Artist.new(:name=>'a')
385
- Forme.form(@ab, {},{:config=>:bs3}){|f| f.subform(:artist){f.input(:name)}}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Artist</legend><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></div></fieldset></form>'
386
- end
387
-
388
- it "should use allow nested forms with one_to_one associations" do
389
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_album_info_attributes_info">Info</label> <input class="form-control" id="album_album_info_attributes_info" maxlength="255" name="album[album_info_attributes][info]" type="text" value="a"/></div></fieldset></form>'
390
- end
391
-
392
- it "should use allow nested forms with one_to_many associations" do
393
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></div></fieldset><fieldset class="inputs"><legend>Track #2</legend><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></div></fieldset></form>'
394
- end
395
-
396
- it "should support :obj option for *_to_many associations" do
397
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="x"/></div></fieldset><fieldset class="inputs"><legend>Track #2</legend><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="y"/></div></fieldset></form>'
398
- end
399
-
400
- it "should auto number legends when using subform with inputs for *_to_many associations" do
401
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></div></fieldset><fieldset class="inputs"><legend>Track #2</legend><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></div></fieldset></form>'
402
- end
403
-
404
- it "should support callable :legend option when using subform with inputs for *_to_many associations" do
405
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></div></fieldset><fieldset class="inputs"><legend>Track 1 (n)</legend><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></div></fieldset></form>'
406
- end
407
-
408
- it "should not add hidden primary key field for nested forms with one_to_many associations with new objects" do
409
- @ab.associations[:tracks] = [Track.new(:name=>'m'), Track.new(:name=>'n')]
410
- Forme.form(@ab, {},{:config=>:bs3}){|f| f.subform(:tracks){f.input(:name)}}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Track #1</legend><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" maxlength="255" name="album[tracks_attributes][0][name]" type="text" value="m"/></div></fieldset><fieldset class="inputs"><legend>Track #2</legend><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" maxlength="255" name="album[tracks_attributes][1][name]" type="text" value="n"/></div></fieldset></form>'
411
- end
412
-
413
- it "should use allow nested forms with many_to_many associations" do
414
- Forme.form(@ab, {},{:config=>:bs3}){|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><div class="form-group string"><label for="album_tags_attributes_0_name">Name</label> <input class="form-control" id="album_tags_attributes_0_name" maxlength="255" name="album[tags_attributes][0][name]" type="text" value="s"/></div></fieldset><fieldset class="inputs"><legend>Tag #2</legend><div class="form-group string"><label for="album_tags_attributes_1_name">Name</label> <input class="form-control" id="album_tags_attributes_1_name" maxlength="255" name="album[tags_attributes][1][name]" type="text" value="t"/></div></fieldset></form>'
415
- end
416
-
417
- it "should not add hidden primary key field for nested forms with many_to_many associations with new objects" do
418
- @ab.associations[:tags] = [Tag.new(:name=>'s'), Tag.new(:name=>'t')]
419
- Forme.form(@ab, {},{:config=>:bs3}){|f| f.subform(:tags){f.input(:name)}}.must_equal '<form class="forme album" method="post"><fieldset class="inputs"><legend>Tag #1</legend><div class="form-group string"><label for="album_tags_attributes_0_name">Name</label> <input class="form-control" id="album_tags_attributes_0_name" maxlength="255" name="album[tags_attributes][0][name]" type="text" value="s"/></div></fieldset><fieldset class="inputs"><legend>Tag #2</legend><div class="form-group string"><label for="album_tags_attributes_1_name">Name</label> <input class="form-control" id="album_tags_attributes_1_name" maxlength="255" name="album[tags_attributes][1][name]" type="text" value="t"/></div></fieldset></form>'
420
- end
421
-
422
- it "should handle multiple nested levels" do
423
- Forme.form(Artist[1], {},{:config=>:bs3}){|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><div class="form-group string"><label for="artist_albums_attributes_0_name">Name</label> <input class="form-control" id="artist_albums_attributes_0_name" maxlength="255" name="artist[albums_attributes][0][name]" type="text" value="b"/></div><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><div class="form-group string"><label for="artist_albums_attributes_0_tracks_attributes_0_name">Name</label> <input class="form-control" id="artist_albums_attributes_0_tracks_attributes_0_name" maxlength="255" name="artist[albums_attributes][0][tracks_attributes][0][name]" type="text" value="m"/></div></fieldset><fieldset class="inputs"><legend>Track #2</legend><div class="form-group string"><label for="artist_albums_attributes_0_tracks_attributes_1_name">Name</label> <input class="form-control" id="artist_albums_attributes_0_tracks_attributes_1_name" maxlength="255" name="artist[albums_attributes][0][tracks_attributes][1][name]" type="text" value="n"/></div></fieldset></fieldset></form>'
424
- end
425
-
426
- it "should have #subform :grid option create a grid" do
427
- Forme.form(@ab, {},{:config=>:bs3}){|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 class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
428
- end
429
-
430
- it "should have #subform :grid option respect :inputs_opts option to pass options to inputs" do
431
- Forme.form(@ab, {},{:config=>:bs3}){|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 class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
432
- end
433
-
434
- it "should have #subform :grid option handle :legend and :labels options" do
435
- Forme.form(@ab, {},{:config=>:bs3}){|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 class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
436
- end
437
-
438
- it "should have #subform :grid option handle :legend and :labels nil values" do
439
- Forme.form(@ab, {},{:config=>:bs3}){|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 class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
440
- end
441
-
442
- it "should have #subform :grid option handle :skip_primary_key option" do
443
- Forme.form(@ab, {},{:config=>:bs3}){|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 class="form-control" id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></tbody></table></form>'
444
- end
445
-
446
- end
447
-
448
- describe "Forme Sequel plugin default input types based on column names" do
449
- def f(name)
450
- DB.create_table!(:test){String name}
451
- Forme::Form.new(Class.new(Sequel::Model).class_eval{def self.name; 'Test' end; set_dataset :test}.new, {:config=>:bs3}).input(name, :value=>'foo')
452
- end
453
-
454
- it "should use password input with no value for string columns with name password" do
455
- f(:password).must_equal '<div class="form-group string"><label for="test_password">Password</label> <input class="form-control" id="test_password" maxlength="255" name="test[password]" type="password"/></div>'
456
- end
457
-
458
- it "should use email input for string columns with name email" do
459
- f(:email).must_equal '<div class="form-group string"><label for="test_email">Email</label> <input class="form-control" id="test_email" maxlength="255" name="test[email]" type="email" value="foo"/></div>'
460
- end
461
-
462
- it "should use tel input for string columns with name phone or fax" do
463
- f(:phone).must_equal '<div class="form-group string"><label for="test_phone">Phone</label> <input class="form-control" id="test_phone" maxlength="255" name="test[phone]" type="tel" value="foo"/></div>'
464
- f(:fax).must_equal '<div class="form-group string"><label for="test_fax">Fax</label> <input class="form-control" id="test_fax" maxlength="255" name="test[fax]" type="tel" value="foo"/></div>'
465
- end
466
-
467
- it "should use url input for string columns with name url, uri, or website" do
468
- f(:url).must_equal '<div class="form-group string"><label for="test_url">Url</label> <input class="form-control" id="test_url" maxlength="255" name="test[url]" type="url" value="foo"/></div>'
469
- f(:uri).must_equal '<div class="form-group string"><label for="test_uri">Uri</label> <input class="form-control" id="test_uri" maxlength="255" name="test[uri]" type="url" value="foo"/></div>'
470
- f(:website).must_equal '<div class="form-group string"><label for="test_website">Website</label> <input class="form-control" id="test_website" maxlength="255" name="test[website]" type="url" value="foo"/></div>'
471
- end
472
- end
473
-
474
- describe "Forme Sequel plugin default input types based on column type" do
475
- def f(type)
476
- DB.create_table!(:test){column :foo, type}
477
- Forme::Form.new(Class.new(Sequel::Model).class_eval{def self.name; 'Test' end; set_dataset :test}.new,{:config=>:bs3}).input(:foo, :value=>'foo')
478
- end
479
-
480
- it "should use password input with no value for string columns with name password" do
481
- f(File).must_equal '<div class="blob form-group"><label for="test_foo">Foo</label> <input id="test_foo" name="test[foo]" type="file"/></div>'
482
- end
483
- end
484
-
485
- describe "Forme Sequel::Model validation parsing" do
486
- def f(*a)
487
- c = Class.new(Album){def self.name; "Album"; end}
488
- c.plugin :validation_class_methods
489
- c.send(*a)
490
- Forme::Form.new(c.new, :config=>:bs3)
491
- end
492
-
493
- it "should turn format into a pattern" do
494
- f(:validates_format_of, :name, :with=>/[A-z]+/).input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" pattern="[A-z]+" type="text"/></div>'
495
- end
496
-
497
- it "should respect :title option for format" do
498
- f(:validates_format_of, :name, :with=>/[A-z]+/, :title=>'Foo').input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" pattern="[A-z]+" title="Foo" type="text"/></div>'
499
- end
500
-
501
- it "should use maxlength for length :maximum, :is, and :within" do
502
- f(:validates_length_of, :name, :maximum=>10).input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="10" name="album[name]" type="text"/></div>'
503
- f(:validates_length_of, :name, :is=>10).input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="10" name="album[name]" type="text"/></div>'
504
- f(:validates_length_of, :name, :within=>2..10).input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="10" name="album[name]" type="text"/></div>'
505
- f(:validates_length_of, :name, :within=>2...11).input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="10" name="album[name]" type="text"/></div>'
506
- end
507
-
508
- it "should turn numericality into a pattern" do
509
- f(:validates_numericality_of, :name).input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="must be a number" type="text"/></div>'
510
- end
511
-
512
- it "should turn numericality :only_integer into a pattern" do
513
- f(:validates_numericality_of, :name, :only_integer=>true).input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+$" title="must be a number" type="text"/></div>'
514
- end
515
-
516
- it "should respect :title option for numericality" do
517
- f(:validates_numericality_of, :name, :title=>'Foo').input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" pattern="^[+\-]?\d+(\.\d+)?$" title="Foo" type="text"/></div>'
518
- end
519
-
520
- it "should respect :placeholder option for any validation" do
521
- f(:validates_uniqueness_of, :name, :placeholder=>'must be unique').input(:name).must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" maxlength="255" name="album[name]" placeholder="must be unique" type="text"/></div>'
522
- end
523
- end