sensis-formtastic-rails3 1.d4e5326

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.textile +584 -0
  3. data/Rakefile +127 -0
  4. data/generators/form/USAGE +16 -0
  5. data/generators/form/form_generator.rb +120 -0
  6. data/generators/form/templates/view__form.html.erb +5 -0
  7. data/generators/form/templates/view__form.html.haml +4 -0
  8. data/generators/formtastic/formtastic_generator.rb +24 -0
  9. data/generators/formtastic/templates/formtastic.css +131 -0
  10. data/generators/formtastic/templates/formtastic.rb +54 -0
  11. data/generators/formtastic/templates/formtastic_changes.css +14 -0
  12. data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
  13. data/init.rb +5 -0
  14. data/lib/formtastic.rb +1870 -0
  15. data/lib/formtastic/i18n.rb +35 -0
  16. data/lib/formtastic/layout_helper.rb +10 -0
  17. data/lib/formtastic/railtie.rb +12 -0
  18. data/lib/formtastic/util.rb +36 -0
  19. data/lib/generators/formtastic/form/form_generator.rb +86 -0
  20. data/lib/generators/formtastic/install/install_generator.rb +24 -0
  21. data/lib/locale/en.yml +8 -0
  22. data/rails/init.rb +2 -0
  23. data/spec/buttons_spec.rb +166 -0
  24. data/spec/commit_button_spec.rb +401 -0
  25. data/spec/custom_builder_spec.rb +98 -0
  26. data/spec/defaults_spec.rb +20 -0
  27. data/spec/error_proc_spec.rb +27 -0
  28. data/spec/errors_spec.rb +105 -0
  29. data/spec/form_helper_spec.rb +142 -0
  30. data/spec/helpers/layout_helper_spec.rb +21 -0
  31. data/spec/i18n_spec.rb +152 -0
  32. data/spec/include_blank_spec.rb +74 -0
  33. data/spec/input_spec.rb +786 -0
  34. data/spec/inputs/boolean_input_spec.rb +104 -0
  35. data/spec/inputs/check_boxes_input_spec.rb +426 -0
  36. data/spec/inputs/country_input_spec.rb +118 -0
  37. data/spec/inputs/date_input_spec.rb +168 -0
  38. data/spec/inputs/datetime_input_spec.rb +310 -0
  39. data/spec/inputs/file_input_spec.rb +34 -0
  40. data/spec/inputs/hidden_input_spec.rb +78 -0
  41. data/spec/inputs/numeric_input_spec.rb +44 -0
  42. data/spec/inputs/password_input_spec.rb +46 -0
  43. data/spec/inputs/radio_input_spec.rb +243 -0
  44. data/spec/inputs/select_input_spec.rb +546 -0
  45. data/spec/inputs/string_input_spec.rb +64 -0
  46. data/spec/inputs/text_input_spec.rb +34 -0
  47. data/spec/inputs/time_input_spec.rb +206 -0
  48. data/spec/inputs/time_zone_input_spec.rb +110 -0
  49. data/spec/inputs_spec.rb +476 -0
  50. data/spec/label_spec.rb +89 -0
  51. data/spec/semantic_errors_spec.rb +98 -0
  52. data/spec/semantic_fields_for_spec.rb +45 -0
  53. data/spec/spec.opts +2 -0
  54. data/spec/spec_helper.rb +289 -0
  55. data/spec/support/custom_macros.rb +494 -0
  56. data/spec/support/output_buffer.rb +4 -0
  57. data/spec/support/test_environment.rb +45 -0
  58. metadata +234 -0
