formtastic 1.0.1 → 1.1.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README.textile +2 -2
  2. data/Rakefile +33 -8
  3. data/generators/formtastic/templates/formtastic.css +3 -18
  4. data/init.rb +5 -0
  5. data/lib/formtastic.rb +180 -82
  6. data/lib/formtastic/i18n.rb +8 -9
  7. data/lib/formtastic/layout_helper.rb +0 -1
  8. data/lib/formtastic/railtie.rb +12 -0
  9. data/lib/formtastic/util.rb +7 -0
  10. data/lib/generators/formtastic/form/form_generator.rb +86 -0
  11. data/lib/generators/formtastic/install/install_generator.rb +24 -0
  12. data/rails/init.rb +1 -6
  13. data/spec/buttons_spec.rb +25 -8
  14. data/spec/commit_button_spec.rb +67 -29
  15. data/spec/custom_builder_spec.rb +40 -4
  16. data/spec/defaults_spec.rb +1 -1
  17. data/spec/error_proc_spec.rb +1 -1
  18. data/spec/errors_spec.rb +3 -2
  19. data/spec/form_helper_spec.rb +33 -17
  20. data/spec/helpers/layout_helper_spec.rb +21 -0
  21. data/spec/i18n_spec.rb +13 -9
  22. data/spec/include_blank_spec.rb +9 -5
  23. data/spec/input_spec.rb +188 -48
  24. data/spec/inputs/boolean_input_spec.rb +14 -7
  25. data/spec/inputs/check_boxes_input_spec.rb +150 -20
  26. data/spec/inputs/country_input_spec.rb +9 -5
  27. data/spec/inputs/date_input_spec.rb +21 -9
  28. data/spec/inputs/datetime_input_spec.rb +33 -14
  29. data/spec/inputs/file_input_spec.rb +4 -3
  30. data/spec/inputs/hidden_input_spec.rb +11 -4
  31. data/spec/inputs/numeric_input_spec.rb +3 -3
  32. data/spec/inputs/password_input_spec.rb +3 -3
  33. data/spec/inputs/radio_input_spec.rb +29 -10
  34. data/spec/inputs/select_input_spec.rb +66 -26
  35. data/spec/inputs/string_input_spec.rb +5 -4
  36. data/spec/inputs/text_input_spec.rb +4 -3
  37. data/spec/inputs/time_input_spec.rb +26 -10
  38. data/spec/inputs/time_zone_input_spec.rb +11 -5
  39. data/spec/inputs_spec.rb +103 -39
  40. data/spec/label_spec.rb +5 -3
  41. data/spec/semantic_errors_spec.rb +7 -7
  42. data/spec/semantic_fields_for_spec.rb +5 -4
  43. data/spec/spec_helper.rb +86 -38
  44. data/spec/{custom_macros.rb → support/custom_macros.rb} +69 -35
  45. data/spec/support/output_buffer.rb +4 -0
  46. data/spec/support/test_environment.rb +45 -0
  47. metadata +26 -28
  48. data/spec/layout_helper_spec.rb +0 -29
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'boolean input' do
5
5
 
@@ -9,16 +9,17 @@ describe 'boolean input' do
9
9
  @output_buffer = ''
10
10
  mock_everything
11
11
 
12
- semantic_form_for(@new_post) do |builder|
12
+ @form = semantic_form_for(@new_post) do |builder|
13
13
  concat(builder.input(:allow_comments, :as => :boolean))
14
14
  end
15
15
  end
16
-
16
+
17
17
  it_should_have_input_wrapper_with_class("boolean")
18
18
  it_should_have_input_wrapper_with_id("post_allow_comments_input")
19
19
  it_should_apply_error_logic_for_input_type(:boolean)
20
20
 
21
21
  it 'should generate a label containing the input' do
22
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
22
23
  output_buffer.should have_tag('form li label', :count => 1)
23
24
  output_buffer.should have_tag('form li label[@for="post_allow_comments"]')
24
25
  output_buffer.should have_tag('form li label', /Allow comments/)
@@ -26,6 +27,7 @@ describe 'boolean input' do
26
27
  end
27
28
 
28
29
  it 'should generate a checkbox input' do
