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
@@ -0,0 +1,523 @@
|
|
1
|
+
require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb')
|
2
|
+
require File.join(File.dirname(File.expand_path(__FILE__)), 'sequel_helper.rb')
|
3
|
+
require '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.to_s.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).to_s.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').to_s.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]).to_s.must_equal '<form class="foo bar forme album" method="post"></form>'
|
30
|
+
@b.form(:class=>[:foo, [:bar, :baz]]).to_s.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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" name="album[name]" type="text" value="b"/></div>'
|
35
|
+
@c.input(:name).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" 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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" name="album[name]">b</textarea></div>'
|
40
|
+
@c.input(:name, :as=>:textarea).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" 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).to_s.must_equal '<div class="form-group"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" name="album[name]">b</textarea></div>'
|
45
|
+
@c.input(:name, :type=>:textarea).to_s.must_equal '<div class="form-group"><label for="album_name">Name</label> <textarea class="form-control" id="album_name" name="album[name]">c</textarea></div>'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should use number inputs for integers" do
|
49
|
+
@b.input(:copies_sold).to_s.must_equal '<div class="form-group integer"><label for="album_copies_sold">Copies sold</label> <input class="form-control" id="album_copies_sold" name="album[copies_sold]" type="number" value="10"/></div>'
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should use date inputs for Dates" do
|
53
|
+
@b.input(:release_date).to_s.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).to_s.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.000000"/></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).to_s.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.000000"/></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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" name="album[name]" type="text" value="b"/></div>'
|
69
|
+
f.input(:release_date).to_s.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).to_s.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.000000"/></div>'
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should include required * in label if required" do
|
74
|
+
@b.input(:name, :required=>true).to_s.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" 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').to_s.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" 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).to_s.must_equal '<li class="string required"><label for="album_name">Name<abbr title="required">*</abbr></label> <input class="form-control" id="album_name" 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).to_s.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).to_s.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").to_s.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").to_s.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').to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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 />')).to_s.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 />').to_s.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>'
|
135
|
+
@b.input(:platinum, :as=>:radio, :label=>'Foo:<br />', :wrapper=>nil).to_s.must_equal '<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>'
|
136
|
+
@b.input(:platinum, :as=>:radio, :label=>'Foo:<br />', :wrapper=>:li).to_s.must_equal '<li 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></li>'
|
137
|
+
@b.input(:platinum, :as=>:radio, :label=>'Foo:<br />', :wrapper=>:bs3).to_s.must_equal '<div class="boolean form-group 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>'
|
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").to_s.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).to_s.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).to_s.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).to_s.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").to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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 />')).to_s.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 />').to_s.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>'
|
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).to_s.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).to_s.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).to_s.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])}).to_s.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]}).to_s.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]}).to_s.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).to_s.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).to_s.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}).to_s.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}).to_s.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)).to_s.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)).to_s.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)}).to_s.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)}).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.must_equal '<div class="form-group has-error 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><span class="help-block with-errors">tis not valid</span></div>'
|
267
|
+
@b.input(:atags, :as=>:checkbox).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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 />').to_s.must_equal '<div class="one_to_many checkboxset"><label>Foo<br /></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).to_s.must_equal '<div class="one_to_many checkboxset"><label>Foo<br /></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).to_s.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).to_s.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])}).to_s.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).to_s.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).to_s.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).to_s.must_equal '<div class="form-group has-error"><label for="album_artist_name">Artist name</label> <input class="form-control" id="album_artist_name" name="album[artist_name]" type="text" value="a"/><span class="help-block with-errors">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).to_s.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).to_s.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).to_s.must_equal '<div class="form-group has-error string"><label for="album_name">Name</label> <input class="form-control" id="album_name" name="album[name]" type="text" value="b"/><span class="help-block with-errors">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).to_s.must_equal '<div class="form-group has-error 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><span class="help-block with-errors">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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.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" 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)}}.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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" 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])}.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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" 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'))}.to_s.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" 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')}.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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" 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}"})}.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><div class="form-group string"><label for="album_artist_attributes_name">Name</label> <input class="form-control" id="album_artist_attributes_name" 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)}}.to_s.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" 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)}}.to_s.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" 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)}}.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><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" name="album[tracks_attributes][0][name]" type="text" value="m"/></div></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><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" 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)}}.to_s.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" name="album[tracks_attributes][0][name]" type="text" value="x"/></div></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><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" 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])}.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><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" name="album[tracks_attributes][0][name]" type="text" value="m"/></div></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><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" 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})"})}.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><div class="form-group string"><label for="album_tracks_attributes_0_name">Name</label> <input class="form-control" id="album_tracks_attributes_0_name" name="album[tracks_attributes][0][name]" type="text" value="m"/></div></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><div class="form-group string"><label for="album_tracks_attributes_1_name">Name</label> <input class="form-control" id="album_tracks_attributes_1_name" 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)}}.to_s.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" 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" 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)}}.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><div class="form-group string"><label for="album_tags_attributes_0_name">Name</label> <input class="form-control" id="album_tags_attributes_0_name" name="album[tags_attributes][0][name]" type="text" value="s"/></div></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><div class="form-group string"><label for="album_tags_attributes_1_name">Name</label> <input class="form-control" id="album_tags_attributes_1_name" 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)}}.to_s.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" 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" 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.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" 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"/><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" name="artist[albums_attributes][0][tracks_attributes][0][name]" type="text" value="m"/></div></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><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" 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)}.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 class="form-control" id="album_artist_attributes_name" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></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'}})}.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 class="form-control" id="album_artist_attributes_name" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></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')}.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 class="form-control" id="album_artist_attributes_name" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></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)}.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 class="form-control" id="album_artist_attributes_name" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></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)}.to_s.must_equal '<form class="forme album" method="post"><table><caption>Artist</caption><tr><th>Name</th></tr><tr><td class="string"><input class="form-control" id="album_artist_attributes_name" name="album[artist_attributes][name]" type="text" value="a"/></td></tr></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).to_s.must_equal '<div class="form-group string"><label for="test_password">Password</label> <input class="form-control" id="test_password" 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).to_s.must_equal '<div class="form-group string"><label for="test_email">Email</label> <input class="form-control" id="test_email" 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).to_s.must_equal '<div class="form-group string"><label for="test_phone">Phone</label> <input class="form-control" id="test_phone" name="test[phone]" type="tel" value="foo"/></div>'
|
464
|
+
f(:fax).to_s.must_equal '<div class="form-group string"><label for="test_fax">Fax</label> <input class="form-control" id="test_fax" 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).to_s.must_equal '<div class="form-group string"><label for="test_url">Url</label> <input class="form-control" id="test_url" name="test[url]" type="url" value="foo"/></div>'
|
469
|
+
f(:uri).to_s.must_equal '<div class="form-group string"><label for="test_uri">Uri</label> <input class="form-control" id="test_uri" name="test[uri]" type="url" value="foo"/></div>'
|
470
|
+
f(:website).to_s.must_equal '<div class="form-group string"><label for="test_website">Website</label> <input class="form-control" id="test_website" 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).to_s.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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" 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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" 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).to_s.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).to_s.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).to_s.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).to_s.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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" 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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" 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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" 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).to_s.must_equal '<div class="form-group string"><label for="album_name">Name</label> <input class="form-control" id="album_name" name="album[name]" placeholder="must be unique" type="text"/></div>'
|
522
|
+
end
|
523
|
+
end
|