forked from formtastic/formtastic
/
formtastic_spec.rb
2501 lines (1984 loc) · 95.4 KB
/
formtastic_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require File.dirname(__FILE__) + '/test_helper'
require 'formtastic'
module FormtasticSpecHelper
def should_have_maxlength_matching_column_limit(method_name, as, column_type)
semantic_form_for(@new_post) do |builder|
concat(builder.input(method_name, :as => as))
end
@new_post.column_for_attribute(method_name).limit.should == 50
output_buffer.should have_tag("form li input[@maxlength='#{@new_post.column_for_attribute(method_name).limit}']")
end
def should_use_default_text_size_for_columns_longer_than_default(method_name, as, column_type)
default_size = Formtastic::SemanticFormBuilder.default_text_field_size
column_limit_larger_than_default = default_size * 2
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => column_type, :limit => column_limit_larger_than_default))
semantic_form_for(@new_post) do |builder|
concat(builder.input(method_name, :as => as))
end
output_buffer.should have_tag("form li input[@size='#{default_size}']")
end
def should_use_the_column_size_for_columns_shorter_than_default(method_name, as, column_type)
default_size = Formtastic::SemanticFormBuilder.default_text_field_size
column_limit_shorter_than_default = 1
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => column_type, :limit => column_limit_shorter_than_default))
semantic_form_for(@new_post) do |builder|
concat(builder.input(method_name, :as => as))
end
output_buffer.should have_tag("form li input[@size='#{column_limit_shorter_than_default}']")
end
def should_use_default_size_for_methods_without_columns(as)
default_size = Formtastic::SemanticFormBuilder.default_text_field_size
@new_post.stub!(:method_without_column)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:method_without_column, :as => as))
end
output_buffer.should have_tag("form li input[@size='#{default_size}']")
end
def default_input_type(column_type, column_name = :generic_column_name)
@new_post.stub!(column_name)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => column_type)) unless column_type.nil?
semantic_form_for(@new_post) do |builder|
@default_type = builder.send(:default_input_type, @new_post, column_name)
end
return @default_type
end
def attachment_input_type(method, column_name = :generic_column_name)
@new_post.stub!(:column_for_attribute).and_return(nil)
column = mock('column')
[:file?, :public_filename].each do |test|
column.stub!(:respond_to?).with(test).and_return(method == test)
end
@new_post.should_receive(column_name).and_return(column)
semantic_form_for(@new_post) do |builder|
@default_type = builder.send(:default_input_type, @new_post, column_name)
end
return @default_type
end
end
describe 'Formtastic' do
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::ActiveRecordHelper
include ActionView::Helpers::RecordIdentificationHelper
include ActionView::Helpers::DateHelper
include ActionView::Helpers::CaptureHelper
include ActiveSupport
include ActionController::PolymorphicRoutes
include Formtastic::SemanticFormHelper
attr_accessor :output_buffer
def protect_against_forgery?; false; end
before do
Formtastic::SemanticFormBuilder.label_str_method = :titleize
@output_buffer = ''
# Resource-oriented styles like form_for(@post) will expect a path method for the object,
# so we're defining some here.
def post_path(o); "/posts/1"; end
def posts_path; "/posts"; end
def new_post_path; "/posts/new"; end
def author_path(o); "/authors/1"; end
def authors_path; "/authors"; end
def new_author_path; "/authors/new"; end
# Sometimes we need some classes
class Post;
def id; end
end
class Author; end
@fred = mock('user')
@fred.stub!(:class).and_return(Author)
@fred.stub!(:to_label).and_return('Fred Smith')
@fred.stub!(:login).and_return('fred_smith')
@fred.stub!(:id).and_return(37)
@fred.stub!(:new_record?).and_return(false)
@fred.stub!(:errors).and_return(mock('errors', :on => nil))
@bob = mock('user')
@bob.stub!(:class).and_return(Author)
@bob.stub!(:to_label).and_return('Bob Rock')
@bob.stub!(:login).and_return('bob')
@bob.stub!(:id).and_return(42)
@bob.stub!(:posts).and_return([])
@bob.stub!(:post_ids).and_return([])
@bob.stub!(:new_record?).and_return(false)
@bob.stub!(:errors).and_return(mock('errors', :on => nil))
Author.stub!(:find).and_return([@fred, @bob])
Author.stub!(:human_attribute_name).and_return { |column_name| column_name.to_s }
Author.stub!(:human_name).and_return('Author')
Author.stub!(:reflect_on_all_validations).and_return([])
Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :klass => Post, :macro => :has_many) if column_name == :posts }
# Sometimes we need a mock @post object and some Authors for belongs_to
@new_post = mock('post')
@new_post.stub!(:class).and_return(Post)
@new_post.stub!(:id).and_return(nil)
@new_post.stub!(:new_record?).and_return(true)
@new_post.stub!(:errors).and_return(mock('errors', :on => nil))
@new_post.stub!(:author).and_return(nil)
@freds_post = mock('post')
@freds_post.stub!(:class).and_return(Post)
@freds_post.stub!(:to_label).and_return('Fred Smith')
@freds_post.stub!(:id).and_return(19)
@freds_post.stub!(:author).and_return(@fred)
@freds_post.stub!(:author_id).and_return(@fred.id)
@freds_post.stub!(:authors).and_return([@fred])
@freds_post.stub!(:author_ids).and_return([@fred.id])
@freds_post.stub!(:new_record?).and_return(false)
@freds_post.stub!(:errors).and_return(mock('errors', :on => nil))
@fred.stub!(:posts).and_return([@freds_post])
@fred.stub!(:post_ids).and_return([@freds_post.id])
Post.stub!(:human_attribute_name).and_return { |column_name| column_name.to_s }
Post.stub!(:human_name).and_return('Post')
Post.stub!(:reflect_on_all_validations).and_return([])
Post.stub!(:reflect_on_association).and_return do |column_name|
case column_name
when :author
mock('reflection', :klass => Author, :macro => :belongs_to)
when :authors
mock('reflection', :klass => Author, :macro => :has_and_belongs_to_many)
end
end
Post.stub!(:find).and_return([@freds_post])
end
describe 'JustinFrench::Formtastic::SemanticFormBuilder' do
require 'justin_french/formtastic'
it 'should be deprecated' do
::ActiveSupport::Deprecation.should_receive(:warn).with(/JustinFrench\:\:Formtastic\:\:SemanticFormBuilder/, anything())
form_for(@new_post, :builder => JustinFrench::Formtastic::SemanticFormBuilder) do |builder|
end
end
end
describe 'SemanticFormHelper' do
describe '#semantic_form_for' do
it 'yields an instance of SemanticFormBuilder' do
semantic_form_for(:post, Post.new, :url => '/hello') do |builder|
builder.class.should == Formtastic::SemanticFormBuilder
end
end
it 'adds a class of "formtastic" to the generated form' do
semantic_form_for(:post, Post.new, :url => '/hello') do |builder|
end
output_buffer.should have_tag("form.formtastic")
end
it 'adds class matching the object\'s class to the generated form when a symbol is provided' do
semantic_form_for(:post, Post.new, :url => '/hello') do |builder|
end
output_buffer.should have_tag("form.post")
end
it 'adds class matching the object\'s class to the generated form when a symbol is provided when an object is provided' do
semantic_form_for(@new_post) do |builder|
end
output_buffer.should have_tag("form.post")
end
describe 'allows :html options' do
before(:each) do
semantic_form_for(:post, Post.new, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
end
end
it 'to add a id of "something-special" to generated form' do
output_buffer.should have_tag("form#something-special")
end
it 'to add a class of "something-extra" to generated form' do
output_buffer.should have_tag("form.something-extra")
end
it 'to add enctype="multipart/form-data"' do
output_buffer.should have_tag('form[@enctype="multipart/form-data"]')
end
end
it 'can be called with a resource-oriented style' do
semantic_form_for(@new_post) do |builder|
builder.object.class.should == Post
builder.object_name.should == "post"
end
end
it 'can be called with a resource-oriented style with an inline object' do
semantic_form_for(Post.new) do |builder|
builder.object.class.should == Post
builder.object_name.should == "post"
end
end
it 'can be called with a generic style and instance variable' do
semantic_form_for(:post, @new_post, :url => new_post_path) do |builder|
builder.object.class.should == Post
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
end
end
it 'can be called with a generic style and inline object' do
semantic_form_for(:post, Post.new, :url => new_post_path) do |builder|
builder.object.class.should == Post
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
end
end
end
describe '#semantic_fields_for' do
it 'yields an instance of SemanticFormBuilder' do
semantic_fields_for(:post, Post.new, :url => '/hello') do |builder|
builder.class.should == Formtastic::SemanticFormBuilder
end
end
end
describe '#semantic_form_remote_for' do
it 'yields an instance of SemanticFormBuilder' do
semantic_form_remote_for(:post, Post.new, :url => '/hello') do |builder|
builder.class.should == Formtastic::SemanticFormBuilder
end
end
end
describe '#semantic_form_for_remote' do
it 'yields an instance of SemanticFormBuilder' do
semantic_form_remote_for(:post, Post.new, :url => '/hello') do |builder|
builder.class.should == Formtastic::SemanticFormBuilder
end
end
end
end
describe 'SemanticFormBuilder' do
include FormtasticSpecHelper
describe "@@builder" do
before do
@new_post.stub!(:title)
@new_post.stub!(:body)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
end
it "can be overridden" do
class CustomFormBuilder < Formtastic::SemanticFormBuilder
def custom(arg1, arg2, options = {})
[arg1, arg2, options]
end
end
Formtastic::SemanticFormHelper.builder = CustomFormBuilder
semantic_form_for(@new_post) do |builder|
builder.class.should == CustomFormBuilder
builder.custom("one", "two").should == ["one", "two", {}]
end
end
end
describe 'Formtastic::SemanticFormBuilder#semantic_fields_for' do
before do
@new_post.stub!(:author).and_return(Author.new)
end
it 'yields an instance of SemanticFormBuilder' do
semantic_form_for(@new_post) do |builder|
builder.semantic_fields_for(:author) do |nested_builder|
nested_builder.class.should == Formtastic::SemanticFormBuilder
end
end
end
it 'nests the object name' do
semantic_form_for(@new_post) do |builder|
builder.semantic_fields_for(@bob) do |nested_builder|
nested_builder.object_name.should == 'post[author]'
end
end
end
it 'should generate html id for li tag properly' do
@bob.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
semantic_form_for(@new_post) do |builder|
builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
concat(nested_builder.inputs :login)
end
end
output_buffer.should have_tag('form fieldset.inputs #post_author_1_login_input')
output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input')
end
end
describe '#input' do
before do
@new_post.stub!(:title)
@new_post.stub!(:body)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
end
describe 'with inline order customization' do
it 'should allow input, hints, errors as order' do
Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
semantic_form_for(@new_post) do |builder|
builder.should_receive(:string_input).once.ordered
builder.should_receive(:inline_hints).once.ordered
builder.should_receive(:inline_errors).once.ordered
concat(builder.input(:title))
end
end
it 'should allow hints, input, errors as order' do
Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
semantic_form_for(@new_post) do |builder|
builder.should_receive(:inline_hints).once.ordered
builder.should_receive(:string_input).once.ordered
builder.should_receive(:inline_errors).once.ordered
concat(builder.input(:title))
end
end
end
describe 'arguments and options' do
it 'should require the first argument (the method on form\'s object)' do
lambda {
semantic_form_for(@new_post) do |builder|
concat(builder.input()) # no args passed in at all
end
}.should raise_error(ArgumentError)
end
it 'should raise a helpful error when the object does not respond to the method supplied in the first argument' do
semantic_form_for(@new_post) do |builder|
lambda { builder.input(:method_on_post_that_doesnt_exist) }.should raise_error(NoMethodError, /method_on_post_that_doesnt_exist/)
end
end
describe ':required option' do
describe 'when true' do
before do
@string = Formtastic::SemanticFormBuilder.required_string = " required yo!" # ensure there's something in the string
@new_post.class.should_not_receive(:reflect_on_all_validations)
end
after do
Formtastic::SemanticFormBuilder.required_string = %{<abbr title="required">*</abbr>}
end
it 'should set a "required" class' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :required => true))
end
output_buffer.should_not have_tag('form li.optional')
output_buffer.should have_tag('form li.required')
end
it 'should append the "required" string to the label' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :required => true))
end
output_buffer.should have_tag('form li.required label', /#{@string}$/)
end
end
describe 'when false' do
before do
@string = Formtastic::SemanticFormBuilder.optional_string = " optional yo!" # ensure there's something in the string
@new_post.class.should_not_receive(:reflect_on_all_validations)
end
after do
Formtastic::SemanticFormBuilder.optional_string = ''
end
it 'should set an "optional" class' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :required => false))
end
output_buffer.should_not have_tag('form li.required')
output_buffer.should have_tag('form li.optional')
end
it 'should append the "optional" string to the label' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :required => false))
end
output_buffer.should have_tag('form li.optional label', /#{@string}$/)
end
end
describe 'when not provided' do
describe 'and the validation reflection plugin is available' do
before do
@new_post.class.stub!(:method_defined?).with(:reflect_on_all_validations).and_return(true)
end
describe 'and validates_presence_of was called for the method' do
before do
@new_post.class.should_receive(:reflect_on_all_validations).and_return([
mock('MacroReflection', :macro => :validates_presence_of, :name => :title)
])
end
it 'should be required' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
output_buffer.should have_tag('form li.required')
output_buffer.should_not have_tag('form li.optional')
end
end
describe 'and validates_presence_of was not called for the method' do
before do
@new_post.class.should_receive(:reflect_on_all_validations).and_return([])
end
it 'should not be required' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
output_buffer.should_not have_tag('form li.required')
output_buffer.should have_tag('form li.optional')
end
end
end
describe 'and the validation reflection plugin is not available' do
it 'should use the default value' do
Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
output_buffer.should_not have_tag('form li.required')
output_buffer.should have_tag('form li.optional')
end
end
end
end
describe ':as option' do
describe 'when not provided' do
it 'should default to a string for methods that don\'t have a column in the database (and can\'t be guessed)' do
@new_post.stub!(:method_without_a_database_column)
@new_post.stub!(:column_for_attribute).and_return(nil)
default_input_type(nil, :method_without_a_database_column).should == :string
end
it 'should default to a string for methods on objects that don\'t respond to "column_for_attribute"' do
@new_post.stub!(:method_without_a_database_column)
# @new_post.stub!(:column_for_attribute).and_raise(NoMethodError)
default_input_type(nil, :method_without_a_database_column).should == :string
end
it 'should default to :password for methods that don\'t have a column in the database but "password" is in the method name' do
@new_post.stub!(:password_method_without_a_database_column)
@new_post.stub!(:column_for_attribute).and_return(nil)
default_input_type(nil, :password_method_without_a_database_column).should == :password
end
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
@new_post.stub!(:password_method_without_a_database_column)
@new_post.stub!(:column_for_attribute).and_return(nil)
default_input_type(nil, :password_method_without_a_database_column).should == :password
end
it 'should default to :select for column names ending in "_id"' do
default_input_type(:integer, :user_id).should == :select
default_input_type(:integer, :section_id).should == :select
end
it 'should default to :password for :string column types with "password" in the method name' do
default_input_type(:string, :password).should == :password
default_input_type(:string, :hashed_password).should == :password
default_input_type(:string, :password_hash).should == :password
end
it 'should default to :text for :text column types' do
default_input_type(:text).should == :text
end
it 'should default to :date for :date column types' do
default_input_type(:date).should == :date
end
it 'should default to :datetime for :datetime and :timestamp column types' do
default_input_type(:datetime).should == :datetime
default_input_type(:timestamp).should == :datetime
end
it 'should default to :time for :time column types' do
default_input_type(:time).should == :time
end
it 'should default to :boolean for :boolean column types' do
default_input_type(:boolean).should == :boolean
end
it 'should default to :string for :string column types' do
default_input_type(:string).should == :string
end
it 'should default to :numeric for :integer, :float and :decimal column types' do
default_input_type(:integer).should == :numeric
default_input_type(:float).should == :numeric
default_input_type(:decimal).should == :numeric
end
describe 'defaulting to file column' do
it 'should default to :file for attributes that respond to #file?' do
attachment_input_type(:file?).should == :file
end
it 'should default to :file for attributes that respond to #public_filename' do
attachment_input_type(:public_filename).should == :file
end
end
end
it 'should call the corresponding input method' do
[:select, :radio, :password, :text, :date, :datetime, :time, :boolean, :boolean_select, :string, :numeric, :file].each do |input_style|
@new_post.stub!(:generic_column_name)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
semantic_form_for(@new_post) do |builder|
builder.should_receive(:"#{input_style}_input").once.and_return("fake HTML output from #input")
concat(builder.input(:generic_column_name, :as => input_style))
end
end
end
end
describe ':label option' do
describe 'when provided' do
it 'should be passed down to the label tag' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :label => "Kustom"))
end
output_buffer.should have_tag("form li label", /Kustom/)
end
end
describe 'when not provided' do
describe 'when the default is :titleize' do
before do
Formtastic::SemanticFormBuilder.label_str_method = :titleize
@new_post.stub!(:meta_description) # a two word method name
semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description))
end
end
it 'should default the titleized method name, passing it down to the label tag' do
output_buffer.should have_tag("form li label", /#{'meta_description'.titleize}/)
end
end
describe 'when the default is :humanize' do
before do
Formtastic::SemanticFormBuilder.label_str_method = :humanize
@new_post.stub!(:meta_description) # a two word method name
semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description))
end
end
it 'should default the humanized method name, passing it down to the label tag' do
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
end
end
describe 'when the default is :to_s' do
before do
Formtastic::SemanticFormBuilder.label_str_method = :to_s
@new_post.stub!(:meta_description) # a two word method name
semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description))
end
end
it 'should default the humanized method name, passing it down to the label tag' do
output_buffer.should have_tag("form li label", /meta_description/)
end
end
end
end
describe ':hint option' do
describe 'when provided' do
it 'should be passed down to the paragraph tag' do
hint_text = "this is the title of the post"
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :hint => hint_text))
end
output_buffer.should have_tag("form li p.inline-hints", hint_text)
end
end
describe 'when not provided' do
it 'should not render a hint paragraph' do
hint_text = "this is the title of the post"
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
output_buffer.should_not have_tag("form li p.inline-hints")
end
end
end
end
describe ':as any type of input' do
it 'should create a list item for each input' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
concat(builder.input(:body))
end
output_buffer.should have_tag('form li', :count => 2)
end
describe 'when there are errors on the object for this method' do
before do
@title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome']
@errors = mock('errors')
@errors.stub!(:on).with('title').and_return(@title_errors)
@new_post.stub!(:errors).and_return(@errors)
end
it 'should apply an errors class to the list item' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
output_buffer.should have_tag('form li.error')
end
it 'should not wrap the input with the Rails default error wrapping' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
output_buffer.should_not have_tag('div.fieldWithErrors')
end
describe 'and the errors will be displayed as a sentence' do
before do
Formtastic::SemanticFormBuilder.inline_errors = :sentence
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
end
it 'should render a paragraph with the errors joined into a sentence' do
output_buffer.should have_tag('form li.error p.inline-errors', @title_errors.to_sentence)
end
end
describe 'and the errors will be displayed as a list' do
before do
Formtastic::SemanticFormBuilder.inline_errors = :list
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
end
it 'should render an unordered list with the class errors' do
output_buffer.should have_tag('form li.error ul.errors')
end
it 'should include a list element for each of the errors within the unordered list' do
@title_errors.each do |error|
output_buffer.should have_tag('form li.error ul.errors li', error)
end
end
end
describe 'but the errors will not be shown' do
before do
Formtastic::SemanticFormBuilder.inline_errors = :none
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
end
it 'should not display an error sentence' do
output_buffer.should_not have_tag('form li.error p.inline-errors')
end
it 'should not display an error list' do
output_buffer.should_not have_tag('form li.error ul.errors')
end
end
end
describe 'when there are no errors on the object for this method' do
before do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
end
end
it 'should not apply an errors class to the list item' do
output_buffer.should_not have_tag('form li.error')
end
it 'should not render a paragraph for the errors' do
output_buffer.should_not have_tag('form li.error p.inline-errors')
end
it 'should not display an error list' do
output_buffer.should_not have_tag('form li.error ul.errors')
end
end
end
describe ':as => :string' do
before do
@new_post.stub!(:title)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 50))
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :as => :string))
end
end
it 'should have a string class on the wrapper' do
output_buffer.should have_tag('form li.string')
end
it 'should have a post_title_input id on the wrapper' do
output_buffer.should have_tag('form li#post_title_input')
end
it 'should generate a label for the input' do
output_buffer.should have_tag('form li label')
output_buffer.should have_tag('form li label[@for="post_title"')
output_buffer.should have_tag('form li label', /Title/)
end
it 'should generate a text input' do
output_buffer.should have_tag('form li input')
output_buffer.should have_tag('form li input#post_title')
output_buffer.should have_tag('form li input[@type="text"]')
output_buffer.should have_tag('form li input[@name="post[title]"]')
end
it 'should have a maxlength matching the column limit' do
should_have_maxlength_matching_column_limit(:title, :string, :string)
end
it 'should use default_text_field_size for columns longer than default_text_field_size' do
should_use_default_text_size_for_columns_longer_than_default(:title, :string, :string)
end
it 'should use the column size for columns shorter than default_text_field_size' do
should_use_the_column_size_for_columns_shorter_than_default(:title, :string, :string)
end
it 'should use default_text_field_size for methods without database columns' do
should_use_default_size_for_methods_without_columns(:string)
end
describe "with object that does not respond to 'column_for_attribute'" do
before do
@new_post.stub!(:column_for_attribute).and_raise(NoMethodError)
end
it "should have a maxlength of default_text_field_size" do
should_use_default_size_for_methods_without_columns(:string)
end
end
end
describe 'for belongs_to associations' do
before do
@new_post.stub!(:author).and_return(@bob)
@new_post.stub!(:author_id).and_return(@bob.id)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
end
describe ':as => :radio' do
describe 'when using association_id' do
before do
# Check for deprecation message
::ActiveSupport::Deprecation.should_receive(:warn).with(/association/, anything())
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author_id, :as => :radio))
end
end
it 'should have a radio class on the wrapper' do
output_buffer.should have_tag('form li.radio')
end
it 'should have a post_author_id_input id on the wrapper' do
output_buffer.should have_tag('form li#post_author_id_input')
end
it 'should generate a fieldset and legend containing label text for the input' do
output_buffer.should have_tag('form li fieldset')
output_buffer.should have_tag('form li fieldset legend')
output_buffer.should have_tag('form li fieldset legend', /Author/)
end
it 'should generate an ordered list with a list item for each choice' do
output_buffer.should have_tag('form li fieldset ol')
output_buffer.should have_tag('form li fieldset ol li', :count => Author.find(:all).size)
end
it 'should have one option with a "selected" attribute' do
output_buffer.should have_tag('form li input[@checked]', :count => 1)
end
describe "each choice" do
it 'should contain a label for the radio input with a nested input and label text' do
Author.find(:all).each do |author|
output_buffer.should have_tag('form li fieldset ol li label')
output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']")
output_buffer.should have_tag("form li fieldset ol li label input")
end
end
it "should have a radio input" do
Author.find(:all).each do |author|
output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}")
output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']")
end
end
xit "should mark input as checked if it's the the existing choice" do
@new_post.author_id.should == @bob.id
@new_post.author.id.should == @bob.id
@new_post.author.should == @bob
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author_id, :as => :radio))
end
output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
end
end
end
describe 'when using association' do
before do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :radio))
end
end
it 'should have a radio class on the wrapper' do
output_buffer.should have_tag('form li.radio')
end
it 'should have a post_author_id_input id on the wrapper' do
output_buffer.should have_tag('form li#post_author_input')
end
it 'should generate a fieldset and legend containing label text for the input' do
output_buffer.should have_tag('form li fieldset')
output_buffer.should have_tag('form li fieldset legend')
output_buffer.should have_tag('form li fieldset legend', /Author/)
end
it 'should generate an ordered list with a list item for each choice' do
output_buffer.should have_tag('form li fieldset ol')
output_buffer.should have_tag('form li fieldset ol li', :count => Author.find(:all).size)
end
it 'should have one option with a "selected" attribute' do
output_buffer.should have_tag('form li input[@checked]', :count => 1)
end
describe "each choice" do
it 'should contain a label for the radio input with a nested input and label text' do
Author.find(:all).each do |author|
output_buffer.should have_tag('form li fieldset ol li label')
output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']")
output_buffer.should have_tag("form li fieldset ol li label input")
end
end
it "should have a radio input" do
Author.find(:all).each do |author|
output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}")
output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']")
end
end
xit "should mark input as checked if it's the the existing choice" do
@new_post.author_id.should == @bob.id
@new_post.author.id.should == @bob.id
@new_post.author.should == @bob
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author_id, :as => :radio))
end