30
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
29
31
  output_buffer.should have_tag('form li label input')
30
32
  output_buffer.should have_tag('form li label input#post_allow_comments')
31
33
  output_buffer.should have_tag('form li label input[@type="checkbox"]')
@@ -34,19 +36,22 @@ describe 'boolean input' do
34
36
  end
35
37
 
36
38
  it 'should allow checked and unchecked values to be sent' do
37
- semantic_form_for(@new_post) do |builder|
39
+ form = semantic_form_for(@new_post) do |builder|
38
40
  concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked'))
39
41
  end
40
42
 
43
+ output_buffer.concat(form) if Formtastic::Util.rails3?
41
44
  output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]:not([@unchecked_value][@checked_value])')
42
45
  output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
43
46
  end
44
47
 
45
48
  it 'should generate a label and a checkbox even if no object is given' do
46
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
49
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
47
50
  concat(builder.input(:allow_comments, :as => :boolean))
48
51
  end
49
52
 
53
+ output_buffer.concat(form) if Formtastic::Util.rails3?
54
+
50
55
  output_buffer.should have_tag('form li label[@for="project_allow_comments"]')
51
56
  output_buffer.should have_tag('form li label', /Allow comments/)
52
57
  output_buffer.should have_tag('form li label input[@type="checkbox"]')
@@ -66,13 +71,14 @@ describe 'boolean input' do
66
71
  @new_post.stub!(:allow_comments).and_return(true)
67
72
 
68
73
  with_deprecation_silenced do
69
- semantic_form_for(@new_post) do |builder|
74
+ @form = semantic_form_for(@new_post) do |builder|
70
75
  concat(builder.input(:allow_comments, :as => :boolean, :selected => false))
71
76
  end
72
77
  end
73
78
  end
74
79
 
75
80
  it 'should not be selected' do
81
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
76
82
  output_buffer.should_not have_tag("form li label input[@type='checkbox'][@checked='checked']")
77
83
  end
78
84
  end
@@ -82,13 +88,14 @@ describe 'boolean input' do
82
88
  @new_post.stub!(:allow_comments).and_return(false)
83
89
 
84
90
  with_deprecation_silenced do
85
- semantic_form_for(@new_post) do |builder|
91
+ @form = semantic_form_for(@new_post) do |builder|
86
92
  concat(builder.input(:allow_comments, :as => :boolean, :selected => true))
87
93
  end
88
94
  end
89
95
  end
90
96
 
91
97
  it 'should be selected' do
98
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
92
99
  output_buffer.should have_tag("form li label input[@type='checkbox'][@checked='checked']")
93
100
  end
94
101
  end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
- require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'spec_helper'
3
3
 
4
4
  describe 'check_boxes input' do
5
5
 
@@ -10,7 +10,7 @@ describe 'check_boxes input' do
10
10
  @output_buffer = ''
11
11
  mock_everything
12
12
 
13
- semantic_form_for(@fred) do |builder|
13
+ @form = semantic_form_for(@fred) do |builder|
14
14
  concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true))
15
15
  end
16
16
  end
@@ -23,31 +23,37 @@ describe 'check_boxes input' do
23
23
  it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
24
24
 
25
25
  it 'should generate a legend containing a label with text for the input' do
26
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
26
27
  output_buffer.should have_tag('form li fieldset legend.label label')
27
28
  output_buffer.should have_tag('form li fieldset legend.label label', /Posts/)
28
29
  end
29
30
 
30
31
  it 'should not link the label within the legend to any input' do
32
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
31
33
  output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
32
34
  end
33
35
 
34
36
 
35
37
  it 'should generate an ordered list with a list item for each choice' do
38
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
36
39
  output_buffer.should have_tag('form li fieldset ol')
37
- output_buffer.should have_tag('form li fieldset ol li', :count => ::Post.find(:all).size)
40
+ output_buffer.should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Post.find(:all).size)
38
41
  end
39
42
 
40
43
  it 'should have one option with a "checked" attribute' do
44
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
41
45
  output_buffer.should have_tag('form li input[@checked]', :count => 1)
42
46
  end
43
47
 