@@ -0,0 +1,546 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'select input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe 'explicit values' do
14
+ describe 'using an array of values' do
15
+ before do
16
+ @array_with_values = ["Title A", "Title B", "Title C"]
17
+ @array_with_keys_and_values = [["Title D", 1], ["Title E", 2], ["Title F", 3]]
18
+ @form = semantic_form_for(@new_post) do |builder|
19
+ concat(builder.input(:title, :as => :select, :collection => @array_with_values))
20
+ concat(builder.input(:title, :as => :select, :collection => @array_with_keys_and_values))
21
+ end
22
+ end
23
+
24
+ it 'should have a option for each key and/or value' do
25
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
26
+ @array_with_values.each do |v|
27
+ output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
28
+ end
29
+ @array_with_keys_and_values.each do |v|
30
+ output_buffer.should have_tag("form li select option[@value='#{v.second}']", /^#{v.first}$/)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe 'using a range' do
36
+ before do
37
+ @range_with_values = 1..5
38
+ @form = semantic_form_for(@new_post) do |builder|
39
+ concat(builder.input(:title, :as => :select, :collection => @range_with_values))
40
+ end
41
+ end
42
+
43
+ it 'should have an option for each value' do
44
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
45
+ @range_with_values.each do |v|
46
+ output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ describe 'for boolean columns' do
53
+ describe 'default formtastic locale' do
54
+ before do
55
+ # Note: Works, but something like Formtastic.root.join(...) would probably be "safer".
56
+ ::I18n.load_path = [File.join(File.dirname(__FILE__), *%w[.. .. lib locale en.yml])]
57
+ ::I18n.backend.send(:init_translations)
58
+
59
+ @form = semantic_form_for(@new_post) do |builder|
60
+ concat(builder.input(:published, :as => :select))
61
+ end
62
+ end
63
+
64
+ after do
65
+ ::I18n.backend.store_translations :en, {}
66
+ end
67
+
68
+ it 'should render a select with at least options: true/false' do
69
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
70
+ output_buffer.should have_tag("form li select option[@value='true']", /^Yes$/)
71
+ output_buffer.should have_tag("form li select option[@value='false']", /^No$/)
72
+ end
73
+ end
74
+
75
+ describe 'custom locale' do
76
+ before do
77
+ @boolean_select_labels = {:yes => 'Yep', :no => 'Nope'}
78
+ ::I18n.backend.store_translations :en, :formtastic => @boolean_select_labels
79
+
80
+ @form = semantic_form_for(@new_post) do |builder|
81
+ concat(builder.input(:published, :as => :select))
82
+ end
83
+ end
84
+
85
+ after do
86
+ ::I18n.backend.store_translations :en, {}
87
+ end
88
+
89
+ it 'should render a select with at least options: true/false' do
90
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
91
+ output_buffer.should have_tag("form li select option[@value='true']", /#{@boolean_select_labels[:yes]}/)
92
+ output_buffer.should have_tag("form li select option[@value='false']", /#{@boolean_select_labels[:no]}/)
93
+ end
94
+ end
95
+ end
96
+
97
+ describe 'for a belongs_to association' do
98
+ before do
99
+ @form = semantic_form_for(@new_post) do |builder|
100
+ concat(builder.input(:author, :as => :select))
101
+ concat(builder.input(:reviewer, :as => :select))
102
+ end
103
+ end
104
+
105
+ it_should_have_input_wrapper_with_class("select")
106
+ it_should_have_input_wrapper_with_id("post_author_input")
107
+ it_should_have_label_with_text(/Author/)
108
+ it_should_have_label_for('post_author_id')
109
+ it_should_apply_error_logic_for_input_type(:select)
110
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
111
+ it_should_use_the_collection_when_provided(:select, 'option')
112
+
113
+ it 'should have a select inside the wrapper' do
114
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
115
+ output_buffer.should have_tag('form li select')
116
+ output_buffer.should have_tag('form li select#post_author_id')
117
+ output_buffer.should have_tag('form li select#post_reviewer_id')
118
+ end
119
+
120
+ it 'should have a valid name' do
121
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
122
+ output_buffer.should have_tag("form li select[@name='post[author_id]']")
123
+ output_buffer.should_not have_tag("form li select[@name='post[author_id][]']")
124
+ output_buffer.should_not have_tag("form li select[@name='post[reviewer_id][]']")
125
+ end
126
+
127
+ it 'should not create a multi-select' do
128
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
129
+ output_buffer.should_not have_tag('form li select[@multiple]')
130
+ end
131
+
132
+ it 'should create a select without size' do
133
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
134
+ output_buffer.should_not have_tag('form li select[@size]')
135
+ end
136
+
137
+ it 'should have a blank option' do
138
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
139
+ output_buffer.should have_tag("form li select option[@value='']")
140
+ end
141
+
142
+ it 'should have a select option for each Author' do
143
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
144
+ output_buffer.should have_tag("form li select[@name='post[author_id]'] option", :count => ::Author.find(:all).size + 1)
145
+ ::Author.find(:all).each do |author|
146
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
147
+ end
148
+ end
149
+
150
+ it 'should have one option with a "selected" attribute' do
151
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
152
+ output_buffer.should have_tag("form li select[@name='post[author_id]'] option[@selected]", :count => 1)
153
+ end
154
+
155
+ it 'should not singularize the association name' do
156
+ @new_post.stub!(:author_status).and_return(@bob)
157
+ @new_post.stub!(:author_status_id).and_return(@bob.id)
158
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
159
+
160
+ form = semantic_form_for(@new_post) do |builder|
161
+ concat(builder.input(:author_status, :as => :select))
162
+ end
163
+
164
+ output_buffer.concat(form) if Formtastic::Util.rails3?
165
+ output_buffer.should have_tag('form li select#post_author_status_id')
166
+ end
167
+ end
168
+
169
+ describe "for a belongs_to association with :group_by => :author" do
170
+ it "should call author.posts" do
171
+ [@freds_post].each { |post| post.stub!(:to_label).and_return("Post - #{post.id}") }
172
+ @fred.should_receive(:posts)
173
+
174
+ semantic_form_for(@new_post) do |builder|
175
+ concat(builder.input(:main_post, :as => :select, :group_by => :author ) )
176
+ end
177
+ end
178
+ end
179
+
180
+ describe "for a belongs_to association with :conditions" do
181
+ before do
182
+ ::Post.stub!(:reflect_on_association).with(:author).and_return do
183
+ mock = mock('reflection', :options => {:conditions => {:active => true}}, :klass => ::Author, :macro => :belongs_to)
184
+ mock.stub!(:[]).with(:class_name).and_return("Author")
185
+ mock
186
+ end
187
+ end
188
+
189
+ it "should call author.find with association conditions" do
190
+ ::Author.should_receive(:merge_conditions).with({:active => true}, nil).and_return(:active => true)
191
+ ::Author.should_receive(:all).with(:conditions => {:active => true})
192
+
193
+ semantic_form_for(@new_post) do |builder|
194
+ concat(builder.input(:author, :as => :select))
195
+ end
196
+ end
197
+
198
+ it "should call author.find with association conditions and find_options conditions" do
199
+ ::Author.should_receive(:merge_conditions).with({:active => true}, {:publisher => true}).and_return(:active => true, :publisher => true)
200
+ ::Author.should_receive(:all).with(:conditions => {:active => true, :publisher => true})
201
+
202
+ semantic_form_for(@new_post) do |builder|
203
+ concat(builder.input(:author, :as => :select, :find_options => {:conditions => {:publisher => true}}))
204
+ end
205
+ end
206
+ end
207
+
208
+ describe 'for a belongs_to association with :group_by => :continent' do
209
+ before do
210
+ @authors = [@bob, @fred, @fred, @fred]
211
+ ::Author.stub!(:find).and_return(@authors)
212
+ @continent_names = %w(Europe Africa)
213
+ @continents = (0..1).map { |i| c = ::Continent.new; c.stub!(:id).and_return(100 - i);c }
214
+ @authors[0..1].each_with_index { |author, i| author.stub!(:continent).and_return(@continents[i]) }
215
+ ::Continent.stub!(:reflect_on_all_associations).and_return {|macro| mock('reflection', :klass => Author) if macro == :has_many}
216
+ ::Continent.stub!(:reflect_on_association).and_return {|column_name| mock('reflection', :klass => Author) if column_name == :authors}
217
+ ::Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Continent, :macro => :belongs_to) if column_name == :continent }
218
+
219
+
220
+ @continents.each_with_index do |continent, i|
221
+ continent.stub!(:to_label).and_return(@continent_names[i])
222
+ continent.stub!(:authors).and_return([@authors[i]])
223
+ end
224
+
225
+ @form = semantic_form_for(@new_post) do |builder|
226
+ concat(builder.input(:author, :as => :select, :group_by => :continent ) )
227
+ concat(builder.input(:author, :as => :select, :group_by => :continent, :group_label_method => :id ) )
228
+ end
229
+ end
230
+
231
+ it_should_have_input_wrapper_with_class("select")
232
+ it_should_have_input_wrapper_with_id("post_author_input")
233
+ it_should_have_label_with_text(/Author/)
234
+ it_should_have_label_for('post_author_id')
235
+
236
+ # TODO, need to find a way to repeat some of the specs and logic from the belongs_to specs without grouping
237
+
238
+ 0.upto(1) do |i|
239
+ it 'should have all option groups and the right values' do
240
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
241
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continent_names[i]}']", @authors[i].to_label)
242
+ end
243
+
244
+ it 'should have custom group labels' do
245
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
246
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continents[i].id}']", @authors[i].to_label)
247
+ end
248
+ end
249
+
250
+ it 'should have no duplicate groups' do
251
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
252
+ output_buffer.should have_tag('form li select optgroup', :count => 4)
253
+ end
254
+
255
+ it 'should sort the groups on the label method' do
256
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
257
+ output_buffer.should have_tag("form li select optgroup[@label='Africa']")
258
+ output_buffer.should have_tag("form li select optgroup[@label='99']")
259
+ end
260
+
261
+ it 'should call find with :include for more optimized queries' do
262
+ Author.should_receive(:all).with(:include => :continent)
263
+
264
+ semantic_form_for(@new_post) do |builder|
265
+ concat(builder.input(:author, :as => :select, :group_by => :continent ) )
266
+ end
267
+ end
268
+ end
269
+
270
+ describe 'for a has_many association' do
271
+ before do
272
+ @form = semantic_form_for(@fred) do |builder|
273
+ concat(builder.input(:posts, :as => :select))
274
+ end
275
+ end
276
+
277
+ it_should_have_input_wrapper_with_class("select")
278
+ it_should_have_input_wrapper_with_id("author_posts_input")
279
+ it_should_have_label_with_text(/Post/)
280
+ it_should_have_label_for('author_post_ids')
281
+ it_should_apply_error_logic_for_input_type(:select)
282
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
283
+ it_should_use_the_collection_when_provided(:select, 'option')
284
+
285
+ it 'should have a select inside the wrapper' do
286
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
287
+ output_buffer.should have_tag('form li select')
288
+ output_buffer.should have_tag('form li select#author_post_ids')
289
+ end
290
+
291
+ it 'should have a multi-select select' do
292
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
293
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
294
+ end
295
+
296
+ it 'should have a select option for each Post' do
297
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
298
+ output_buffer.should have_tag('form li select option', :count => ::Post.find(:all).size)
299
+ ::Post.find(:all).each do |post|
300
+ output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/)
301
+ end
302
+ end
303
+
304
+ it 'should not have a blank option' do
305
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
306
+ output_buffer.should_not have_tag("form li select option[@value='']")
307
+ end
308
+
309
+ it 'should have one option with a "selected" attribute' do
310
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
311
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
312
+ end
313
+ end
314
+
315
+ describe 'for a has_and_belongs_to_many association' do
316
+ before do
317
+ @form = semantic_form_for(@freds_post) do |builder|
318
+ concat(builder.input(:authors, :as => :select))
319
+ end
320
+ end
321
+
322
+ it_should_have_input_wrapper_with_class("select")
323
+ it_should_have_input_wrapper_with_id("post_authors_input")
324
+ it_should_have_label_with_text(/Author/)
325
+ it_should_have_label_for('post_author_ids')
326
+ it_should_apply_error_logic_for_input_type(:select)
327
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
328
+ it_should_use_the_collection_when_provided(:select, 'option')
329
+
330
+ it 'should have a select inside the wrapper' do
331
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
332
+ output_buffer.should have_tag('form li select')
333
+ output_buffer.should have_tag('form li select#post_author_ids')
334
+ end
335
+
336
+ it 'should have a multi-select select' do
337
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
338
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
339
+ end
340
+
341
+ it 'should have a select option for each Author' do
342
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
343
+ output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size)
344
+ ::Author.find(:all).each do |author|
345
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
346
+ end
347
+ end
348
+
349
+ it 'should not have a blank option' do
350
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
351
+ output_buffer.should_not have_tag("form li select option[@value='']")
352
+ end
353
+
354
+ it 'should have one option with a "selected" attribute' do
355
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
356
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
357
+ end
358
+ end
359
+
360
+ describe 'when :prompt => "choose something" is set' do
361
+ before do
362
+ @new_post.stub!(:author_id).and_return(nil)
363
+ @form = semantic_form_for(@new_post) do |builder|
364
+ concat(builder.input(:author, :as => :select, :prompt => "choose author"))
365
+ end
366
+ end
367
+
368
+ it 'should have a select with prompt' do
369
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
370
+ output_buffer.should have_tag("form li select option[@value='']", /choose author/)
371
+ end
372
+
373
+ it 'should not have a blank select option' do
374
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
375
+ output_buffer.should_not have_tag("form li select option[@value='']", "")
376
+ end
377
+ end
378
+
379
+ describe 'when no object is given' do
380
+ before(:each) do
381
+ @form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
382
+ concat(builder.input(:author, :as => :select, :collection => ::Author.find(:all)))
383
+ end
384
+ end
385
+
386
+ it 'should generate label' do
387
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
388
+ output_buffer.should have_tag('form li label', /Author/)
389
+ output_buffer.should have_tag("form li label[@for='project_author']")
390
+ end
391
+
392
+ it 'should generate select inputs' do
393
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
394
+ output_buffer.should have_tag('form li select#project_author')
395
+ output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
396
+ end
397
+
398
+ it 'should generate an option to each item' do
399
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
400
+ ::Author.find(:all).each do |author|
401
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
402
+ end
403
+ end
404
+ end
405
+
406
+ describe 'when :selected is set' do
407
+ before do
408
+ @output_buffer = ''
409
+ end
410
+
411
+ describe "no selected items" do
412
+ before do
413
+ @new_post.stub!(:author_id).and_return(nil)
414
+ with_deprecation_silenced do
415
+ @form = semantic_form_for(@new_post) do |builder|
416
+ concat(builder.input(:author, :as => :select, :selected => nil))
417
+ end
418
+ end
419
+ end
420
+
421
+ it 'should not have any selected item(s)' do
422
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
423
+ output_buffer.should_not have_tag("form li select option[@selected='selected']")
424
+ end
425
+ end
426
+
427
+ describe "single selected item" do
428
+ before do
429
+ @new_post.stub!(:author_id).and_return(nil)
430
+ with_deprecation_silenced do
431
+ @form = semantic_form_for(@new_post) do |builder|
432
+ concat(builder.input(:author, :as => :select, :selected => @bob.id))
433
+ end
434
+ end
435
+ end
436
+
437
+ it 'should have a selected item; the specified one' do
438
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
439
+ output_buffer.should have_tag("form li select option[@selected='selected']", :count => 1)
440
+ output_buffer.should have_tag("form li select option[@selected='selected']", /bob/i)
441
+ output_buffer.should have_tag("form li select option[@selected='selected'][@value='#{@bob.id}']")
442
+ end
443
+ end
444
+
445
+ describe "multiple selected items" do
446
+
447
+ describe "when :multiple => false" do
448
+ before do
449
+ @new_post.stub!(:author_ids).and_return(nil)
450
+
451
+ with_deprecation_silenced do
452
+ @form = semantic_form_for(@new_post) do |builder|
453
+ concat(builder.input(:authors, :as => :select, :selected => [@bob.id, @fred.id], :multiple => false))
454
+ end
455
+ end
456
+ end
457
+
458
+ it "should only select the first value" do
459
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
460
+ output_buffer.should have_tag("form li select option[@selected='selected']", :count => 1)
461
+ output_buffer.should have_tag("form li select:not([@multiple]) option[@selected='selected']", /bob/i)
462
+ output_buffer.should have_tag("form li select:not([@multiple]) option[@selected='selected'][@value='#{@bob.id}']")
463
+ end
464
+ end
465
+
466
+ describe "when :multiple => true" do
467
+ before do
468
+ @new_post.stub!(:author_ids).and_return(nil)
469
+
470
+ with_deprecation_silenced do
471
+ @form = semantic_form_for(@new_post) do |builder|
472
+ concat(builder.input(:authors, :as => :select, :selected => [@bob.id, @fred.id]))
473
+ end
474
+ end
475
+ end
476
+
477
+ it "should have multiple items selected; the specified ones" do
478
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
479
+ output_buffer.should have_tag("form li select option[@selected='selected']", :count => 2)
480
+ output_buffer.should have_tag("form li select[@multiple] option[@selected='selected']", /bob/i)
481
+ output_buffer.should have_tag("form li select[@multiple] option[@selected='selected'][@value='#{@bob.id}']")
482
+ output_buffer.should have_tag("form li select[@multiple] option[@selected='selected']", /fred/i)
483
+ output_buffer.should have_tag("form li select[@multiple] option[@selected='selected'][@value='#{@fred.id}']")
484
+ end
485
+ end
486
+
487
+ end
488
+
489
+ end
490
+
491
+ describe "enum" do
492
+ before do
493
+ @output_buffer = ''
494
+ @some_meta_descriptions = ["One", "Two", "Three"]
495
+ @new_post.stub!(:meta_description).any_number_of_times
496
+ end
497
+
498
+ describe ":as is not set" do
499
+ before do
500
+ @form_new_post = semantic_form_for(@new_post) do |builder|
501
+ concat(builder.input(:meta_description, :collection => @some_meta_descriptions))
502
+ end
503
+ @form_project = semantic_form_for(:project, :url => 'http://test.host') do |builder|
504
+ concat(builder.input(:meta_description, :collection => @some_meta_descriptions))
505
+ end
506
+
507
+ end
508
+
509
+ it "should render a select field" do
510
+ output_buffer.concat(@form_new_post) if Formtastic::Util.rails3?
511
+ output_buffer.concat(@form_project) if Formtastic::Util.rails3?
512
+ output_buffer.should have_tag("form li select", :count => 2)
513
+ end
514
+ end
515
+
516
+ describe ":as is set" do
517
+ before do
518
+ # Should not be a case, but just checking :as got highest priority in setting input type.
519
+ @form_new_post = semantic_form_for(@new_post) do |builder|
520
+ concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions))
521
+ end
522
+ @form_project = semantic_form_for(:project, :url => 'http://test.host') do |builder|
523
+ concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions))
524
+ end
525
+ end
526
+
527
+ it "should render a text field" do
528
+ output_buffer.concat(@form_new_post) if Formtastic::Util.rails3?
529
+ output_buffer.concat(@form_project) if Formtastic::Util.rails3?
530
+ output_buffer.should have_tag("form li input[@type='text']", :count => 2)
531
+ end
532
+ end
533
+ end
534
+
535
+ it 'should warn about :selected deprecation' do
536
+ with_deprecation_silenced do
537
+ ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times
538
+ semantic_form_for(@new_post) do |builder|
539
+ concat(builder.input(:author_id, :as => :select, :selected => @bob.id))
540
+ end
541
+ end
542
+ end
543
+
544
+
545
+
546
+ end