jgdavey-formtastic 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3051 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'formtastic'
3
+
4
+ module FormtasticSpecHelper
5
+ def default_input_type(column_type, column_name = :generic_column_name)
6
+ @new_post.stub!(column_name)
7
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => column_type)) unless column_type.nil?
8
+
9
+ semantic_form_for(@new_post) do |builder|
10
+ @default_type = builder.send(:default_input_type, column_name)
11
+ end
12
+
13
+ return @default_type
14
+ end
15
+ end
16
+
17
+ describe 'Formtastic' do
18
+
19
+ include ActionView::Helpers::FormHelper
20
+ include ActionView::Helpers::FormTagHelper
21
+ include ActionView::Helpers::FormOptionsHelper
22
+ include ActionView::Helpers::UrlHelper
23
+ include ActionView::Helpers::TagHelper
24
+ include ActionView::Helpers::TextHelper
25
+ include ActionView::Helpers::ActiveRecordHelper
26
+ include ActionView::Helpers::RecordIdentificationHelper
27
+ include ActionView::Helpers::DateHelper
28
+ include ActionView::Helpers::CaptureHelper
29
+ include ActiveSupport
30
+ include ActionController::PolymorphicRoutes
31
+
32
+ include Formtastic::SemanticFormHelper
33
+
34
+ attr_accessor :output_buffer
35
+
36
+ def protect_against_forgery?; false; end
37
+
38
+ before do
39
+ Formtastic::SemanticFormBuilder.label_str_method = :humanize
40
+
41
+ @output_buffer = ''
42
+
43
+ # Resource-oriented styles like form_for(@post) will expect a path method for the object,
44
+ # so we're defining some here.
45
+ def post_path(o); "/posts/1"; end
46
+ def posts_path; "/posts"; end
47
+ def new_post_path; "/posts/new"; end
48
+
49
+ def author_path(o); "/authors/1"; end
50
+ def authors_path; "/authors"; end
51
+ def new_author_path; "/authors/new"; end
52
+
53
+ # Sometimes we need some classes
54
+ class Post;
55
+ def id; end
56
+ end
57
+ class Author; end
58
+
59
+ @fred = mock('user')
60
+ @fred.stub!(:class).and_return(Author)
61
+ @fred.stub!(:to_label).and_return('Fred Smith')
62
+ @fred.stub!(:login).and_return('fred_smith')
63
+ @fred.stub!(:id).and_return(37)
64
+ @fred.stub!(:new_record?).and_return(false)
65
+ @fred.stub!(:errors).and_return(mock('errors', :on => nil))
66
+
67
+ @bob = mock('user')
68
+ @bob.stub!(:class).and_return(Author)
69
+ @bob.stub!(:to_label).and_return('Bob Rock')
70
+ @bob.stub!(:login).and_return('bob')
71
+ @bob.stub!(:id).and_return(42)
72
+ @bob.stub!(:posts).and_return([])
73
+ @bob.stub!(:post_ids).and_return([])
74
+ @bob.stub!(:new_record?).and_return(false)
75
+ @bob.stub!(:errors).and_return(mock('errors', :on => nil))
76
+
77
+ Author.stub!(:find).and_return([@fred, @bob])
78
+ Author.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize }
79
+ Author.stub!(:human_name).and_return('Author')
80
+ Author.stub!(:reflect_on_all_validations).and_return([])
81
+ Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Post, :macro => :has_many) if column_name == :posts }
82
+
83
+ # Sometimes we need a mock @post object and some Authors for belongs_to
84
+ @new_post = mock('post')
85
+ @new_post.stub!(:class).and_return(Post)
86
+ @new_post.stub!(:id).and_return(nil)
87
+ @new_post.stub!(:new_record?).and_return(true)
88
+ @new_post.stub!(:errors).and_return(mock('errors', :on => nil))
89
+ @new_post.stub!(:author).and_return(nil)
90
+
91
+ @freds_post = mock('post')
92
+ @freds_post.stub!(:class).and_return(Post)
93
+ @freds_post.stub!(:to_label).and_return('Fred Smith')
94
+ @freds_post.stub!(:id).and_return(19)
95
+ @freds_post.stub!(:author).and_return(@fred)
96
+ @freds_post.stub!(:author_id).and_return(@fred.id)
97
+ @freds_post.stub!(:authors).and_return([@fred])
98
+ @freds_post.stub!(:author_ids).and_return([@fred.id])
99
+ @freds_post.stub!(:new_record?).and_return(false)
100
+ @freds_post.stub!(:errors).and_return(mock('errors', :on => nil))
101
+ @fred.stub!(:posts).and_return([@freds_post])
102
+ @fred.stub!(:post_ids).and_return([@freds_post.id])
103
+
104
+ Post.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize }
105
+ Post.stub!(:human_name).and_return('Post')
106
+ Post.stub!(:reflect_on_all_validations).and_return([])
107
+ Post.stub!(:reflect_on_association).and_return do |column_name|
108
+ case column_name
109
+ when :author, :author_status
110
+ mock('reflection', :options => {}, :klass => Author, :macro => :belongs_to)
111
+ when :authors
112
+ mock('reflection', :options => {}, :klass => Author, :macro => :has_and_belongs_to_many)
113
+ end
114
+ end
115
+ Post.stub!(:find).and_return([@freds_post])
116
+ end
117
+
118
+ describe 'JustinFrench::Formtastic::SemanticFormBuilder' do
119
+ require 'justin_french/formtastic'
120
+ it 'should be deprecated' do
121
+ ::ActiveSupport::Deprecation.should_receive(:warn).with(/JustinFrench\:\:Formtastic\:\:SemanticFormBuilder/, anything())
122
+ form_for(@new_post, :builder => JustinFrench::Formtastic::SemanticFormBuilder) do |builder|
123
+ end
124
+ end
125
+ end
126
+
127
+ describe 'SemanticFormHelper' do
128
+
129
+ describe '#semantic_form_for' do
130
+
131
+ it 'yields an instance of SemanticFormBuilder' do
132
+ semantic_form_for(:post, Post.new, :url => '/hello') do |builder|
133
+ builder.class.should == Formtastic::SemanticFormBuilder
134
+ end
135
+ end
136
+
137
+ it 'adds a class of "formtastic" to the generated form' do
138
+ semantic_form_for(:post, Post.new, :url => '/hello') do |builder|
139
+ end
140
+ output_buffer.should have_tag("form.formtastic")
141
+ end
142
+
143
+ it 'adds class matching the object name to the generated form when a symbol is provided' do
144
+ semantic_form_for(:post, Post.new, :url => '/hello') do |builder|
145
+ end
146
+ output_buffer.should have_tag("form.post")
147
+
148
+ semantic_form_for(:project, :url => '/hello') do |builder|
149
+ end
150
+ output_buffer.should have_tag("form.project")
151
+ end
152
+
153
+ it 'adds class matching the object\'s class to the generated form when an object is provided' do
154
+ semantic_form_for(@new_post) do |builder|
155
+ end
156
+ output_buffer.should have_tag("form.post")
157
+ end
158
+
159
+ describe 'allows :html options' do
160
+ before(:each) do
161
+ semantic_form_for(:post, Post.new, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
162
+ end
163
+ end
164
+
165
+ it 'to add a id of "something-special" to generated form' do
166
+ output_buffer.should have_tag("form#something-special")
167
+ end
168
+
169
+ it 'to add a class of "something-extra" to generated form' do
170
+ output_buffer.should have_tag("form.something-extra")
171
+ end
172
+
173
+ it 'to add enctype="multipart/form-data"' do
174
+ output_buffer.should have_tag('form[@enctype="multipart/form-data"]')
175
+ end
176
+ end
177
+
178
+ it 'can be called with a resource-oriented style' do
179
+ semantic_form_for(@new_post) do |builder|
180
+ builder.object.class.should == Post
181
+ builder.object_name.should == "post"
182
+ end
183
+ end
184
+
185
+ it 'can be called with a generic style and instance variable' do
186
+ semantic_form_for(:post, @new_post, :url => new_post_path) do |builder|
187
+ builder.object.class.should == Post
188
+ builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
189
+ end
190
+ end
191
+
192
+ it 'can be called with a generic style and inline object' do
193
+ semantic_form_for(:post, Post.new, :url => new_post_path) do |builder|
194
+ builder.object.class.should == Post
195
+ builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
196
+ end
197
+ end
198
+
199
+ end
200
+
201
+ describe '#semantic_fields_for' do
202
+ it 'yields an instance of SemanticFormBuilder' do
203
+ semantic_fields_for(:post, Post.new, :url => '/hello') do |builder|
204
+ builder.class.should == Formtastic::SemanticFormBuilder
205
+ end
206
+ end
207
+ end
208
+
209
+ describe '#semantic_form_remote_for' do
210
+ it 'yields an instance of SemanticFormBuilder' do
211
+ semantic_form_remote_for(:post, Post.new, :url => '/hello') do |builder|
212
+ builder.class.should == Formtastic::SemanticFormBuilder
213
+ end
214
+ end
215
+ end
216
+
217
+ describe '#semantic_form_for_remote' do
218
+ it 'yields an instance of SemanticFormBuilder' do
219
+ semantic_form_remote_for(:post, Post.new, :url => '/hello') do |builder|
220
+ builder.class.should == Formtastic::SemanticFormBuilder
221
+ end
222
+ end
223
+ end
224
+
225
+ end
226
+
227
+ describe 'SemanticFormBuilder' do
228
+
229
+ include FormtasticSpecHelper
230
+
231
+ describe "@@builder" do
232
+ before do
233
+ @new_post.stub!(:title)
234
+ @new_post.stub!(:body)
235
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
236
+ end
237
+
238
+ after do
239
+ Formtastic::SemanticFormHelper.builder = Formtastic::SemanticFormBuilder
240
+ end
241
+
242
+ it "can be overridden" do
243
+
244
+ class CustomFormBuilder < Formtastic::SemanticFormBuilder
245
+ def custom(arg1, arg2, options = {})
246
+ [arg1, arg2, options]
247
+ end
248
+ end
249
+
250
+ Formtastic::SemanticFormHelper.builder = CustomFormBuilder
251
+
252
+ semantic_form_for(@new_post) do |builder|
253
+ builder.class.should == CustomFormBuilder
254
+ builder.custom("one", "two").should == ["one", "two", {}]
255
+ end
256
+ end
257
+
258
+ end
259
+
260
+ describe 'Formtastic::SemanticFormBuilder#semantic_fields_for' do
261
+ before do
262
+ @new_post.stub!(:author).and_return(Author.new)
263
+ end
264
+
265
+ it 'yields an instance of SemanticFormBuilder' do
266
+ semantic_form_for(@new_post) do |builder|
267
+ builder.semantic_fields_for(:author) do |nested_builder|
268
+ nested_builder.class.should == Formtastic::SemanticFormBuilder
269
+ end
270
+ end
271
+ end
272
+
273
+ it 'nests the object name' do
274
+ semantic_form_for(@new_post) do |builder|
275
+ builder.semantic_fields_for(@bob) do |nested_builder|
276
+ nested_builder.object_name.should == 'post[author]'
277
+ end
278
+ end
279
+ end
280
+
281
+ it 'should sanitize html id for li tag' do
282
+ @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
283
+ semantic_form_for(@new_post) do |builder|
284
+ builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
285
+ concat(nested_builder.inputs(:login))
286
+ end
287
+ end
288
+ output_buffer.should have_tag('form fieldset.inputs #post_author_1_login_input')
289
+ output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input')
290
+ end
291
+ end
292
+
293
+ describe '#label' do
294
+ it 'should humanize the given attribute' do
295
+ semantic_form_for(@new_post) do |builder|
296
+ builder.label(:login).should have_tag('label', :with => /Login/)
297
+ end
298
+ end
299
+
300
+ it 'should be printed as span' do
301
+ semantic_form_for(@new_post) do |builder|
302
+ builder.label(:login, nil, { :required => true, :as_span => true }).should have_tag('span.label abbr')
303
+ end
304
+ end
305
+
306
+ describe 'when required is given' do
307
+ it 'should append a required note' do
308
+ semantic_form_for(@new_post) do |builder|
309
+ builder.label(:login, nil, :required => true).should have_tag('label abbr')
310
+ end
311
+ end
312
+
313
+ it 'should allow require option to be given as second argument' do
314
+ semantic_form_for(@new_post) do |builder|
315
+ builder.label(:login, :required => true).should have_tag('label abbr')
316
+ end
317
+ end
318
+ end
319
+
320
+ describe 'when label is given' do
321
+ it 'should allow the text to be given as label option' do
322
+ semantic_form_for(@new_post) do |builder|
323
+ builder.label(:login, :required => true, :label => 'My label').should have_tag('label', :with => /My label/)
324
+ end
325
+ end
326
+
327
+ it 'should return nil if label is false' do
328
+ semantic_form_for(@new_post) do |builder|
329
+ builder.label(:login, :label => false).should be_nil
330
+ end
331
+ end
332
+ end
333
+ end
334
+
335
+ describe '#errors_on' do
336
+ before(:each) do
337
+ @title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome']
338
+ @errors = mock('errors')
339
+ @errors.stub!(:on).with('title').and_return(@title_errors)
340
+ @errors.stub!(:on).with('body').and_return(nil)
341
+ @new_post.stub!(:errors).and_return(@errors)
342
+ end
343
+
344
+ describe 'and the errors will be displayed as a sentence' do
345
+ it 'should render a paragraph with the errors joined into a sentence' do
346
+ Formtastic::SemanticFormBuilder.inline_errors = :sentence
347
+ semantic_form_for(@new_post) do |builder|
348
+ builder.errors_on(:title).should have_tag('p.inline-errors', @title_errors.to_sentence)
349
+ end
350
+ end
351
+ end
352
+
353
+ describe 'and the errors will be displayed as a list' do
354
+ it 'should render an unordered list with the class errors' do
355
+ Formtastic::SemanticFormBuilder.inline_errors = :list
356
+ semantic_form_for(@new_post) do |builder|
357
+ builder.errors_on(:title).should have_tag('ul.errors')
358
+ end
359
+ end
360
+
361
+ it 'should include a list element for each of the errors within the unordered list' do
362
+ Formtastic::SemanticFormBuilder.inline_errors = :list
363
+ semantic_form_for(@new_post) do |builder|
364
+ @title_errors.each do |error|
365
+ builder.errors_on(:title).should have_tag('ul.errors li', error)
366
+ end
367
+ end
368
+ end
369
+ end
370
+
371
+ describe 'but the errors will not be shown' do
372
+ it 'should return nil' do
373
+ Formtastic::SemanticFormBuilder.inline_errors = :none
374
+ semantic_form_for(@new_post) do |builder|
375
+ builder.errors_on(:title).should be_nil
376
+ end
377
+ end
378
+ end
379
+
380
+ describe 'and no error is found on the method' do
381
+ it 'should return nil' do
382
+ Formtastic::SemanticFormBuilder.inline_errors = :sentence
383
+ semantic_form_for(@new_post) do |builder|
384
+ builder.errors_on(:body).should be_nil
385
+ end
386
+ end
387
+ end
388
+ end
389
+
390
+ describe '#input' do
391
+
392
+ before do
393
+ @new_post.stub!(:title)
394
+ @new_post.stub!(:body)
395
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
396
+ end
397
+
398
+ describe 'with inline order customization' do
399
+ it 'should allow input, hints, errors as order' do
400
+ Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
401
+
402
+ semantic_form_for(@new_post) do |builder|
403
+ builder.should_receive(:inline_input_for).once.ordered
404
+ builder.should_receive(:inline_hints_for).once.ordered
405
+ builder.should_receive(:inline_errors_for).once.ordered
406
+ concat(builder.input(:title))
407
+ end
408
+ end
409
+
410
+ it 'should allow hints, input, errors as order' do
411
+ Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
412
+
413
+ semantic_form_for(@new_post) do |builder|
414
+ builder.should_receive(:inline_hints_for).once.ordered
415
+ builder.should_receive(:inline_input_for).once.ordered
416
+ builder.should_receive(:inline_errors_for).once.ordered
417
+ concat(builder.input(:title))
418
+ end
419
+ end
420
+ end
421
+
422
+ describe 'arguments and options' do
423
+
424
+ it 'should require the first argument (the method on form\'s object)' do
425
+ lambda {
426
+ semantic_form_for(@new_post) do |builder|
427
+ concat(builder.input()) # no args passed in at all
428
+ end
429
+ }.should raise_error(ArgumentError)
430
+ end
431
+
432
+ describe ':required option' do
433
+
434
+ describe 'when true' do
435
+
436
+ before do
437
+ @string = Formtastic::SemanticFormBuilder.required_string = " required yo!" # ensure there's something in the string
438
+ @new_post.class.should_not_receive(:reflect_on_all_validations)
439
+ end
440
+
441
+ after do
442
+ Formtastic::SemanticFormBuilder.required_string = %{<abbr title="required">*</abbr>}
443
+ end
444
+
445
+ it 'should set a "required" class' do
446
+ semantic_form_for(@new_post) do |builder|
447
+ concat(builder.input(:title, :required => true))
448
+ end
449
+ output_buffer.should_not have_tag('form li.optional')
450
+ output_buffer.should have_tag('form li.required')
451
+ end
452
+
453
+ it 'should append the "required" string to the label' do
454
+ semantic_form_for(@new_post) do |builder|
455
+ concat(builder.input(:title, :required => true))
456
+ end
457
+ output_buffer.should have_tag('form li.required label', /#{@string}$/)
458
+ end
459
+
460
+ end
461
+
462
+ describe 'when false' do
463
+
464
+ before do
465
+ @string = Formtastic::SemanticFormBuilder.optional_string = " optional yo!" # ensure there's something in the string
466
+ @new_post.class.should_not_receive(:reflect_on_all_validations)
467
+ end
468
+
469
+ after do
470
+ Formtastic::SemanticFormBuilder.optional_string = ''
471
+ end
472
+
473
+ it 'should set an "optional" class' do
474
+ semantic_form_for(@new_post) do |builder|
475
+ concat(builder.input(:title, :required => false))
476
+ end
477
+ output_buffer.should_not have_tag('form li.required')
478
+ output_buffer.should have_tag('form li.optional')
479
+ end
480
+
481
+ it 'should append the "optional" string to the label' do
482
+ semantic_form_for(@new_post) do |builder|
483
+ concat(builder.input(:title, :required => false))
484
+ end
485
+ output_buffer.should have_tag('form li.optional label', /#{@string}$/)
486
+ end
487
+
488
+ end
489
+
490
+ describe 'when not provided' do
491
+
492
+ describe 'and an object was not given' do
493
+
494
+ it 'should use the default value' do
495
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
496
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
497
+
498
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
499
+ concat(builder.input(:title))
500
+ end
501
+ output_buffer.should_not have_tag('form li.required')
502
+ output_buffer.should have_tag('form li.optional')
503
+
504
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
505
+ end
506
+
507
+ end
508
+
509
+ describe 'and an object was given' do
510
+
511
+ describe 'and the validation reflection plugin is available' do
512
+
513
+ before do
514
+ @new_post.class.stub!(:method_defined?).with(:reflect_on_all_validations).and_return(true)
515
+ end
516
+
517
+ describe 'and validates_presence_of was called for the method' do
518
+ before do
519
+ @new_post.class.should_receive(:reflect_on_all_validations).and_return([
520
+ mock('MacroReflection', :macro => :validates_presence_of, :name => :title)
521
+ ])
522
+ end
523
+
524
+ it 'should be required' do
525
+ semantic_form_for(@new_post) do |builder|
526
+ concat(builder.input(:title))
527
+ end
528
+ output_buffer.should have_tag('form li.required')
529
+ output_buffer.should_not have_tag('form li.optional')
530
+ end
531
+ end
532
+
533
+ describe 'and validates_presence_of was not called for the method' do
534
+ before do
535
+ @new_post.class.should_receive(:reflect_on_all_validations).and_return([])
536
+ end
537
+
538
+ it 'should not be required' do
539
+ semantic_form_for(@new_post) do |builder|
540
+ concat(builder.input(:title))
541
+ end
542
+ output_buffer.should_not have_tag('form li.required')
543
+ output_buffer.should have_tag('form li.optional')
544
+ end
545
+ end
546
+
547
+ end
548
+
549
+ describe 'and the validation reflection plugin is not available' do
550
+
551
+ it 'should use the default value' do
552
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
553
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
554
+
555
+ semantic_form_for(@new_post) do |builder|
556
+ concat(builder.input(:title))
557
+ end
558
+ output_buffer.should_not have_tag('form li.required')
559
+ output_buffer.should have_tag('form li.optional')
560
+
561
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
562
+ end
563
+
564
+ end
565
+
566
+ end
567
+
568
+ end
569
+
570
+ end
571
+
572
+ describe ':as option' do
573
+
574
+ describe 'when not provided' do
575
+
576
+ it 'should default to a string for forms without objects unless column is password' do
577
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
578
+ concat(builder.input(:anything))
579
+ end
580
+ output_buffer.should have_tag('form li.string')
581
+ end
582
+
583
+ it 'should default to password for forms without objects if column is password' do
584
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
585
+ concat(builder.input(:password))
586
+ concat(builder.input(:password_confirmation))
587
+ concat(builder.input(:confirm_password))
588
+ end
589
+ output_buffer.should have_tag('form li.password', :count => 3)
590
+ end
591
+
592
+ it 'should default to a string for methods on objects that don\'t respond to "column_for_attribute"' do
593
+ @new_post.stub!(:method_without_a_database_column)
594
+ @new_post.stub!(:column_for_attribute).and_return(nil)
595
+ default_input_type(nil, :method_without_a_database_column).should == :string
596
+ end
597
+
598
+ it 'should default to :password for methods that don\'t have a column in the database but "password" is in the method name' do
599
+ @new_post.stub!(:password_method_without_a_database_column)
600
+ @new_post.stub!(:column_for_attribute).and_return(nil)
601
+ default_input_type(nil, :password_method_without_a_database_column).should == :password
602
+ end
603
+
604
+ it 'should default to :password for methods on objects that don\'t respond to "column_for_attribute" but "password" is in the method name' do
605
+ @new_post.stub!(:password_method_without_a_database_column)
606
+ @new_post.stub!(:column_for_attribute).and_return(nil)
607
+ default_input_type(nil, :password_method_without_a_database_column).should == :password
608
+ end
609
+
610
+ it 'should default to :select for column names ending in "_id"' do
611
+ default_input_type(:integer, :user_id).should == :select
612
+ default_input_type(:integer, :section_id).should == :select
613
+ end
614
+
615
+ it 'should default to :password for :string column types with "password" in the method name' do
616
+ default_input_type(:string, :password).should == :password
617
+ default_input_type(:string, :hashed_password).should == :password
618
+ default_input_type(:string, :password_hash).should == :password
619
+ end
620
+
621
+ it 'should default to :text for :text column types' do
622
+ default_input_type(:text).should == :text
623
+ end
624
+
625
+ it 'should default to :date for :date column types' do
626
+ default_input_type(:date).should == :date
627
+ end
628
+
629
+ it 'should default to :datetime for :datetime and :timestamp column types' do
630
+ default_input_type(:datetime).should == :datetime
631
+ default_input_type(:timestamp).should == :datetime
632
+ end
633
+
634
+ it 'should default to :time for :time column types' do
635
+ default_input_type(:time).should == :time
636
+ end
637
+
638
+ it 'should default to :boolean for :boolean column types' do
639
+ default_input_type(:boolean).should == :boolean
640
+ end
641
+
642
+ it 'should default to :string for :string column types' do
643
+ default_input_type(:string).should == :string
644
+ end
645
+
646
+ it 'should default to :numeric for :integer, :float and :decimal column types' do
647
+ default_input_type(:integer).should == :numeric
648
+ default_input_type(:float).should == :numeric
649
+ default_input_type(:decimal).should == :numeric
650
+ end
651
+
652
+ it 'should default to :country for :string columns named country' do
653
+ default_input_type(:string, :country).should == :country
654
+ end
655
+
656
+ describe 'defaulting to file column' do
657
+ Formtastic::SemanticFormBuilder.file_methods.each do |method|
658
+ it "should default to :file for attributes that respond to ##{method}" do
659
+ @new_post.stub!(:column_for_attribute).and_return(nil)
660
+ column = mock('column')
661
+
662
+ Formtastic::SemanticFormBuilder.file_methods.each do |test|
663
+ column.stub!(:respond_to?).with(test).and_return(method == test)
664
+ end
665
+
666
+ @new_post.should_receive(method).and_return(column)
667
+
668
+ semantic_form_for(@new_post) do |builder|
669
+ builder.send(:default_input_type, method).should == :file
670
+ end
671
+ end
672
+ end
673
+
674
+ end
675
+ end
676
+
677
+ it 'should call the corresponding input method' do
678
+ [:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden].each do |input_style|
679
+ @new_post.stub!(:generic_column_name)
680
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
681
+ semantic_form_for(@new_post) do |builder|
682
+ builder.should_receive(:"#{input_style}_input").once.and_return("fake HTML output from #input")
683
+ concat(builder.input(:generic_column_name, :as => input_style))
684
+ end
685
+ end
686
+
687
+ Formtastic::SemanticFormBuilder::INPUT_MAPPINGS.keys.each do |input_style|
688
+ @new_post.stub!(:generic_column_name)
689
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
690
+ semantic_form_for(@new_post) do |builder|
691
+ builder.should_receive(:input_simple).once.and_return("fake HTML output from #input")
692
+ concat(builder.input(:generic_column_name, :as => input_style))
693
+ end
694
+ end
695
+ end
696
+
697
+ end
698
+
699
+ describe ':label option' do
700
+
701
+ describe 'when provided' do
702
+ it 'should be passed down to the label tag' do
703
+ semantic_form_for(@new_post) do |builder|
704
+ concat(builder.input(:title, :label => "Kustom"))
705
+ end
706
+ output_buffer.should have_tag("form li label", /Kustom/)
707
+ end
708
+ end
709
+
710
+ describe 'when not provided' do
711
+ describe 'when localized label is NOT provided' do
712
+ describe 'and object is not given' do
713
+ it 'should default the humanized method name, passing it down to the label tag' do
714
+ Formtastic::SemanticFormBuilder.label_str_method = :humanize
715
+
716
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
717
+ concat(builder.input(:meta_description))
718
+ end
719
+
720
+ output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
721
+ end
722
+ end
723
+
724
+ describe 'and object is given' do
725
+ it 'should delegate the label logic to class human attribute name and pass it down to the label tag' do
726
+ @new_post.stub!(:meta_description) # a two word method name
727
+ @new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return('meta_description'.humanize)
728
+
729
+ semantic_form_for(@new_post) do |builder|
730
+ concat(builder.input(:meta_description))
731
+ end
732
+
733
+ output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
734
+ end
735
+ end
736
+ end
737
+
738
+ describe 'when localized label is provided' do
739
+ before do
740
+ @localized_label_text = 'Localized title'
741
+ @default_localized_label_text = 'Default localized title'
742
+ ::I18n.backend.store_translations :en,
743
+ :formtastic => {
744
+ :labels => {
745
+ :title => @default_localized_label_text,
746
+ :post => {
747
+ :title => @localized_label_text
748
+ }
749
+ }
750
+ }
751
+ ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
752
+ end
753
+
754
+ it 'should render a label with localized label (I18n)' do
755
+ semantic_form_for(@new_post) do |builder|
756
+ concat(builder.input(:title, :label => true))
757
+ end
758
+ output_buffer.should have_tag('form li label', @localized_label_text)
759
+ end
760
+
761
+ it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do
762
+ ::I18n.backend.store_translations :en,
763
+ :formtastic => {
764
+ :labels => {
765
+ :post => {
766
+ :title => nil
767
+ }
768
+ }
769
+ }
770
+ semantic_form_for(@new_post) do |builder|
771
+ concat(builder.input(:title, :label => true))
772
+ end
773
+ output_buffer.should have_tag('form li label', @default_localized_label_text)
774
+ end
775
+ end
776
+ end
777
+
778
+ end
779
+
780
+ describe ':hint option' do
781
+
782
+ describe 'when provided' do
783
+ it 'should be passed down to the paragraph tag' do
784
+ hint_text = "this is the title of the post"
785
+ semantic_form_for(@new_post) do |builder|
786
+ concat(builder.input(:title, :hint => hint_text))
787
+ end
788
+ output_buffer.should have_tag("form li p.inline-hints", hint_text)
789
+ end
790
+ end
791
+
792
+ describe 'when not provided' do
793
+ describe 'when localized hint (I18n) is provided' do
794
+ before do
795
+ @localized_hint_text = "This is the localized hint."
796
+ @default_localized_hint_text = "This is the default localized hint."
797
+ ::I18n.backend.store_translations :en,
798
+ :formtastic => {
799
+ :hints => {
800
+ :title => @default_localized_hint_text,
801
+ :post => {
802
+ :title => @localized_hint_text
803
+ }
804
+ }
805
+ }
806
+ ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
807
+ end
808
+
809
+ describe 'when provided value (hint value) is set to TRUE' do
810
+ it 'should render a hint paragraph containing a localized hint (I18n)' do
811
+ semantic_form_for(@new_post) do |builder|
812
+ concat(builder.input(:title, :hint => true))
813
+ end
814
+ output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
815
+ end
816
+
817
+ it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
818
+ ::I18n.backend.store_translations :en,
819
+ :formtastic => {
820
+ :hints => {
821
+ :post => {
822
+ :title => nil
823
+ }
824
+ }
825
+ }
826
+ semantic_form_for(@new_post) do |builder|
827
+ concat(builder.input(:title, :hint => true))
828
+ end
829
+ output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
830
+ end
831
+ end
832
+
833
+ describe 'when provided value (label value) is set to FALSE' do
834
+ it 'should not render a hint paragraph' do
835
+ semantic_form_for(@new_post) do |builder|
836
+ concat(builder.input(:title, :hint => false))
837
+ end
838
+ output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text)
839
+ end
840
+ end
841
+ end
842
+
843
+ describe 'when localized hint (I18n) is not provided' do
844
+ it 'should not render a hint paragraph' do
845
+ semantic_form_for(@new_post) do |builder|
846
+ concat(builder.input(:title))
847
+ end
848
+ output_buffer.should_not have_tag('form li p.inline-hints')
849
+ end
850
+ end
851
+ end
852
+
853
+ end
854
+
855
+ describe ':wrapper_html option' do
856
+
857
+ describe 'when provided' do
858
+ it 'should be passed down to the li tag' do
859
+ semantic_form_for(@new_post) do |builder|
860
+ concat(builder.input(:title, :wrapper_html => {:id => :another_id}))
861
+ end
862
+ output_buffer.should have_tag("form li#another_id")
863
+ end
864
+
865
+ it 'should append given classes to li default classes' do
866
+ semantic_form_for(@new_post) do |builder|
867
+ concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true))
868
+ end
869
+ output_buffer.should have_tag("form li.string")
870
+ output_buffer.should have_tag("form li.required")
871
+ output_buffer.should have_tag("form li.another_class")
872
+ end
873
+
874
+ it 'should allow classes to be an array' do
875
+ semantic_form_for(@new_post) do |builder|
876
+ concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]}))
877
+ end
878
+ output_buffer.should have_tag("form li.string")
879
+ output_buffer.should have_tag("form li.my_class")
880
+ output_buffer.should have_tag("form li.another_class")
881
+ end
882
+ end
883
+
884
+ describe 'when not provided' do
885
+ it 'should use default id and class' do
886
+ semantic_form_for(@new_post) do |builder|
887
+ concat(builder.input(:title))
888
+ end
889
+ output_buffer.should have_tag("form li#post_title_input")
890
+ output_buffer.should have_tag("form li.string")
891
+ end
892
+ end
893
+
894
+ end
895
+ end
896
+
897
+ describe ':as any type of input' do
898
+
899
+ it 'should create a list item for each input' do
900
+ semantic_form_for(@new_post) do |builder|
901
+ concat(builder.input(:title))
902
+ concat(builder.input(:body))
903
+ end
904
+ output_buffer.should have_tag('form li', :count => 2)
905
+ end
906
+
907
+ describe 'when there are errors on the object for this method' do
908
+ before do
909
+ @title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome']
910
+ @errors = mock('errors')
911
+ @errors.stub!(:on).with('title').and_return(@title_errors)
912
+ @new_post.stub!(:errors).and_return(@errors)
913
+ end
914
+
915
+ it 'should apply an errors class to the list item' do
916
+ semantic_form_for(@new_post) do |builder|
917
+ concat(builder.input(:title))
918
+ end
919
+ output_buffer.should have_tag('form li.error')
920
+ end
921
+
922
+ it 'should not wrap the input with the Rails default error wrapping' do
923
+ semantic_form_for(@new_post) do |builder|
924
+ concat(builder.input(:title))
925
+ end
926
+ output_buffer.should_not have_tag('div.fieldWithErrors')
927
+ end
928
+
929
+ it 'should render a paragraph for the errors' do
930
+ Formtastic::SemanticFormBuilder.inline_errors = :sentence
931
+ semantic_form_for(@new_post) do |builder|
932
+ concat(builder.input(:title))
933
+ end
934
+ output_buffer.should have_tag('form li.error p.inline-errors')
935
+ end
936
+
937
+ it 'should not display an error list' do
938
+ Formtastic::SemanticFormBuilder.inline_errors = :list
939
+ semantic_form_for(@new_post) do |builder|
940
+ concat(builder.input(:title))
941
+ end
942
+ output_buffer.should have_tag('form li.error ul.errors')
943
+ end
944
+ end
945
+
946
+ describe 'when there are no errors on the object for this method' do
947
+ before do
948
+ semantic_form_for(@new_post) do |builder|
949
+ concat(builder.input(:title))
950
+ end
951
+ end
952
+
953
+ it 'should not apply an errors class to the list item' do
954
+ output_buffer.should_not have_tag('form li.error')
955
+ end
956
+
957
+ it 'should not render a paragraph for the errors' do
958
+ output_buffer.should_not have_tag('form li.error p.inline-errors')
959
+ end
960
+
961
+ it 'should not display an error list' do
962
+ output_buffer.should_not have_tag('form li.error ul.errors')
963
+ end
964
+ end
965
+
966
+ describe 'when no object is provided' do
967
+ before do
968
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
969
+ concat(builder.input(:title))
970
+ end
971
+ end
972
+
973
+ it 'should not apply an errors class to the list item' do
974
+ output_buffer.should_not have_tag('form li.error')
975
+ end
976
+
977
+ it 'should not render a paragraph for the errors' do
978
+ output_buffer.should_not have_tag('form li.error p.inline-errors')
979
+ end
980
+
981
+ it 'should not display an error list' do
982
+ output_buffer.should_not have_tag('form li.error ul.errors')
983
+ end
984
+ end
985
+ end
986
+
987
+ # Test string_mappings: :string, :password and :numeric
988
+ string_mappings = Formtastic::SemanticFormBuilder::INPUT_MAPPINGS.slice(*Formtastic::SemanticFormBuilder::STRING_MAPPINGS)
989
+ string_mappings.each do |type, template_method|
990
+ describe ":as => #{type.inspect}" do
991
+
992
+ before do
993
+ @new_post.stub!(:title)
994
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type, :limit => 50))
995
+
996
+ semantic_form_for(@new_post) do |builder|
997
+ concat(builder.input(:title, :as => type))
998
+ end
999
+ end
1000
+
1001
+ it "should have a #{type} class on the wrapper" do
1002
+ output_buffer.should have_tag("form li.#{type}")
1003
+ end
1004
+
1005
+ it 'should have a post_title_input id on the wrapper' do
1006
+ output_buffer.should have_tag('form li#post_title_input')
1007
+ end
1008
+
1009
+ it 'should generate a label for the input' do
1010
+ output_buffer.should have_tag('form li label')
1011
+ output_buffer.should have_tag('form li label[@for="post_title"')
1012
+ output_buffer.should have_tag('form li label', /Title/)
1013
+ end
1014
+
1015
+ input_type = template_method.to_s.split('_').first
1016
+
1017
+ it "should generate a #{input_type} input" do
1018
+ output_buffer.should have_tag("form li input")
1019
+ output_buffer.should have_tag("form li input#post_title")
1020
+ output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]")
1021
+ output_buffer.should have_tag("form li input[@name=\"post[title]\"]")
1022
+ end
1023
+
1024
+ unless type == :numeric
1025
+ it 'should have a maxlength matching the column limit' do
1026
+ @new_post.column_for_attribute(:title).limit.should == 50
1027
+ output_buffer.should have_tag("form li input[@maxlength='50']")
1028
+ end
1029
+
1030
+ it 'should use default_text_field_size for columns longer than default_text_field_size' do
1031
+ default_size = Formtastic::SemanticFormBuilder.default_text_field_size
1032
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type, :limit => default_size * 2))
1033
+
1034
+ semantic_form_for(@new_post) do |builder|
1035
+ concat(builder.input(:title, :as => type))
1036
+ end
1037
+
1038
+ output_buffer.should have_tag("form li input[@size='#{default_size}']")
1039
+ end
1040
+
1041
+ it 'should use the column size for columns shorter than default_text_field_size' do
1042
+ column_limit_shorted_than_default = 1
1043
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type, :limit => column_limit_shorted_than_default))
1044
+
1045
+ semantic_form_for(@new_post) do |builder|
1046
+ concat(builder.input(:title, :as => type))
1047
+ end
1048
+
1049
+ output_buffer.should have_tag("form li input[@size='#{column_limit_shorted_than_default}']")
1050
+ end
1051
+ end
1052
+
1053
+ it 'should use default_text_field_size for methods without database columns' do
1054
+ default_size = Formtastic::SemanticFormBuilder.default_text_field_size
1055
+ @new_post.stub!(:column_for_attribute).and_return(nil) # Return a nil column
1056
+
1057
+ semantic_form_for(@new_post) do |builder|
1058
+ concat(builder.input(:title, :as => type))
1059
+ end
1060
+
1061
+ output_buffer.should have_tag("form li input[@size='#{default_size}']")
1062
+ end
1063
+
1064
+ it 'should use input_html to style inputs' do
1065
+ semantic_form_for(@new_post) do |builder|
1066
+ concat(builder.input(:title, :as => type, :input_html => { :class => 'myclass' }))
1067
+ end
1068
+ output_buffer.should have_tag("form li input.myclass")
1069
+ end
1070
+
1071
+ it 'should generate input and labels even if no object is given' do
1072
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
1073
+ concat(builder.input(:title, :as => type))
1074
+ end
1075
+
1076
+ output_buffer.should have_tag('form li label')
1077
+ output_buffer.should have_tag('form li label[@for="project_title"')
1078
+ output_buffer.should have_tag('form li label', /Title/)
1079
+
1080
+ output_buffer.should have_tag("form li input")
1081
+ output_buffer.should have_tag("form li input#project_title")
1082
+ output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]")
1083
+ output_buffer.should have_tag("form li input[@name=\"project[title]\"]")
1084
+ end
1085
+
1086
+ end
1087
+ end
1088
+
1089
+ # Test other mappings that are not strings: :text and :file.
1090
+ other_mappings = Formtastic::SemanticFormBuilder::INPUT_MAPPINGS.except(*Formtastic::SemanticFormBuilder::STRING_MAPPINGS)
1091
+ other_mappings.each do |type, template_method|
1092
+ describe ":as => #{type.inspect}" do
1093
+
1094
+ before do
1095
+ @new_post.stub!(:body)
1096
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type))
1097
+
1098
+ semantic_form_for(@new_post) do |builder|
1099
+ concat(builder.input(:body, :as => type))
1100
+ end
1101
+ end
1102
+
1103
+ it "should have a #{type} class on the wrapper" do
1104
+ output_buffer.should have_tag('form li.#{type}')
1105
+ end
1106
+
1107
+ it 'should have a post_title_input id on the wrapper' do
1108
+ output_buffer.should have_tag('form li#post_body_input')
1109
+ end
1110
+
1111
+ it 'should generate a label for the input' do
1112
+ output_buffer.should have_tag('form li label')
1113
+ output_buffer.should have_tag('form li label[@for="post_body"')
1114
+ output_buffer.should have_tag('form li label', /Body/)
1115
+ end
1116
+
1117
+ input_type = template_method.to_s.gsub(/_field|_/, '')
1118
+
1119
+ if template_method.to_s =~ /_field$/ # password_field
1120
+
1121
+ it "should generate a #{input_type} input" do
1122
+ output_buffer.should have_tag("form li input")
1123
+ output_buffer.should have_tag("form li input#post_body")
1124
+ output_buffer.should have_tag("form li input[@name=\"post[body]\"]")
1125
+ output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]")
1126
+ end
1127
+
1128
+ it 'should use input_html to style inputs' do
1129
+ semantic_form_for(@new_post) do |builder|
1130
+ concat(builder.input(:title, :as => type, :input_html => { :class => 'myclass' }))
1131
+ end
1132
+ output_buffer.should have_tag("form li input.myclass")
1133
+ end
1134
+
1135
+ else # text_area
1136
+
1137
+ it "should generate a #{input_type} input" do
1138
+ output_buffer.should have_tag("form li #{input_type}")
1139
+ output_buffer.should have_tag("form li #{input_type}#post_body")
1140
+ output_buffer.should have_tag("form li #{input_type}[@name=\"post[body]\"]")
1141
+ end
1142
+
1143
+ it 'should use input_html to style inputs' do
1144
+ semantic_form_for(@new_post) do |builder|
1145
+ concat(builder.input(:title, :as => type, :input_html => { :class => 'myclass' }))
1146
+ end
1147
+ output_buffer.should have_tag("form li #{input_type}.myclass")
1148
+ end
1149
+
1150
+ end
1151
+
1152
+ describe 'when no object is given' do
1153
+ before(:each) do
1154
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
1155
+ concat(builder.input(:title, :as => type))
1156
+ end
1157
+ end
1158
+
1159
+ it 'should generate input' do
1160
+ if template_method.to_s =~ /_field$/ # password_field
1161
+ output_buffer.should have_tag("form li input")
1162
+ output_buffer.should have_tag("form li input#project_title")
1163
+ output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]")
1164
+ output_buffer.should have_tag("form li input[@name=\"project[title]\"]")
1165
+ else
1166
+ output_buffer.should have_tag("form li #{input_type}")
1167
+ output_buffer.should have_tag("form li #{input_type}#project_title")
1168
+ output_buffer.should have_tag("form li #{input_type}[@name=\"project[title]\"]")
1169
+ end
1170
+ end
1171
+
1172
+ it 'should generate labels' do
1173
+ output_buffer.should have_tag('form li label')
1174
+ output_buffer.should have_tag('form li label[@for="project_title"')
1175
+ output_buffer.should have_tag('form li label', /Title/)
1176
+ end
1177
+ end
1178
+
1179
+ end
1180
+ end
1181
+
1182
+ describe ":as => :hidden" do
1183
+ before do
1184
+ @new_post.stub!(:hidden)
1185
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string))
1186
+
1187
+ semantic_form_for(@new_post) do |builder|
1188
+ concat(builder.input(:hidden, :as => :hidden))
1189
+ end
1190
+ end
1191
+
1192
+ it "should have a hidden class on the wrapper" do
1193
+ output_buffer.should have_tag('form li.hidden')
1194
+ end
1195
+
1196
+ it 'should have a post_hidden_input id on the wrapper' do
1197
+ output_buffer.should have_tag('form li#post_hidden_input')
1198
+ end
1199
+
1200
+ it 'should not generate a label for the input' do
1201
+ output_buffer.should_not have_tag('form li label')
1202
+ end
1203
+
1204
+ it "should generate a input field" do
1205
+ output_buffer.should have_tag("form li input#post_hidden")
1206
+ output_buffer.should have_tag("form li input[@type=\"hidden\"]")
1207
+ output_buffer.should have_tag("form li input[@name=\"post[hidden]\"]")
1208
+ end
1209
+ end
1210
+
1211
+ describe ":as => :time_zone" do
1212
+ before do
1213
+ @new_post.stub!(:time_zone)
1214
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string))
1215
+
1216
+ semantic_form_for(@new_post) do |builder|
1217
+ concat(builder.input(:time_zone))
1218
+ end
1219
+ end
1220
+
1221
+ it "should have a time_zone class on the wrapper" do
1222
+ output_buffer.should have_tag('form li.time_zone')
1223
+ end
1224
+
1225
+ it 'should have a post_title_input id on the wrapper' do
1226
+ output_buffer.should have_tag('form li#post_time_zone_input')
1227
+ end
1228
+
1229
+ it 'should generate a label for the input' do
1230
+ output_buffer.should have_tag('form li label')
1231
+ output_buffer.should have_tag('form li label[@for="post_time_zone"')
1232
+ output_buffer.should have_tag('form li label', /Time zone/)
1233
+ end
1234
+
1235
+ it "should generate a select" do
1236
+ output_buffer.should have_tag("form li select")
1237
+ output_buffer.should have_tag("form li select#post_time_zone")
1238
+ output_buffer.should have_tag("form li select[@name=\"post[time_zone]\"]")
1239
+ end
1240
+
1241
+ it 'should use input_html to style inputs' do
1242
+ semantic_form_for(@new_post) do |builder|
1243
+ concat(builder.input(:time_zone, :input_html => { :class => 'myclass' }))
1244
+ end
1245
+ output_buffer.should have_tag("form li select.myclass")
1246
+ end
1247
+
1248
+ describe 'when no object is given' do
1249
+ before(:each) do
1250
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
1251
+ concat(builder.input(:time_zone, :as => :time_zone))
1252
+ end
1253
+ end
1254
+
1255
+ it 'should generate labels' do
1256
+ output_buffer.should have_tag('form li label')
1257
+ output_buffer.should have_tag('form li label[@for="project_time_zone"')
1258
+ output_buffer.should have_tag('form li label', /Time zone/)
1259
+ end
1260
+
1261
+ it 'should generate select inputs' do
1262
+ output_buffer.should have_tag("form li select")
1263
+ output_buffer.should have_tag("form li select#project_time_zone")
1264
+ output_buffer.should have_tag("form li select[@name=\"project[time_zone]\"]")
1265
+ end
1266
+ end
1267
+ end
1268
+
1269
+ describe ":as => :country" do
1270
+
1271
+ before do
1272
+ @new_post.stub!(:country)
1273
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string))
1274
+ end
1275
+
1276
+ describe "when country_select is not available as a helper from a plugin" do
1277
+
1278
+ it "should raise an error, sugesting the author installs a plugin" do
1279
+ lambda {
1280
+ semantic_form_for(@new_post) do |builder|
1281
+ concat(builder.input(:country, :as => :country))
1282
+ end
1283
+ }.should raise_error
1284
+ end
1285
+
1286
+ end
1287
+
1288
+ describe "when country_select is available as a helper (from a plugin)" do
1289
+
1290
+ before do
1291
+ semantic_form_for(@new_post) do |builder|
1292
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
1293
+ concat(builder.input(:country, :as => :country))
1294
+ end
1295
+ end
1296
+
1297
+ it "should have a time_zone class on the wrapper" do
1298
+ output_buffer.should have_tag('form li.country')
1299
+ end
1300
+
1301
+ it 'should have a post_title_input id on the wrapper' do
1302
+ output_buffer.should have_tag('form li#post_country_input')
1303
+ end
1304
+
1305
+ it 'should generate a label for the input' do
1306
+ output_buffer.should have_tag('form li label')
1307
+ output_buffer.should have_tag('form li label[@for="post_country"')
1308
+ output_buffer.should have_tag('form li label', /Country/)
1309
+ end
1310
+
1311
+ it "should generate a select" do
1312
+ output_buffer.should have_tag("form li select")
1313
+ end
1314
+
1315
+ end
1316
+
1317
+ describe ":priority_countries option" do
1318
+
1319
+ it "should be passed down to the country_select helper when provided" do
1320
+ priority_countries = ["Foo", "Bah"]
1321
+ semantic_form_for(@new_post) do |builder|
1322
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
1323
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
1324
+
1325
+ concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
1326
+ end
1327
+ end
1328
+
1329
+ it "should default to the @@priority_countries config when absent" do
1330
+ priority_countries = Formtastic::SemanticFormBuilder.priority_countries
1331
+ priority_countries.should_not be_empty
1332
+ priority_countries.should_not be_nil
1333
+
1334
+ semantic_form_for(@new_post) do |builder|
1335
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
1336
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
1337
+
1338
+ concat(builder.input(:country, :as => :country))
1339
+ end
1340
+ end
1341
+
1342
+ end
1343
+
1344
+ end
1345
+
1346
+ describe ':as => :radio' do
1347
+
1348
+ before do
1349
+ @new_post.stub!(:author).and_return(@bob)
1350
+ @new_post.stub!(:author_id).and_return(@bob.id)
1351
+ Post.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Author, :macro => :belongs_to) }
1352
+ end
1353
+
1354
+ describe 'for belongs_to association' do
1355
+ before do
1356
+ semantic_form_for(@new_post) do |builder|
1357
+ concat(builder.input(:author, :as => :radio, :value_as_class => true))
1358
+ end
1359
+ end
1360
+
1361
+ it 'should have a radio class on the wrapper' do
1362
+ output_buffer.should have_tag('form li.radio')
1363
+ end
1364
+
1365
+ it 'should have a post_author_input id on the wrapper' do
1366
+ output_buffer.should have_tag('form li#post_author_input')
1367
+ end
1368
+
1369
+ it 'should generate a fieldset and legend containing label text for the input' do
1370
+ output_buffer.should have_tag('form li fieldset')
1371
+ output_buffer.should have_tag('form li fieldset legend')
1372
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
1373
+ end
1374
+
1375
+ it 'should generate an ordered list with a list item for each choice' do
1376
+ output_buffer.should have_tag('form li fieldset ol')
1377
+ output_buffer.should have_tag('form li fieldset ol li', :count => Author.find(:all).size)
1378
+ end
1379
+
1380
+ it 'should have one option with a "checked" attribute' do
1381
+ output_buffer.should have_tag('form li input[@checked]', :count => 1)
1382
+ end
1383
+
1384
+ describe "each choice" do
1385
+
1386
+ it 'should contain a label for the radio input with a nested input and label text' do
1387
+ Author.find(:all).each do |author|
1388
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
1389
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']")
1390
+ end
1391
+ end
1392
+
1393
+ it 'should use values as li.class when value_as_class is true' do
1394
+ Author.find(:all).each do |author|
1395
+ output_buffer.should have_tag("form li fieldset ol li.#{author.id} label")
1396
+ end
1397
+ end
1398
+
1399
+ it "should have a radio input" do
1400
+ Author.find(:all).each do |author|
1401
+ output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}")
1402
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
1403
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
1404
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']")
1405
+ end
1406
+ end
1407
+
1408
+ it "should mark input as checked if it's the the existing choice" do
1409
+ @new_post.author_id.should == @bob.id
1410
+ @new_post.author.id.should == @bob.id
1411
+ @new_post.author.should == @bob
1412
+
1413
+ semantic_form_for(@new_post) do |builder|
1414
+ concat(builder.input(:author, :as => :radio))
1415
+ end
1416
+
1417
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
1418
+ end
1419
+ end
1420
+
1421
+ describe 'and no object is given' do
1422
+ before(:each) do
1423
+ output_buffer.replace ''
1424
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
1425
+ concat(builder.input(:author_id, :as => :radio, :collection => Author.find(:all)))
1426
+ end
1427
+ end
1428
+
1429
+ it 'should generate a fieldset with legend' do
1430
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
1431
+ end
1432
+
1433
+ it 'shold generate an li tag for each item in the collection' do
1434
+ output_buffer.should have_tag('form li fieldset ol li', :count => Author.find(:all).size)
1435
+ end
1436
+
1437
+ it 'should generate labels for each item' do
1438
+ Author.find(:all).each do |author|
1439
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
1440
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
1441
+ end
1442
+ end
1443
+
1444
+ it 'should generate inputs for each item' do
1445
+ Author.find(:all).each do |author|
1446
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
1447
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
1448
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
1449
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id]']")
1450
+ end
1451
+ end
1452
+ end
1453
+ end
1454
+ end
1455
+
1456
+ describe ':as => :select' do
1457
+
1458
+ before do
1459
+ @new_post.stub!(:author).and_return(@bob)
1460
+ @new_post.stub!(:author_id).and_return(@bob.id)
1461
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
1462
+ end
1463
+
1464
+ describe 'for a belongs_to association' do
1465
+ before do
1466
+ semantic_form_for(@new_post) do |builder|
1467
+ concat(builder.input(:author, :as => :select))
1468
+ end
1469
+ end
1470
+
1471
+ it 'should have a select class on the wrapper' do
1472
+ output_buffer.should have_tag('form li.select')
1473
+ end
1474
+
1475
+ it 'should have a post_author_input id on the wrapper' do
1476
+ output_buffer.should have_tag('form li#post_author_input')
1477
+ end
1478
+
1479
+ it 'should have a label inside the wrapper' do
1480
+ output_buffer.should have_tag('form li label')
1481
+ output_buffer.should have_tag('form li label', /Author/)
1482
+ output_buffer.should have_tag("form li label[@for='post_author_id']")
1483
+ end
1484
+
1485
+ it 'should have a select inside the wrapper' do
1486
+ output_buffer.should have_tag('form li select')
1487
+ output_buffer.should have_tag('form li select#post_author_id')
1488
+ end
1489
+
1490
+ it 'should not create a multi-select' do
1491
+ output_buffer.should_not have_tag('form li select[@multiple]')
1492
+ end
1493
+
1494
+ it 'should create a select without size' do
1495
+ output_buffer.should_not have_tag('form li select[@size]')
1496
+ end
1497
+
1498
+ it 'should have a blank option' do
1499
+ output_buffer.should have_tag("form li select option[@value='']")
1500
+ end
1501
+
1502
+ it 'should have a select option for each Author' do
1503
+ output_buffer.should have_tag('form li select option', :count => Author.find(:all).size + 1)
1504
+ Author.find(:all).each do |author|
1505
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
1506
+ end
1507
+ end
1508
+
1509
+ it 'should have one option with a "selected" attribute' do
1510
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
1511
+ end
1512
+
1513
+ it 'should not singularize the association name' do
1514
+ @new_post.stub!(:author_status).and_return(@bob)
1515
+ @new_post.stub!(:author_status_id).and_return(@bob.id)
1516
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
1517
+
1518
+ semantic_form_for(@new_post) do |builder|
1519
+ concat(builder.input(:author_status, :as => :select))
1520
+ end
1521
+
1522
+ output_buffer.should have_tag('form li select#post_author_status_id')
1523
+ end
1524
+ end
1525
+
1526
+ describe 'for a has_many association' do
1527
+ before do
1528
+ semantic_form_for(@fred) do |builder|
1529
+ concat(builder.input(:posts, :as => :select))
1530
+ end
1531
+ end
1532
+
1533
+ it 'should have a select class on the wrapper' do
1534
+ output_buffer.should have_tag('form li.select')
1535
+ end
1536
+
1537
+ it 'should have a post_author_input id on the wrapper' do
1538
+ output_buffer.should have_tag('form li#author_posts_input')
1539
+ end
1540
+
1541
+ it 'should have a label inside the wrapper' do
1542
+ output_buffer.should have_tag('form li label')
1543
+ output_buffer.should have_tag('form li label', /Post/)
1544
+ output_buffer.should have_tag("form li label[@for='author_post_ids']")
1545
+ end
1546
+
1547
+ it 'should have a select inside the wrapper' do
1548
+ output_buffer.should have_tag('form li select')
1549
+ output_buffer.should have_tag('form li select#author_post_ids')
1550
+ end
1551
+
1552
+ it 'should have a multi-select select' do
1553
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
1554
+ end
1555
+
1556
+ it 'should have a select option for each Post' do
1557
+ output_buffer.should have_tag('form li select option', :count => Post.find(:all).size)
1558
+ Post.find(:all).each do |post|
1559
+ output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/)
1560
+ end
1561
+ end
1562
+
1563
+ it 'should not have a blank option' do
1564
+ output_buffer.should_not have_tag("form li select option[@value='']")
1565
+ end
1566
+
1567
+ it 'should have one option with a "selected" attribute' do
1568
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
1569
+ end
1570
+ end
1571
+
1572
+ describe 'for a has_and_belongs_to_many association' do
1573
+ before do
1574
+ semantic_form_for(@freds_post) do |builder|
1575
+ concat(builder.input(:authors, :as => :select))
1576
+ end
1577
+ end
1578
+
1579
+ it 'should have a select class on the wrapper' do
1580
+ output_buffer.should have_tag('form li.select')
1581
+ end
1582
+
1583
+ it 'should have a post_author_input id on the wrapper' do
1584
+ output_buffer.should have_tag('form li#post_authors_input')
1585
+ end
1586
+
1587
+ it 'should have a label inside the wrapper' do
1588
+ output_buffer.should have_tag('form li label')
1589
+ output_buffer.should have_tag('form li label', /Author/)
1590
+ output_buffer.should have_tag("form li label[@for='post_author_ids']")
1591
+ end
1592
+
1593
+ it 'should have a select inside the wrapper' do
1594
+ output_buffer.should have_tag('form li select')
1595
+ output_buffer.should have_tag('form li select#post_author_ids')
1596
+ end
1597
+
1598
+ it 'should have a multi-select select' do
1599
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
1600
+ end
1601
+
1602
+ it 'should have a select option for each Author' do
1603
+ output_buffer.should have_tag('form li select option', :count => Author.find(:all).size)
1604
+ Author.find(:all).each do |author|
1605
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
1606
+ end
1607
+ end
1608
+
1609
+ it 'should not have a blank option' do
1610
+ output_buffer.should_not have_tag("form li select option[@value='']")
1611
+ end
1612
+
1613
+ it 'should have one option with a "selected" attribute' do
1614
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
1615
+ end
1616
+ end
1617
+
1618
+ describe 'when :include_blank is not set' do
1619
+ before do
1620
+ @new_post.stub!(:author_id).and_return(nil)
1621
+ semantic_form_for(@new_post) do |builder|
1622
+ concat(builder.input(:author, :as => :select))
1623
+ end
1624
+ end
1625
+
1626
+ it 'should have a blank option by default' do
1627
+ output_buffer.should have_tag("form li select option[@value='']", "")
1628
+ end
1629
+ end
1630
+
1631
+ describe 'when :include_blank is set to false' do
1632
+ before do
1633
+ @new_post.stub!(:author_id).and_return(nil)
1634
+ semantic_form_for(@new_post) do |builder|
1635
+ concat(builder.input(:author, :as => :select, :include_blank => false))
1636
+ end
1637
+ end
1638
+
1639
+ it 'should not have a blank option' do
1640
+ output_buffer.should_not have_tag("form li select option[@value='']", "")
1641
+ end
1642
+ end
1643
+
1644
+ describe 'when :prompt => "choose something" is set' do
1645
+ before do
1646
+ @new_post.stub!(:author_id).and_return(nil)
1647
+ semantic_form_for(@new_post) do |builder|
1648
+ concat(builder.input(:author, :as => :select, :prompt => "choose author"))
1649
+ end
1650
+ end
1651
+
1652
+ it 'should have a select with prompt' do
1653
+ output_buffer.should have_tag("form li select option[@value='']", /choose author/)
1654
+ end
1655
+
1656
+ it 'should not have a blank select option' do
1657
+ output_buffer.should_not have_tag("form li select option[@value='']", "")
1658
+ end
1659
+ end
1660
+
1661
+ describe 'when no object is given' do
1662
+ before(:each) do
1663
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
1664
+ concat(builder.input(:author, :as => :select, :collection => Author.find(:all)))
1665
+ end
1666
+ end
1667
+
1668
+ it 'should generate label' do
1669
+ output_buffer.should have_tag('form li label', /Author/)
1670
+ output_buffer.should have_tag("form li label[@for='project_author']")
1671
+ end
1672
+
1673
+ it 'should generate select inputs' do
1674
+ output_buffer.should have_tag('form li select#project_author')
1675
+ output_buffer.should have_tag('form li select option', :count => Author.find(:all).size + 1)
1676
+ end
1677
+
1678
+ it 'should generate an option to each item' do
1679
+ Author.find(:all).each do |author|
1680
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
1681
+ end
1682
+ end
1683
+ end
1684
+ end
1685
+
1686
+ describe ':as => :check_boxes' do
1687
+
1688
+ describe 'for a has_many association' do
1689
+ before do
1690
+ semantic_form_for(@fred) do |builder|
1691
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true))
1692
+ end
1693
+ end
1694
+
1695
+ it 'should have a check_boxes class on the wrapper' do
1696
+ output_buffer.should have_tag('form li.check_boxes')
1697
+ end
1698
+
1699
+ it 'should have a author_posts_input id on the wrapper' do
1700
+ output_buffer.should have_tag('form li#author_posts_input')
1701
+ end
1702
+
1703
+ it 'should generate a fieldset and legend containing label text for the input' do
1704
+ output_buffer.should have_tag('form li fieldset')
1705
+ output_buffer.should have_tag('form li fieldset legend')
1706
+ output_buffer.should have_tag('form li fieldset legend', /Posts/)
1707
+ end
1708
+
1709
+ it 'should generate an ordered list with a list item for each choice' do
1710
+ output_buffer.should have_tag('form li fieldset ol')
1711
+ output_buffer.should have_tag('form li fieldset ol li', :count => Post.find(:all).size)
1712
+ end
1713
+
1714
+ it 'should have one option with a "checked" attribute' do
1715
+ output_buffer.should have_tag('form li input[@checked]', :count => 1)
1716
+ end
1717
+
1718
+ it 'should generate hidden inputs with default value blank' do
1719
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => Post.find(:all).size)
1720
+ end
1721
+
1722
+ describe "each choice" do
1723
+
1724
+ it 'should contain a label for the radio input with a nested input and label text' do
1725
+ Post.find(:all).each do |post|
1726
+ output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
1727
+ output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
1728
+ end
1729
+ end
1730
+
1731
+ it 'should use values as li.class when value_as_class is true' do
1732
+ Post.find(:all).each do |post|
1733
+ output_buffer.should have_tag("form li fieldset ol li.#{post.id} label")
1734
+ end
1735
+ end
1736
+
1737
+ it 'should have a checkbox input for each post' do
1738
+ Post.find(:all).each do |post|
1739
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
1740
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
1741
+ end
1742
+ end
1743
+
1744
+ it "should mark input as checked if it's the the existing choice" do
1745
+ Post.find(:all).include?(@fred.posts.first).should be_true
1746
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
1747
+ end
1748
+ end
1749
+
1750
+ describe 'and no object is given' do
1751
+ before(:each) do
1752
+ output_buffer.replace ''
1753
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
1754
+ concat(builder.input(:author_id, :as => :check_boxes, :collection => Author.find(:all)))
1755
+ end
1756
+ end
1757
+
1758
+ it 'should generate a fieldset with legend' do
1759
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
1760
+ end
1761
+
1762
+ it 'shold generate an li tag for each item in the collection' do
1763
+ output_buffer.should have_tag('form li fieldset ol li', :count => Author.find(:all).size)
1764
+ end
1765
+
1766
+ it 'should generate labels for each item' do
1767
+ Author.find(:all).each do |author|
1768
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
1769
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
1770
+ end
1771
+ end
1772
+
1773
+ it 'should generate inputs for each item' do
1774
+ Author.find(:all).each do |author|
1775
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
1776
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
1777
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
1778
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
1779
+ end
1780
+ end
1781
+ end
1782
+ end
1783
+ end
1784
+
1785
+ describe 'for collections' do
1786
+
1787
+ before do
1788
+ @new_post.stub!(:author).and_return(@bob)
1789
+ @new_post.stub!(:author_id).and_return(@bob.id)
1790
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
1791
+ end
1792
+
1793
+ { :select => :option, :radio => :input, :check_boxes => :'input[@type="checkbox"]' }.each do |type, countable|
1794
+
1795
+ describe ":as => #{type.inspect}" do
1796
+ describe 'when the :collection option is not provided' do
1797
+ it 'should perform a basic find on the association class' do
1798
+ Author.should_receive(:find)
1799
+
1800
+ semantic_form_for(@new_post) do |builder|
1801
+ concat(builder.input(:author, :as => type))
1802
+ end
1803
+ end
1804
+
1805
+ it 'should show a deprecation warning if user gives the association using _id' do
1806
+ # Check for deprecation message
1807
+ ::ActiveSupport::Deprecation.should_receive(:warn).with(/association/, anything())
1808
+
1809
+ Author.should_receive(:find)
1810
+ semantic_form_for(@new_post) do |builder|
1811
+ concat(builder.input(:author_id, :as => type))
1812
+ end
1813
+ end
1814
+ end
1815
+
1816
+ describe 'when the :collection option is provided' do
1817
+
1818
+ before do
1819
+ @authors = Author.find(:all) * 2
1820
+ output_buffer.replace '' # clears the output_buffer from the before block, hax!
1821
+ end
1822
+
1823
+ it 'should not call find() on the parent class' do
1824
+ Author.should_not_receive(:find)
1825
+ semantic_form_for(@new_post) do |builder|
1826
+ concat(builder.input(:author, :as => type, :collection => @authors))
1827
+ end
1828
+ end
1829
+
1830
+ it 'should use the provided collection' do
1831
+ semantic_form_for(@new_post) do |builder|
1832
+ concat(builder.input(:author, :as => type, :collection => @authors))
1833
+ end
1834
+ output_buffer.should have_tag("form li.#{type} #{countable}", :count => @authors.size + (type == :select ? 1 : 0))
1835
+ end
1836
+
1837
+ describe 'and the :collection is an array of strings' do
1838
+ before do
1839
+ @new_post.stub!(:category_name).and_return('')
1840
+ @categories = [ 'General', 'Design', 'Development' ]
1841
+ end
1842
+
1843
+ it "should use the string as the label text and value for each #{countable}" do
1844
+ semantic_form_for(@new_post) do |builder|
1845
+ concat(builder.input(:category_name, :as => type, :collection => @categories))
1846
+ end
1847
+
1848
+ @categories.each do |value|
1849
+ output_buffer.should have_tag("form li.#{type}", /#{value}/)
1850
+ output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value}']")
1851
+ end
1852
+ end
1853
+
1854
+ if type == :radio
1855
+ it 'should generate a sanitized label for attribute' do
1856
+ @bob.stub!(:category_name).and_return(@categories)
1857
+ semantic_form_for(@new_post) do |builder|
1858
+ builder.semantic_fields_for(@bob) do |bob_builder|
1859
+ concat(bob_builder.input(:category_name, :as => type, :collection => @categories))
1860
+ end
1861
+ end
1862
+
1863
+ @categories.each do |item|
1864
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_#{item.downcase}']")
1865
+ end
1866
+ end
1867
+ end
1868
+ end
1869
+
1870
+ describe 'and the :collection is a hash of strings' do
1871
+ before do
1872
+ @new_post.stub!(:category_name).and_return('')
1873
+ @categories = { 'General' => 'gen', 'Design' => 'des','Development' => 'dev' }
1874
+ end
1875
+
1876
+ it "should use the key as the label text and the hash value as the value attribute for each #{countable}" do
1877
+ semantic_form_for(@new_post) do |builder|
1878
+ concat(builder.input(:category_name, :as => type, :collection => @categories))
1879
+ end
1880
+
1881
+ @categories.each do |label, value|
1882
+ output_buffer.should have_tag("form li.#{type}", /#{label}/)
1883
+ output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value}']")
1884
+ end
1885
+ end
1886
+ end
1887
+
1888
+ describe 'and the :collection is an array of arrays' do
1889
+ before do
1890
+ @new_post.stub!(:category_name).and_return('')
1891
+ @categories = { 'General' => 'gen', 'Design' => 'des','Development' => 'dev' }.to_a
1892
+ end
1893
+
1894
+ it "should use the first value as the label text and the last value as the value attribute for #{countable}" do
1895
+ semantic_form_for(@new_post) do |builder|
1896
+ concat(builder.input(:category_name, :as => type, :collection => @categories))
1897
+ end
1898
+
1899
+ @categories.each do |text, value|
1900
+ label = type == :select ? :option : :label
1901
+ output_buffer.should have_tag("form li.#{type} #{label}", /#{text}/i)
1902
+ output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value.to_s}']")
1903
+ end
1904
+ end
1905
+ end
1906
+
1907
+ describe 'and the :collection is an array of symbols' do
1908
+ before do
1909
+ @new_post.stub!(:category_name).and_return('')
1910
+ @categories = [ :General, :Design, :Development ]
1911
+ end
1912
+
1913
+ it "should use the symbol as the label text and value for each #{countable}" do
1914
+ semantic_form_for(@new_post) do |builder|
1915
+ concat(builder.input(:category_name, :as => type, :collection => @categories))
1916
+ end
1917
+
1918
+ @categories.each do |value|
1919
+ label = type == :select ? :option : :label
1920
+ output_buffer.should have_tag("form li.#{type} #{label}", /#{value}/i)
1921
+ output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value.to_s}']")
1922
+ end
1923
+ end
1924
+ end
1925
+
1926
+ describe 'when the :label_method option is provided' do
1927
+ before do
1928
+ semantic_form_for(@new_post) do |builder|
1929
+ concat(builder.input(:author, :as => type, :label_method => :login))
1930
+ end
1931
+ end
1932
+
1933
+ it 'should have options with text content from the specified method' do
1934
+ Author.find(:all).each do |author|
1935
+ output_buffer.should have_tag("form li.#{type}", /#{author.login}/)
1936
+ end
1937
+ end
1938
+ end
1939
+
1940
+ describe 'when the :label_method option is not provided' do
1941
+ Formtastic::SemanticFormBuilder.collection_label_methods.each do |label_method|
1942
+
1943
+ describe "when the collection objects respond to #{label_method}" do
1944
+ before do
1945
+ @fred.stub!(:respond_to?).and_return { |m| m.to_s == label_method }
1946
+ Author.find(:all).each { |a| a.stub!(label_method).and_return('The Label Text') }
1947
+
1948
+ semantic_form_for(@new_post) do |builder|
1949
+ concat(builder.input(:author, :as => type))
1950
+ end
1951
+ end
1952
+
1953
+ it "should render the options with #{label_method} as the label" do
1954
+ Author.find(:all).each do |author|
1955
+ output_buffer.should have_tag("form li.#{type}", /The Label Text/)
1956
+ end
1957
+ end
1958
+ end
1959
+
1960
+ end
1961
+ end
1962
+
1963
+ describe 'when the :value_method option is provided' do
1964
+ before do
1965
+ semantic_form_for(@new_post) do |builder|
1966
+ concat(builder.input(:author, :as => type, :value_method => :login))
1967
+ end
1968
+ end
1969
+
1970
+ it 'should have options with values from specified method' do
1971
+ Author.find(:all).each do |author|
1972
+ output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{author.login}']")
1973
+ end
1974
+ end
1975
+ end
1976
+
1977
+ end
1978
+ end
1979
+ end
1980
+
1981
+ describe 'for boolean attributes' do
1982
+
1983
+ { :select => :option, :radio => :input }.each do |type, countable|
1984
+ checked_or_selected = { :select => :selected, :radio => :checked }[type]
1985
+
1986
+ describe ":as => #{type.inspect}" do
1987
+
1988
+ before do
1989
+ @new_post.stub!(:allow_comments)
1990
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean))
1991
+
1992
+ semantic_form_for(@new_post) do |builder|
1993
+ concat(builder.input(:allow_comments, :as => type))
1994
+ end
1995
+ end
1996
+
1997
+ it "should have a #{type} class on the wrapper" do
1998
+ output_buffer.should have_tag("form li.#{type}")
1999
+ end
2000
+
2001
+ it 'should have a post_allow_comments_input id on the wrapper' do
2002
+ output_buffer.should have_tag('form li#post_allow_comments_input')
2003
+ end
2004
+
2005
+ it 'should generate a fieldset containing a legend' do
2006
+ output_buffer.should have_tag("form li.#{type}", /Allow comments/)
2007
+ end
2008
+
2009
+ it "should generate two #{countable}" do
2010
+ output_buffer.should have_tag("form li.#{type} #{countable}", :count => (type == :select ? 3 : 2))
2011
+ output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="true"]})
2012
+ output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="false"]})
2013
+ end
2014
+
2015
+ describe 'when the locale sets the label text' do
2016
+ before do
2017
+ I18n.backend.store_translations 'en', :formtastic => {:yes => 'Absolutely!', :no => 'Never!'}
2018
+
2019
+ semantic_form_for(@new_post) do |builder|
2020
+ concat(builder.input(:allow_comments, :as => type))
2021
+ end
2022
+ end
2023
+
2024
+ after do
2025
+ I18n.backend.store_translations 'en', :formtastic => {:yes => nil, :no => nil}
2026
+ end
2027
+
2028
+ it 'should allow translation of the labels' do
2029
+ output_buffer.should have_tag("form li.#{type}", /Absolutely\!/)
2030
+ output_buffer.should have_tag("form li.#{type}", /Never\!/)
2031
+ end
2032
+ end
2033
+
2034
+ describe 'when the value is nil' do
2035
+ before do
2036
+ @new_post.stub!(:allow_comments).and_return(nil)
2037
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean))
2038
+
2039
+ semantic_form_for(@new_post) do |builder|
2040
+ concat(builder.input(:allow_comments, :as => type))
2041
+ end
2042
+ end
2043
+
2044
+ it "should not mark either #{countable} as #{checked_or_selected}" do
2045
+ output_buffer.should_not have_tag(%{form li.#{type} input[@#{checked_or_selected}="#{checked_or_selected}"]})
2046
+ end
2047
+ end
2048
+
2049
+ describe 'when the value is true' do
2050
+ before do
2051
+ @new_post.stub!(:allow_comments).and_return(true)
2052
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean))
2053
+ semantic_form_for(@new_post) do |builder|
2054
+ concat(builder.input(:allow_comments, :as => type))
2055
+ end
2056
+ end
2057
+
2058
+ it "should mark the true #{countable} as #{checked_or_selected}" do
2059
+ output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="true"][@#{checked_or_selected}="#{checked_or_selected}"]}, :count => 1)
2060
+ end
2061
+
2062
+ it "should not mark the false #{countable} as #{checked_or_selected}" do
2063
+ output_buffer.should_not have_tag(%{form li.#{type} #{countable}[@value="false"][@#{checked_or_selected}="#{checked_or_selected}"]})
2064
+ end
2065
+ end
2066
+
2067
+ describe 'when the value is false' do
2068
+ before do
2069
+ @new_post.stub!(:allow_comments).and_return(false)
2070
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean))
2071
+ semantic_form_for(@new_post) do |builder|
2072
+ concat(builder.input(:allow_comments, :as => type))
2073
+ end
2074
+ end
2075
+
2076
+ it "should not mark the true #{countable} as #{checked_or_selected}" do
2077
+ output_buffer.should_not have_tag(%{form li.#{type} #{countable}[@value="true"][@#{checked_or_selected}="#{checked_or_selected}"]})
2078
+ end
2079
+
2080
+ it "should mark the false #{countable} as #{checked_or_selected}" do
2081
+ output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="false"][@#{checked_or_selected}="#{checked_or_selected}"]}, :count => 1)
2082
+ end
2083
+ end
2084
+
2085
+ describe 'when :true and :false options are provided' do
2086
+ before do
2087
+ @new_post.stub!(:allow_comments)
2088
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean))
2089
+ semantic_form_for(@new_post) do |builder|
2090
+ concat(builder.input(:allow_comments, :as => type, :true => "Absolutely", :false => "No Way"))
2091
+ end
2092
+ end
2093
+
2094
+ it 'should use them as labels' do
2095
+ output_buffer.should have_tag("form li.#{type}", /Absolutely/)
2096
+ output_buffer.should have_tag("form li.#{type}", /No Way/)
2097
+ end
2098
+ end
2099
+ end
2100
+
2101
+ end
2102
+ end
2103
+ end
2104
+
2105
+ describe ':as => :date' do
2106
+
2107
+ before do
2108
+ @new_post.stub!(:publish_at)
2109
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :date))
2110
+
2111
+ semantic_form_for(@new_post) do |@builder|
2112
+ concat(@builder.input(:publish_at, :as => :date))
2113
+ end
2114
+ end
2115
+
2116
+ it 'should have a date class on the wrapper li' do
2117
+ output_buffer.should have_tag('form li.date')
2118
+ end
2119
+
2120
+ it 'should have a fieldset inside the li wrapper' do
2121
+ output_buffer.should have_tag('form li.date fieldset')
2122
+ end
2123
+
2124
+ it 'should have a legend containing the label text inside the fieldset' do
2125
+ output_buffer.should have_tag('form li.date fieldset legend', /Publish at/)
2126
+ end
2127
+
2128
+ it 'should have an ordered list of three items inside the fieldset' do
2129
+ output_buffer.should have_tag('form li.date fieldset ol')
2130
+ output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
2131
+ end
2132
+
2133
+ it 'should have three labels for year, month and day' do
2134
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
2135
+ output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
2136
+ output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
2137
+ output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
2138
+ end
2139
+
2140
+ it 'should have three selects for year, month and day' do
2141
+ output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
2142
+ end
2143
+ end
2144
+
2145
+ describe ':as => :datetime' do
2146
+
2147
+ before do
2148
+ @new_post.stub!(:publish_at)
2149
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :datetime))
2150
+
2151
+ semantic_form_for(@new_post) do |builder|
2152
+ concat(builder.input(:publish_at, :as => :datetime))
2153
+ end
2154
+ end
2155
+
2156
+ it 'should have a datetime class on the wrapper li' do
2157
+ output_buffer.should have_tag('form li.datetime')
2158
+ end
2159
+
2160
+ it 'should have a fieldset inside the li wrapper' do
2161
+ output_buffer.should have_tag('form li.datetime fieldset')
2162
+ end
2163
+
2164
+ it 'should have a legend containing the label text inside the fieldset' do
2165
+ output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
2166
+ end
2167
+
2168
+ it 'should have an ordered list of five items inside the fieldset' do
2169
+ output_buffer.should have_tag('form li.datetime fieldset ol')
2170
+ output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5)
2171
+ end
2172
+
2173
+ it 'should have five labels for year, month, day, hour and minute' do
2174
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
2175
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i)
2176
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i)
2177
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i)
2178
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i)
2179
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i)
2180
+ end
2181
+
2182
+ it 'should have five selects for year, month, day, hour and minute' do
2183
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
2184
+ end
2185
+
2186
+ it 'should generate a sanitized label and matching ids for attribute' do
2187
+ @bob.stub!(:publish_at)
2188
+ @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :datetime))
2189
+
2190
+ semantic_form_for(@new_post) do |builder|
2191
+ builder.semantic_fields_for(@bob, :index => 10) do |bob_builder|
2192
+ concat(bob_builder.input(:publish_at, :as => :datetime))
2193
+ end
2194
+ end
2195
+
2196
+ 1.upto(5) do |i|
2197
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_publish_at_#{i}i']")
2198
+ output_buffer.should have_tag("form li fieldset ol li #post_author_10_publish_at_#{i}i")
2199
+ end
2200
+ end
2201
+
2202
+ describe 'when :discard_input => true is set' do
2203
+ it 'should use default hidden value equals to 1 when attribute returns nil' do
2204
+ semantic_form_for(@new_post) do |builder|
2205
+ concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
2206
+ end
2207
+
2208
+ output_buffer.should have_tag("form li input[@type='hidden'][@value='1']")
2209
+ end
2210
+
2211
+ it 'should use default attribute value when it is not nil' do
2212
+ @new_post.stub!(:publish_at).and_return(Date.new(2007,12,27))
2213
+ semantic_form_for(@new_post) do |builder|
2214
+ concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
2215
+ end
2216
+
2217
+ output_buffer.should have_tag("form li input[@type='hidden'][@value='27']")
2218
+ end
2219
+ end
2220
+
2221
+ describe 'when :include_blank => true is set' do
2222
+ before do
2223
+ semantic_form_for(@new_post) do |builder|
2224
+ concat(builder.input(:publish_at, :as => :datetime, :include_blank => true))
2225
+ end
2226
+ end
2227
+
2228
+ it 'should have a blank select option' do
2229
+ output_buffer.should have_tag("option[@value='']", "")
2230
+ end
2231
+ end
2232
+
2233
+ describe 'inputs order' do
2234
+ it 'should have a default' do
2235
+ semantic_form_for(@new_post) do |builder|
2236
+ self.should_receive(:select_year).once.ordered.and_return('')
2237
+ self.should_receive(:select_month).once.ordered.and_return('')
2238
+ self.should_receive(:select_day).once.ordered.and_return('')
2239
+ builder.input(:publish_at, :as => :datetime)
2240
+ end
2241
+ end
2242
+
2243
+ it 'should be specified with :order option' do
2244
+ I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
2245
+ semantic_form_for(@new_post) do |builder|
2246
+ self.should_receive(:select_month).once.ordered.and_return('')
2247
+ self.should_receive(:select_year).once.ordered.and_return('')
2248
+ self.should_receive(:select_day).once.ordered.and_return('')
2249
+ builder.input(:publish_at, :as => :datetime)
2250
+ end
2251
+ end
2252
+
2253
+ it 'should be changed through I18n' do
2254
+ semantic_form_for(@new_post) do |builder|
2255
+ self.should_receive(:select_day).once.ordered.and_return('')
2256
+ self.should_receive(:select_month).once.ordered.and_return('')
2257
+ self.should_receive(:select_year).once.ordered.and_return('')
2258
+ builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year])
2259
+ end
2260
+ end
2261
+ end
2262
+
2263
+ describe 'when the locale changes the label text' do
2264
+ before do
2265
+ I18n.backend.store_translations 'en', :datetime => {:prompts => {
2266
+ :year => 'The Year', :month => 'The Month', :day => 'The Day',
2267
+ :hour => 'The Hour', :minute => 'The Minute'
2268
+ }}
2269
+ semantic_form_for(@new_post) do |builder|
2270
+ concat(builder.input(:publish_at, :as => :datetime))
2271
+ end
2272
+ end
2273
+
2274
+ after do
2275
+ I18n.backend.store_translations 'en', :formtastic => {
2276
+ :year => nil, :month => nil, :day => nil,
2277
+ :hour => nil, :minute => nil
2278
+ }
2279
+ end
2280
+
2281
+ it 'should have translated labels for year, month, day, hour and minute' do
2282
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/)
2283
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/)
2284
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/)
2285
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/)
2286
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/)
2287
+ end
2288
+ end
2289
+
2290
+ describe 'when no object is given' do
2291
+ before(:each) do
2292
+ output_buffer.replace ''
2293
+ semantic_form_for(:project, :url => 'http://test.host') do |@builder|
2294
+ concat(@builder.input(:publish_at, :as => :datetime))
2295
+ end
2296
+ end
2297
+
2298
+ it 'should have fieldset with legend' do
2299
+ output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
2300
+ end
2301
+
2302
+ it 'should have labels for each input' do
2303
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
2304
+ end
2305
+
2306
+ it 'should have selects for each inputs' do
2307
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
2308
+ end
2309
+ end
2310
+ end
2311
+
2312
+ describe ':as => :time' do
2313
+ before do
2314
+ @new_post.stub!(:publish_at)
2315
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :time))
2316
+
2317
+ semantic_form_for(@new_post) do |builder|
2318
+ concat(builder.input(:publish_at, :as => :time))
2319
+ end
2320
+ end
2321
+
2322
+ it 'should have a time class on the wrapper li' do
2323
+ output_buffer.should have_tag('form li.time')
2324
+ end
2325
+
2326
+ it 'should have a fieldset inside the li wrapper' do
2327
+ output_buffer.should have_tag('form li.time fieldset')
2328
+ end
2329
+
2330
+ it 'should have a legend containing the label text inside the fieldset' do
2331
+ output_buffer.should have_tag('form li.time fieldset legend', /Publish at/)
2332
+ end
2333
+
2334
+ it 'should have an ordered list of two items inside the fieldset' do
2335
+ output_buffer.should have_tag('form li.time fieldset ol')
2336
+ output_buffer.should have_tag('form li.time fieldset ol li', :count => 2)
2337
+ end
2338
+
2339
+ it 'should have five labels for hour and minute' do
2340
+ output_buffer.should have_tag('form li.time fieldset ol li label', :count => 2)
2341
+ output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i)
2342
+ output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i)
2343
+ end
2344
+
2345
+ it 'should have two selects for hour and minute' do
2346
+ #output_buffer.should have_tag('form li.time fieldset ol li select', :count => 2)
2347
+ output_buffer.should have_tag('form li.time fieldset ol li', :count => 2)
2348
+ end
2349
+ end
2350
+
2351
+ [:boolean_select, :boolean_radio].each do |type|
2352
+ describe ":as => #{type.inspect}" do
2353
+ it 'should show a deprecation warning' do
2354
+ @new_post.stub!(:allow_comments)
2355
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean))
2356
+
2357
+ ::ActiveSupport::Deprecation.should_receive(:warn).with(/select|radio/, anything())
2358
+
2359
+ semantic_form_for(@new_post) do |builder|
2360
+ concat(builder.input(:allow_comments, :as => type))
2361
+ end
2362
+ end
2363
+ end
2364
+ end
2365
+
2366
+ describe ':as => :boolean' do
2367
+
2368
+ before do
2369
+ @new_post.stub!(:allow_comments)
2370
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean))
2371
+
2372
+ semantic_form_for(@new_post) do |builder|
2373
+ concat(builder.input(:allow_comments, :as => :boolean))
2374
+ end
2375
+ end
2376
+
2377
+ it 'should have a boolean class on the wrapper' do
2378
+ output_buffer.should have_tag('form li.boolean')
2379
+ end
2380
+
2381
+ it 'should have a post_allow_comments_input id on the wrapper' do
2382
+ output_buffer.should have_tag('form li#post_allow_comments_input')
2383
+ end
2384
+
2385
+ it 'should generate a label containing the input' do
2386
+ output_buffer.should have_tag('form li label')
2387
+ output_buffer.should have_tag('form li label[@for="post_allow_comments"')
2388
+ output_buffer.should have_tag('form li label', /Allow comments/)
2389
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
2390
+ end
2391
+
2392
+ it 'should generate a checkbox input' do
2393
+ output_buffer.should have_tag('form li label input')
2394
+ output_buffer.should have_tag('form li label input#post_allow_comments')
2395
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
2396
+ output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]')
2397
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]')
2398
+ end
2399
+
2400
+ it 'should allow checked and unchecked values to be sent' do
2401
+ semantic_form_for(@new_post) do |builder|
2402
+ concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked'))
2403
+ end
2404
+
2405
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]')
2406
+ output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
2407
+ end
2408
+
2409
+ it 'should generate a label and a checkbox even if no object is given' do
2410
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
2411
+ concat(builder.input(:allow_comments, :as => :boolean))
2412
+ end
2413
+
2414
+ output_buffer.should have_tag('form li label[@for="project_allow_comments"')
2415
+ output_buffer.should have_tag('form li label', /Allow comments/)
2416
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
2417
+
2418
+ output_buffer.should have_tag('form li label input#project_allow_comments')
2419
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
2420
+ output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]')
2421
+ end
2422
+
2423
+ end
2424
+ end
2425
+
2426
+ describe '#inputs' do
2427
+
2428
+ describe 'with a block' do
2429
+
2430
+ describe 'when no options are provided' do
2431
+ before do
2432
+ output_buffer.replace 'before_builder' # clear the output buffer and sets before_builder
2433
+ semantic_form_for(@new_post) do |builder|
2434
+ @inputs_output = builder.inputs do
2435
+ concat('hello')
2436
+ end
2437
+ end
2438
+ end
2439
+
2440
+ it 'should output just the content wrapped in inputs, not the whole template' do
2441
+ output_buffer.should =~ /before_builder/
2442
+ @inputs_output.should_not =~ /before_builder/
2443
+ end
2444
+
2445
+ it 'should render a fieldset inside the form, with a class of "inputs"' do
2446
+ output_buffer.should have_tag("form fieldset.inputs")
2447
+ end
2448
+
2449
+ it 'should render an ol inside the fieldset' do
2450
+ output_buffer.should have_tag("form fieldset.inputs ol")
2451
+ end
2452
+
2453
+ it 'should render the contents of the block inside the ol' do
2454
+ output_buffer.should have_tag("form fieldset.inputs ol", /hello/)
2455
+ end
2456
+
2457
+ it 'should not render a legend inside the fieldset' do
2458
+ output_buffer.should_not have_tag("form fieldset.inputs legend")
2459
+ end
2460
+
2461
+ it 'should render a fieldset even if no object is given' do
2462
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
2463
+ @inputs_output = builder.inputs do
2464
+ concat('bye')
2465
+ end
2466
+ end
2467
+
2468
+ output_buffer.should have_tag("form fieldset.inputs ol", /bye/)
2469
+ end
2470
+ end
2471
+
2472
+ describe 'when a :for option is provided' do
2473
+ it 'should render nested inputs' do
2474
+ @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
2475
+
2476
+ semantic_form_for(@new_post) do |builder|
2477
+ builder.inputs :for => [:author, @bob] do |bob_builder|
2478
+ concat(bob_builder.input(:login))
2479
+ end
2480
+ end
2481
+
2482
+ output_buffer.should have_tag("form fieldset.inputs #post_author_login")
2483
+ output_buffer.should_not have_tag("form fieldset.inputs #author_login")
2484
+ end
2485
+
2486
+ it 'should raise an error if :for and block with no argument is given' do
2487
+ semantic_form_for(@new_post) do |builder|
2488
+ proc {
2489
+ builder.inputs(:for => [:author, @bob]) do
2490
+ #
2491
+ end
2492
+ }.should raise_error(ArgumentError, 'You gave :for option with a block to inputs method, ' <<
2493
+ 'but the block does not accept any argument.')
2494
+ end
2495
+ end
2496
+
2497
+ it 'should pass options down to semantic_fields_for' do
2498
+ @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
2499
+
2500
+ semantic_form_for(@new_post) do |builder|
2501
+ builder.inputs :for => [:author, @bob], :for_options => { :index => 10 } do |bob_builder|
2502
+ concat(bob_builder.input(:login))
2503
+ end
2504
+ end
2505
+
2506
+ output_buffer.should have_tag('form fieldset ol li #post_author_10_login')
2507
+ end
2508
+
2509
+ it 'should not add builder as a fieldset attribute tag' do
2510
+ semantic_form_for(@new_post) do |builder|
2511
+ builder.inputs :for => [:author, @bob], :for_options => { :index => 10 } do |bob_builder|
2512
+ concat('input')
2513
+ end
2514
+ end
2515
+
2516
+ output_buffer.should_not have_tag('fieldset[@builder="Formtastic::SemanticFormHelper"]')
2517
+ end
2518
+
2519
+ it 'should send parent_builder as an option to allow child index interpolation' do
2520
+ semantic_form_for(@new_post) do |builder|
2521
+ builder.instance_variable_set('@nested_child_index', 0)
2522
+ builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder|
2523
+ concat('input')
2524
+ end
2525
+ end
2526
+
2527
+ output_buffer.should have_tag('fieldset legend', 'Author #1')
2528
+ end
2529
+
2530
+ it 'should also provide child index interpolation when nested child index is a hash' do
2531
+ semantic_form_for(@new_post) do |builder|
2532
+ builder.instance_variable_set('@nested_child_index', :author => 10)
2533
+ builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder|
2534
+ concat('input')
2535
+ end
2536
+ end
2537
+
2538
+ output_buffer.should have_tag('fieldset legend', 'Author #11')
2539
+ end
2540
+ end
2541
+
2542
+ describe 'when a :name option is provided' do
2543
+ before do
2544
+ @legend_text = "Advanced options"
2545
+
2546
+ semantic_form_for(@new_post) do |builder|
2547
+ builder.inputs :name => @legend_text do
2548
+ end
2549
+ end
2550
+ end
2551
+
2552
+ it 'should render a fieldset inside the form' do
2553
+ output_buffer.should have_tag("form fieldset legend", /#{@legend_text}/)
2554
+ end
2555
+ end
2556
+
2557
+ describe 'when other options are provided' do
2558
+ before do
2559
+ @id_option = 'advanced'
2560
+ @class_option = 'wide'
2561
+
2562
+ semantic_form_for(@new_post) do |builder|
2563
+ builder.inputs :id => @id_option, :class => @class_option do
2564
+ end
2565
+ end
2566
+ end
2567
+
2568
+ it 'should pass the options into the fieldset tag as attributes' do
2569
+ output_buffer.should have_tag("form fieldset##{@id_option}")
2570
+ output_buffer.should have_tag("form fieldset.#{@class_option}")
2571
+ end
2572
+ end
2573
+
2574
+ end
2575
+
2576
+ describe 'without a block' do
2577
+
2578
+ before do
2579
+ Post.stub!(:reflections).and_return({:author => mock('reflection', :options => {}, :macro => :belongs_to),
2580
+ :comments => mock('reflection', :options => {}, :macro => :has_many) })
2581
+ Post.stub!(:content_columns).and_return([mock('column', :name => 'title'), mock('column', :name => 'body'), mock('column', :name => 'created_at')])
2582
+ Author.stub!(:find).and_return([@fred, @bob])
2583
+
2584
+ @new_post.stub!(:title)
2585
+ @new_post.stub!(:body)
2586
+ @new_post.stub!(:author_id)
2587
+
2588
+ @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 255))
2589
+ @new_post.stub!(:column_for_attribute).with(:body).and_return(mock('column', :type => :text))
2590
+ @new_post.stub!(:column_for_attribute).with(:created_at).and_return(mock('column', :type => :datetime))
2591
+ @new_post.stub!(:column_for_attribute).with(:author).and_return(nil)
2592
+ end
2593
+
2594
+ describe 'with no args' do
2595
+ before do
2596
+ semantic_form_for(@new_post) do |builder|
2597
+ concat(builder.inputs)
2598
+ end
2599
+ end
2600
+
2601
+ it 'should render a form' do
2602
+ output_buffer.should have_tag('form')
2603
+ end
2604
+
2605
+ it 'should render a fieldset inside the form' do
2606
+ output_buffer.should have_tag('form > fieldset.inputs')
2607
+ end
2608
+
2609
+ it 'should not render a legend in the fieldset' do
2610
+ output_buffer.should_not have_tag('form > fieldset.inputs > legend')
2611
+ end
2612
+
2613
+ it 'should render an ol in the fieldset' do
2614
+ output_buffer.should have_tag('form > fieldset.inputs > ol')
2615
+ end
2616
+
2617
+ it 'should render a list item in the ol for each column and reflection' do
2618
+ # Remove the :has_many macro and :created_at column
2619
+ count = Post.content_columns.size + Post.reflections.size - 2
2620
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li', :count => count)
2621
+ end
2622
+
2623
+ it 'should render a string list item for title' do
2624
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li.string')
2625
+ end
2626
+
2627
+ it 'should render a text list item for body' do
2628
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li.text')
2629
+ end
2630
+
2631
+ it 'should render a select list item for author_id' do
2632
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li.select', :count => 1)
2633
+ end
2634
+
2635
+ it 'should not render timestamps inputs by default' do
2636
+ output_buffer.should_not have_tag('form > fieldset.inputs > ol > li.datetime')
2637
+ end
2638
+ end
2639
+
2640
+ describe 'with column names as args' do
2641
+ describe 'and an object is given' do
2642
+ it 'should render a form with a fieldset containing two list items' do
2643
+ semantic_form_for(@new_post) do |builder|
2644
+ concat(builder.inputs(:title, :body))
2645
+ end
2646
+
2647
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li', :count => 2)
2648
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li.string')
2649
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li.text')
2650
+ end
2651
+ end
2652
+
2653
+ describe 'and no object is given' do
2654
+ it 'should render a form with a fieldset containing two list items' do
2655
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
2656
+ concat(builder.inputs(:title, :body))
2657
+ end
2658
+
2659
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li.string', :count => 2)
2660
+ end
2661
+ end
2662
+ end
2663
+
2664
+ describe 'when a :for option is provided' do
2665
+ describe 'and an object is given' do
2666
+ it 'should render nested inputs' do
2667
+ @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
2668
+
2669
+ semantic_form_for(@new_post) do |builder|
2670
+ concat(builder.inputs(:login, :for => @bob))
2671
+ end
2672
+
2673
+ output_buffer.should have_tag("form fieldset.inputs #post_author_login")
2674
+ output_buffer.should_not have_tag("form fieldset.inputs #author_login")
2675
+ end
2676
+ end
2677
+
2678
+ describe 'and no object is given' do
2679
+ it 'should render nested inputs' do
2680
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
2681
+ concat(builder.inputs(:login, :for => @bob))
2682
+ end
2683
+
2684
+ output_buffer.should have_tag("form fieldset.inputs #project_author_login")
2685
+ output_buffer.should_not have_tag("form fieldset.inputs #project_login")
2686
+ end
2687
+ end
2688
+ end
2689
+
2690
+ describe 'with column names and an options hash as args' do
2691
+ before do
2692
+ semantic_form_for(@new_post) do |builder|
2693
+ concat(builder.inputs(:title, :body, :name => "Legendary Legend Text", :id => "my-id"))
2694
+ end
2695
+ end
2696
+
2697
+ it 'should render a form with a fieldset containing two list items' do
2698
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li', :count => 2)
2699
+ end
2700
+
2701
+ it 'should pass the options down to the fieldset' do
2702
+ output_buffer.should have_tag('form > fieldset#my-id.inputs')
2703
+ end
2704
+
2705
+ it 'should use the special :name option as a text for the legend tag' do
2706
+ output_buffer.should have_tag('form > fieldset#my-id.inputs > legend', /Legendary Legend Text/)
2707
+ end
2708
+ end
2709
+
2710
+ end
2711
+
2712
+ end
2713
+
2714
+ describe '#buttons' do
2715
+
2716
+ describe 'with a block' do
2717
+ describe 'when no options are provided' do
2718
+ before do
2719
+ semantic_form_for(@new_post) do |builder|
2720
+ builder.buttons do
2721
+ concat('hello')
2722
+ end
2723
+ end
2724
+ end
2725
+
2726
+ it 'should render a fieldset inside the form, with a class of "inputs"' do
2727
+ output_buffer.should have_tag("form fieldset.buttons")
2728
+ end
2729
+
2730
+ it 'should render an ol inside the fieldset' do
2731
+ output_buffer.should have_tag("form fieldset.buttons ol")
2732
+ end
2733
+
2734
+ it 'should render the contents of the block inside the ol' do
2735
+ output_buffer.should have_tag("form fieldset.buttons ol", /hello/)
2736
+ end
2737
+
2738
+ it 'should not render a legend inside the fieldset' do
2739
+ output_buffer.should_not have_tag("form fieldset.buttons legend")
2740
+ end
2741
+ end
2742
+
2743
+ describe 'when a :name option is provided' do
2744
+ before do
2745
+ @legend_text = "Advanced options"
2746
+
2747
+ semantic_form_for(@new_post) do |builder|
2748
+ builder.buttons :name => @legend_text do
2749
+ end
2750
+ end
2751
+ end
2752
+ it 'should render a fieldset inside the form' do
2753
+ output_buffer.should have_tag("form fieldset legend", /#{@legend_text}/)
2754
+ end
2755
+ end
2756
+
2757
+ describe 'when other options are provided' do
2758
+ before do
2759
+ @id_option = 'advanced'
2760
+ @class_option = 'wide'
2761
+
2762
+ semantic_form_for(@new_post) do |builder|
2763
+ builder.buttons :id => @id_option, :class => @class_option do
2764
+ end
2765
+ end
2766
+ end
2767
+ it 'should pass the options into the fieldset tag as attributes' do
2768
+ output_buffer.should have_tag("form fieldset##{@id_option}")
2769
+ output_buffer.should have_tag("form fieldset.#{@class_option}")
2770
+ end
2771
+ end
2772
+
2773
+ end
2774
+
2775
+ describe 'without a block' do
2776
+
2777
+ describe 'with no args (default buttons)' do
2778
+
2779
+ before do
2780
+ semantic_form_for(@new_post) do |builder|
2781
+ concat(builder.buttons)
2782
+ end
2783
+ end
2784
+
2785
+ it 'should render a form' do
2786
+ output_buffer.should have_tag('form')
2787
+ end
2788
+
2789
+ it 'should render a buttons fieldset inside the form' do
2790
+ output_buffer.should have_tag('form fieldset.buttons')
2791
+ end
2792
+
2793
+ it 'should not render a legend in the fieldset' do
2794
+ output_buffer.should_not have_tag('form fieldset.buttons legend')
2795
+ end
2796
+
2797
+ it 'should render an ol in the fieldset' do
2798
+ output_buffer.should have_tag('form fieldset.buttons ol')
2799
+ end
2800
+
2801
+ it 'should render a list item in the ol for each default button' do
2802
+ output_buffer.should have_tag('form fieldset.buttons ol li', :count => 1)
2803
+ end
2804
+
2805
+ it 'should render a commit list item for the commit button' do
2806
+ output_buffer.should have_tag('form fieldset.buttons ol li.commit')
2807
+ end
2808
+
2809
+ end
2810
+
2811
+ describe 'with button names as args' do
2812
+
2813
+ before do
2814
+ semantic_form_for(@new_post) do |builder|
2815
+ concat(builder.buttons(:commit))
2816
+ end
2817
+ end
2818
+
2819
+ it 'should render a form with a fieldset containing a list item for each button arg' do
2820
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
2821
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit')
2822
+ end
2823
+
2824
+ end
2825
+
2826
+ describe 'with button names as args and an options hash' do
2827
+
2828
+ before do
2829
+ semantic_form_for(@new_post) do |builder|
2830
+ concat(builder.buttons(:commit, :name => "Now click a button", :id => "my-id"))
2831
+ end
2832
+ end
2833
+
2834
+ it 'should render a form with a fieldset containing a list item for each button arg' do
2835
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
2836
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit', :count => 1)
2837
+ end
2838
+
2839
+ it 'should pass the options down to the fieldset' do
2840
+ output_buffer.should have_tag('form > fieldset#my-id.buttons')
2841
+ end
2842
+
2843
+ it 'should use the special :name option as a text for the legend tag' do
2844
+ output_buffer.should have_tag('form > fieldset#my-id.buttons > legend', /Now click a button/)
2845
+ end
2846
+
2847
+ end
2848
+
2849
+ end
2850
+
2851
+ end
2852
+
2853
+ describe '#commit_button' do
2854
+
2855
+ describe 'when used on any record' do
2856
+
2857
+ before do
2858
+ @new_post.stub!(:new_record?).and_return(false)
2859
+ semantic_form_for(@new_post) do |builder|
2860
+ concat(builder.commit_button)
2861
+ end
2862
+ end
2863
+
2864
+ it 'should render a commit li' do
2865
+ output_buffer.should have_tag('li.commit')
2866
+ end
2867
+
2868
+ it 'should render an input with a type attribute of "submit"' do
2869
+ output_buffer.should have_tag('li.commit input[@type="submit"]')
2870
+ end
2871
+
2872
+ it 'should render an input with a name attribute of "commit"' do
2873
+ output_buffer.should have_tag('li.commit input[@name="commit"]')
2874
+ end
2875
+
2876
+ it 'should pass options given in :button_html to the button' do
2877
+ @new_post.stub!(:new_record?).and_return(false)
2878
+ semantic_form_for(@new_post) do |builder|
2879
+ concat(builder.commit_button('text', :button_html => {:class => 'my_class', :id => 'my_id'}))
2880
+ end
2881
+
2882
+ output_buffer.should have_tag('li.commit input#my_id')
2883
+ output_buffer.should have_tag('li.commit input.my_class')
2884
+ end
2885
+
2886
+ end
2887
+
2888
+ describe 'when the first option is a string and the second is a hash' do
2889
+
2890
+ before do
2891
+ @new_post.stub!(:new_record?).and_return(false)
2892
+ semantic_form_for(@new_post) do |builder|
2893
+ concat(builder.commit_button("a string", :button_html => { :class => "pretty"}))
2894
+ end
2895
+ end
2896
+
2897
+ it "should render the string as the value of the button" do
2898
+ output_buffer.should have_tag('li input[@value="a string"]')
2899
+ end
2900
+
2901
+ it "should deal with the options hash" do
2902
+ output_buffer.should have_tag('li input.pretty')
2903
+ end
2904
+
2905
+ end
2906
+
2907
+ describe 'when the first option is a hash' do
2908
+
2909
+ before do
2910
+ @new_post.stub!(:new_record?).and_return(false)
2911
+ semantic_form_for(@new_post) do |builder|
2912
+ concat(builder.commit_button(:button_html => { :class => "pretty"}))
2913
+ end
2914
+ end
2915
+
2916
+ it "should deal with the options hash" do
2917
+ output_buffer.should have_tag('li input.pretty')
2918
+ end
2919
+
2920
+ end
2921
+
2922
+ describe 'when given the option "use_button_tag"' do
2923
+ before do
2924
+ @new_post.stub!(:new_record?).and_return(false)
2925
+ semantic_form_for(@new_post) do |builder|
2926
+ concat(builder.commit_button("Save It", :use_button_tag => true))
2927
+ end
2928
+ end
2929
+
2930
+ it "should render a button tag rather than input[type=submit]" do
2931
+ output_buffer.should have_tag('li button[@type="submit"]')
2932
+ output_buffer.should_not have_tag('li input[@type="submit"]')
2933
+ end
2934
+
2935
+ it "should have name=\"commit\" by default" do
2936
+ output_buffer.should have_tag('li button[@name"commit"]')
2937
+ end
2938
+
2939
+ it "should handle the value string correctly" do
2940
+ output_buffer.should have_tag('li button', /Save It/)
2941
+ end
2942
+ end
2943
+
2944
+ describe 'when using <button/> element with button_html hash' do
2945
+
2946
+ before do
2947
+ @new_post.stub!(:new_record?).and_return(false)
2948
+ @button_text = "Click to Save"
2949
+ semantic_form_for(@new_post) do |builder|
2950
+ concat(builder.commit_button(@button_text, :use_button_tag => true, :button_html => { :class => "pretty"}))
2951
+ end
2952
+ end
2953
+
2954
+ it "should deal with the options hash" do
2955
+ output_buffer.should have_tag('li button.pretty', /#{@button_text}/)
2956
+ end
2957
+
2958
+ end
2959
+
2960
+ describe 'when used on an existing record' do
2961
+
2962
+ it 'should render an input with a value attribute of "Save Post"' do
2963
+ @new_post.stub!(:new_record?).and_return(false)
2964
+ semantic_form_for(@new_post) do |builder|
2965
+ concat(builder.commit_button)
2966
+ end
2967
+ output_buffer.should have_tag('li.commit input[@value="Save Post"]')
2968
+ end
2969
+
2970
+ describe 'when the locale sets the label text' do
2971
+ before do
2972
+ I18n.backend.store_translations 'en', :formtastic => {:save => 'Save Changes To' }
2973
+ @new_post.stub!(:new_record?).and_return(false)
2974
+ semantic_form_for(@new_post) do |builder|
2975
+ concat(builder.commit_button)
2976
+ end
2977
+ end
2978
+
2979
+ after do
2980
+ I18n.backend.store_translations 'en', :formtastic => {:save => nil}
2981
+ end
2982
+
2983
+ it 'should allow translation of the labels' do
2984
+ output_buffer.should have_tag('li.commit input[@value="Save Changes To Post"]')
2985
+ end
2986
+ end
2987
+ end
2988
+
2989
+ describe 'when used on a new record' do
2990
+
2991
+ it 'should render an input with a value attribute of "Create Post"' do
2992
+ @new_post.stub!(:new_record?).and_return(true)
2993
+ semantic_form_for(@new_post) do |builder|
2994
+ concat(builder.commit_button)
2995
+ end
2996
+ output_buffer.should have_tag('li.commit input[@value="Create Post"]')
2997
+ end
2998
+
2999
+ describe 'when the locale sets the label text' do
3000
+ before do
3001
+ I18n.backend.store_translations 'en', :formtastic => {:create => 'Make' }
3002
+ semantic_form_for(@new_post) do |builder|
3003
+ concat(builder.commit_button)
3004
+ end
3005
+ end
3006
+
3007
+ after do
3008
+ I18n.backend.store_translations 'en', :formtastic => {:create => nil}
3009
+ end
3010
+
3011
+ it 'should allow translation of the labels' do
3012
+ output_buffer.should have_tag('li.commit input[@value="Make Post"]')
3013
+ end
3014
+ end
3015
+
3016
+ end
3017
+
3018
+ describe 'when used without object' do
3019
+
3020
+ it 'should render an input with a value attribute of "Submit"' do
3021
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
3022
+ concat(builder.commit_button)
3023
+ end
3024
+
3025
+ output_buffer.should have_tag('li.commit input[@value="Submit Project"]')
3026
+ end
3027
+
3028
+ describe 'when the locale sets the label text' do
3029
+ before do
3030
+ I18n.backend.store_translations 'en', :formtastic => { :submit => 'Send' }
3031
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
3032
+ concat(builder.commit_button)
3033
+ end
3034
+ end
3035
+
3036
+ after do
3037
+ I18n.backend.store_translations 'en', :formtastic => {:submit => nil}
3038
+ end
3039
+
3040
+ it 'should allow translation of the labels' do
3041
+ output_buffer.should have_tag('li.commit input[@value="Send Project"]')
3042
+ end
3043
+ end
3044
+
3045
+ end
3046
+
3047
+ end
3048
+
3049
+ end
3050
+
3051
+ end