44
- it 'should generate hidden inputs with default value blank' do
45
- output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
48
+ it 'should not generate hidden inputs with default value blank' do
49
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
50
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
46
51
  end
47
52
 
48
53
  describe "each choice" do
49
54
 
50
55
  it 'should contain a label for the radio input with a nested input and label text' do
56
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
51
57
  ::Post.find(:all).each do |post|
52
58
  output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
53
59
  output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
@@ -55,20 +61,53 @@ describe 'check_boxes input' do
55
61
  end
56
62
 
57
63
  it 'should use values as li.class when value_as_class is true' do
64
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
58
65
  ::Post.find(:all).each do |post|
59
66
  output_buffer.should have_tag("form li fieldset ol li.post_#{post.id} label")
60
67
  end
61
68
  end
62
69
 
63
- it 'should have a checkbox input for each post' do
70
+ it 'should have a checkbox input but no hidden field for each post' do
71
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
72
+ ::Post.find(:all).each do |post|
73
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
74
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 1)
75
+ end
76
+ end
77
+
78
+ it 'should have a hidden field with an empty array value for the collection to allow clearing of all checkboxes' do
79
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
80
+ output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
81
+ end
82
+
83
+ it 'the hidden field with an empty array value should be followed by the ol' do
84
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
85
+ output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
86
+ end
87
+
88
+ it 'should not have a hidden field with an empty string value for the collection' do
89
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
90
+ output_buffer.should_not have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
91
+ end
92
+
93
+ it 'should have a checkbox and a hidden field for each post with :hidden_field => true' do
94
+ output_buffer.replace ''
95
+
96
+ form = semantic_form_for(@fred) do |builder|
97
+ concat(builder.input(:posts, :as => :check_boxes, :hidden_fields => true, :value_as_class => true))
98
+ end
99
+ output_buffer.concat(form) if Formtastic::Util.rails3?
100
+
64
101
  ::Post.find(:all).each do |post|
65
102
  output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
66
103
  output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
67
104
  end
105
+
68
106
  end
69
107
 
70
108
  it "should mark input as checked if it's the the existing choice" do
71
109
  ::Post.find(:all).include?(@fred.posts.first).should be_true
110
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
72
111
  output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
73
112
  end
74
113
  end
@@ -76,20 +115,23 @@ describe 'check_boxes input' do
76
115
  describe 'and no object is given' do
77
116
  before(:each) do
78
117
  output_buffer.replace ''
79
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
118
+ @form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
80
119
  concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all)))
81
120
  end
82
121
  end
83
122
 
84
123
  it 'should generate a fieldset with legend' do
124
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
85
125
  output_buffer.should have_tag('form li fieldset legend', /Author/)
86
126
  end
87
127
 
88
128
  it 'shold generate an li tag for each item in the collection' do
89
- output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
129
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
130
+ output_buffer.should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Author.find(:all).size)
90
131
  end
91
132
 
92
133
  it 'should generate labels for each item' do
134
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
93
135
  ::Author.find(:all).each do |author|
94
136
  output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
95
137
  output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
@@ -97,6 +139,7 @@ describe 'check_boxes input' do
97
139
  end
98
140
 
99
141
  it 'should generate inputs for each item' do
142
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
100
143
  ::Author.find(:all).each do |author|
101
144
  output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
102
145
  output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
@@ -106,16 +149,44 @@ describe 'check_boxes input' do
106
149
  end
107
150
 
108
151
  it 'should html escape the label string' do
109
- output_buffer.replace ''
110
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
152
+ form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
111
153
  concat(builder.input(:author_id, :as => :check_boxes, :collection => [["<b>Item 1</b>", 1], ["<b>Item 2</b>", 2]]))
112
154
  end
155
+ output_buffer.concat(form) if Formtastic::Util.rails3?
113
156
  output_buffer.should have_tag('form li fieldset ol li label') do |label|
114
157
  label.body.should match /&lt;b&gt;Item [12]&lt;\/b&gt;$/
115
158
  end
116
159
  end
117
160
  end
118
161
 
162
+ describe 'when :hidden_fields is set to false' do
163
+ before do
164
+ @output_buffer = ''
165
+ mock_everything
166
+
167
+ form = semantic_form_for(@fred) do |builder|
168
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true, :hidden_fields => false))
169
+ end
170
+ output_buffer.concat(form) if Formtastic::Util.rails3?
171
+ end
172
+
173
+ it 'should have a checkbox input for each post' do
174
+ ::Post.find(:all).each do |post|
175
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
176
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.find(:all).length)
177
+ end
178
+ end
179
+
180
+ it "should mark input as checked if it's the the existing choice" do
181
+ ::Post.find(:all).include?(@fred.posts.first).should be_true
182
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
183
+ end
184
+
185
+ it 'should not generate empty hidden inputs' do
186
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).length)
187
+ end
188
+ end
189
+
119
190
  describe 'when :selected is set' do
120
191
  before do
121
192
  @output_buffer = ''
@@ -126,13 +197,14 @@ describe 'check_boxes input' do
126
197
  @new_post.stub!(:author_ids).and_return(nil)
127
198
 
128
199
  with_deprecation_silenced do
129
- semantic_form_for(@new_post) do |builder|
200
+ @form = semantic_form_for(@new_post) do |builder|
130
201
  concat(builder.input(:authors, :as => :check_boxes, :selected => nil))
131
202
  end
132
203
  end
133
204
  end
134
205
 
135
206
  it 'should not have any selected item(s)' do
207
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
136
208
  output_buffer.should_not have_tag("form li fieldset ol li label input[@checked='checked']")
137
209
  end
138
210
  end
@@ -142,13 +214,14 @@ describe 'check_boxes input' do
142
214
  @new_post.stub!(:author_ids).and_return(nil)
143
215
 
144
216
  with_deprecation_silenced do
145
- semantic_form_for(@new_post) do |builder|
217
+ @form = semantic_form_for(@new_post) do |builder|
146
218
  concat(builder.input(:authors, :as => :check_boxes, :selected => @fred.id))
147
219
  end
148
220
  end
149
221
  end
150
222
 
151
223
  it "should have one item selected; the specified one" do
224
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
152
225
  output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 1)
153
226
  output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
154
227
  output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@fred.id}']")
@@ -160,13 +233,14 @@ describe 'check_boxes input' do
160
233
  @new_post.stub!(:author_ids).and_return(nil)
161
234
 
162
235
  with_deprecation_silenced do
163
- semantic_form_for(@new_post) do |builder|
236
+ @form = semantic_form_for(@new_post) do |builder|
164
237
  concat(builder.input(:authors, :as => :check_boxes, :selected => [@bob.id, @fred.id]))
165
238
  end
166
239
  end
167
240
  end
168
241
 
169
242
  it "should have multiple items selected; the specified ones" do
243
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
170
244
  output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 2)
171
245
  output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
172
246
  output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@bob.id}']")
@@ -196,12 +270,13 @@ describe 'check_boxes input' do
196
270
  before do
197
271
  @new_post.stub!(:author_ids).and_return(nil)
198
272
 
199
- semantic_form_for(@new_post) do |builder|
273
+ @form = semantic_form_for(@new_post) do |builder|
200
274
  concat(builder.input(:authors, :as => :check_boxes, :disabled => nil))
201
275
  end
202
276
  end
203
277
 
204
278
  it 'should not have any disabled item(s)' do
279
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
205
280
  output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
206
281
  end
207
282
  end
@@ -210,12 +285,13 @@ describe 'check_boxes input' do
210
285
  before do
211
286
  @new_post.stub!(:author_ids).and_return(nil)
212
287
 
213
- semantic_form_for(@new_post) do |builder|
288
+ @form = semantic_form_for(@new_post) do |builder|
214
289
  concat(builder.input(:authors, :as => :check_boxes, :disabled => @fred.id))
215
290
  end
216
291
  end
217
292
 
218
293
  it "should have one item disabled; the specified one" do
294
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
219
295
  output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
220
296
  output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
221
297
  output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
@@ -226,12 +302,13 @@ describe 'check_boxes input' do
226
302
  before do
227
303
  @new_post.stub!(:author_ids).and_return(nil)
228
304
 
229
- semantic_form_for(@new_post) do |builder|
305
+ @form = semantic_form_for(@new_post) do |builder|
230
306
  concat(builder.input(:authors, :as => :check_boxes, :disabled => [@bob.id, @fred.id]))
231
307
  end
232
308
  end
233
309
 
234
310
  it "should have multiple items disabled; the specified ones" do
311
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
235
312
  output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
236
313
  output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
237
314
  output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
@@ -249,7 +326,7 @@ describe 'check_boxes input' do
249
326
  Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
250
327
 
251
328
  @new_post.stub!(:author_ids).and_return(nil)
252
- semantic_form_for(@new_post) do |builder|
329
+ @form = semantic_form_for(@new_post) do |builder|
253
330
  concat(builder.input(:authors, :as => :check_boxes))
254
331
  end
255
332
  end
@@ -260,6 +337,7 @@ describe 'check_boxes input' do
260
337
  end
261
338
 
262
339
  it "should do foo" do
340
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
263
341
  output_buffer.should have_tag("legend.label label", /Translated/)
264
342
  end
265
343
 
@@ -268,12 +346,13 @@ describe 'check_boxes input' do
268
346
  describe "when :label option is set" do
269
347
  before do
270
348
  @new_post.stub!(:author_ids).and_return(nil)
271
- semantic_form_for(@new_post) do |builder|
349
+ @form = semantic_form_for(@new_post) do |builder|
272
350
  concat(builder.input(:authors, :as => :check_boxes, :label => 'The authors'))
273
351
  end
274
352
  end
275
353
 
276
354
  it "should output the correct label title" do
355
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
277
356
  output_buffer.should have_tag("legend.label label", /The authors/)
278
357
  end
279
358
  end
@@ -282,15 +361,66 @@ describe 'check_boxes input' do
282
361
  before do
283
362
  @output_buffer = ''
284
363
  @new_post.stub!(:author_ids).and_return(nil)
285
- semantic_form_for(@new_post) do |builder|
286
- concat(builder.input(:authors, :as => :check_boxes, :label => false, :morton => true))
364
+ @form = semantic_form_for(@new_post) do |builder|
365
+ concat(builder.input(:authors, :as => :check_boxes, :label => false))
287
366
  end
288
367
  end
289
368
 
290
369
  it "should not output the legend" do
370
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
291
371
  output_buffer.should_not have_tag("legend.label")
292
372
  end
293
373
  end
294
374
  end
375
+
376
+ describe 'for a has_and_belongs_to_many association' do
377
+
378
+ before do
379
+ @output_buffer = ''
380
+ mock_everything
381
+
382
+ @form = semantic_form_for(@freds_post) do |builder|
383
+ concat(builder.input(:authors, :as => :check_boxes))
384
+ end
385
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
386
+ end
387
+
388
+ it 'should render checkboxes' do
389
+ # I'm aware these two lines test the same thing
390
+ output_buffer.should have_tag('input[type="checkbox"]', :count => 2)
391
+ output_buffer.should have_tag('input[type="checkbox"]', :count => ::Author.find(:all).size)
392
+ end
393
+
394
+ it 'should only select checkboxes that are present in the association' do
395
+ # I'm aware these two lines test the same thing
396
+ output_buffer.should have_tag('input[checked="checked"]', :count => 1)
397
+ output_buffer.should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
398
+ end
399
+
400
+ end
401
+
402
+ describe 'for an association when a :collection is provided' do
403
+ describe 'it should use the specified :value_method option' do
404
+ before do
405
+ @output_buffer = ''
406
+ mock_everything
407
+ end
408
+
409
+ it 'to set the right input value' do
410
+ item = mock('item')
411
+ item.should_not_receive(:id)
412
+ item.stub!(:custom_value).and_return('custom_value')
413
+ item.should_receive(:custom_value).exactly(3).times
414
+ @new_post.author.should_receive(:custom_value)
415
+ @form = semantic_form_for(@new_post) do |builder|
416
+ concat(builder.input(:author, :as => :check_boxes, :value_method => :custom_value, :collection => [item, item, item]))
417
+ end
418
+
419
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
420
+ output_buffer.should have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
421
+ end
422
+ end
423
+ end
424
+
295
425
  end
296
426