/
rails_admin_config_edit_spec.rb
1025 lines (921 loc) · 33 KB
/
rails_admin_config_edit_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
# coding: utf-8
require 'spec_helper'
describe "RailsAdmin Config DSL Edit Section" do
subject { page }
describe " a field with 'format' as a name (Kernel function)" do
it "is updatable without any error" do
RailsAdmin.config FieldTest do
edit do
field :format
end
end
visit new_path(:model_name => "field_test")
fill_in "field_test[format]", :with => "test for format"
click_button "Save"
@record = RailsAdmin::AbstractModel.new("FieldTest").first
expect(@record.format).to eq("test for format")
end
end
describe "default_value" do
it "is set for all types of input fields" do
RailsAdmin.config do |config|
config.excluded_models = []
config.model(FieldTest) do
field :string_field do
default_value 'string_field default_value'
end
field :text_field do
default_value 'string_field text_field'
end
field :boolean_field do
default_value true
end
field :date_field do
default_value Date.today
end
end
end
visit new_path(:model_name => "field_test")
# In Rails 3.2.3 behavior of textarea has changed to insert newline after the opening tag,
# but Capybara's RackTest driver is not up to this behavior change.
# (https://github.com/jnicklas/capybara/issues/677)
# So we manually cut off first newline character as a workaround here.
expect(find_field('field_test[string_field]').value.gsub(/^\n/, '')).to eq('string_field default_value')
expect(find_field('field_test[text_field]').value.gsub(/^\n/, '')).to eq('string_field text_field')
expect(find_field('field_test[date_field]').value).to eq(Date.today.to_s)
expect(has_checked_field?('field_test[boolean_field]')).to be_true
end
it "sets default value for selects" do
RailsAdmin.config(Team) do
field :color, :enum do
default_value 'black'
enum do
['black', 'white']
end
end
end
visit new_path(:model_name => "team")
expect(find_field('team[color]').value).to eq('black')
end
end
describe "attr_accessible" do
it "is configurable in the controller scope" do
RailsAdmin.config do |config|
config.excluded_models = []
config.attr_accessible_role do
_current_user.attr_accessible_role # sould be :custom_role
end
config.model FieldTest do
edit do
field :string_field
field :restricted_field
field :protected_field
end
end
end
visit new_path(:model_name => "field_test")
fill_in "field_test[string_field]", :with => "No problem here"
fill_in "field_test[restricted_field]", :with => "I'm allowed to do that as :custom_role only"
should have_no_selector "field_test[protected_field]"
click_button "Save"
@field_test = FieldTest.first
expect(@field_test.string_field).to eq("No problem here")
expect(@field_test.restricted_field).to eq("I'm allowed to do that as :custom_role only")
end
end
describe "css hooks" do
it "is present" do
visit new_path(:model_name => "team")
should have_selector("#team_division_id_field.belongs_to_association_type.division_field")
end
end
describe "field groupings" do
it "is hideable" do
RailsAdmin.config Team do
edit do
group :default do
label "Hidden group"
hide
end
end
end
visit new_path(:model_name => "team")
# Should not have the group header
should have_no_selector("legend", :text => "Hidden Group")
# Should not have any of the group's fields either
should have_no_selector("select#team_division")
should have_no_selector("input#team_name")
should have_no_selector("input#team_logo_url")
should have_no_selector("input#team_manager")
should have_no_selector("input#team_ballpark")
should have_no_selector("input#team_mascot")
should have_no_selector("input#team_founded")
should have_no_selector("input#team_wins")
should have_no_selector("input#team_losses")
should have_no_selector("input#team_win_percentage")
should have_no_selector("input#team_revenue")
end
it "hides association groupings" do
RailsAdmin.config Team do
edit do
group :players do
label "Players"
field :players
hide
end
end
end
visit new_path(:model_name => "team")
# Should not have the group header
should have_no_selector("legend", :text => "Players")
# Should not have any of the group's fields either
should have_no_selector("select#team_player_ids")
end
it "is renameable" do
RailsAdmin.config Team do
edit do
group :default do
label "Renamed group"
end
end
end
visit new_path(:model_name => "team")
should have_selector("legend", :text => "Renamed group")
end
describe "help" do
before(:each) do
class HelpTest < Tableless
column :name, 'string(50)'
column :division, :string
end
RailsAdmin.config.included_models = [HelpTest, Team]
end
after(:each) do
# restore validation setting
HelpTest._validators[:name] = []
HelpTest.reset_callbacks(:validate)
end
context "using mongoid", :skip_active_record => true do
it "uses the db column size for the maximum length" do
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length up to 255.")
end
it "returns nil for the maximum length" do
visit new_path(:model_name => "team")
expect(find("#team_custom_field_field .help-block")).not_to have_content("Length")
end
end
context "using active_record", :skip_mongoid => true do
it "uses the db column size for the maximum length" do
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length up to 50.")
end
it "uses the :minimum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :minimum => 1
end
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length of 1-50.")
end
it "uses the minimum of db column size or :maximum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :maximum => 51
end
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length up to 50.")
end
end
it "shows help section if present" do
RailsAdmin.config HelpTest do
edit do
group :default do
help "help paragraph to display"
end
end
end
visit new_path(:model_name => "help_test")
should have_selector('fieldset>p', :text => "help paragraph to display")
end
it "does not show help if not present" do
RailsAdmin.config HelpTest do
edit do
group :default do
label 'no help'
end
end
end
visit new_path(:model_name => "help_test")
should_not have_selector('fieldset>p')
end
it "is able to display multiple help if there are multiple sections" do
RailsAdmin.config HelpTest do
edit do
group :default do
field :name
help 'help for default'
end
group :other_section do
label "Other Section"
field :division
help 'help for other section'
end
end
end
visit new_path(:model_name => "help_test")
should have_selector("fieldset>p", :text => 'help for default')
should have_selector("fieldset>p", :text => 'help for other section')
should have_selector("fieldset>p", :count => 2)
end
it "uses the :is setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :is => 3
end
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length of 3.")
end
it "uses the :maximum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :maximum => 49
end
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length up to 49.")
end
it "uses the :minimum and :maximum from the validation" do
HelpTest.class_eval do
validates_length_of :name, :minimum => 1, :maximum => 49
end
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length of 1-49.")
end
it "uses the range from the validation" do
HelpTest.class_eval do
validates_length_of :name, :in => 1..49
end
visit new_path(:model_name => "help_test")
expect(find("#help_test_name_field .help-block")).to have_content("Length of 1-49.")
end
end
it "has accessor for its fields" do
RailsAdmin.config Team do
edit do
group :default do
field :name
field :logo_url
end
group :belongs_to_associations do
label "Belong's to associations"
field :division
end
group :basic_info do
field :manager
end
end
end
visit new_path(:model_name => "team")
should have_selector("legend", :text => "Basic info")
all("legend", :text => "Basic info").tap do |nodes|
expect(nodes.count).to eq(2)
expect(nodes.first.visible?).to be_false
expect(nodes.last.visible?).to be_true
end
should have_selector("legend", :text => "Belong's to associations")
should have_selector("label", :text => "Name")
should have_selector("label", :text => "Logo url")
should have_selector("label", :text => "Division")
should have_selector(".control-group", :count => 4)
end
it "has accessor for its fields by type" do
RailsAdmin.config Team do
edit do
group :default do
field :name
field :logo_url
end
group :other do
field :division_id
field :manager
field :ballpark
fields_of_type :string do
label { "#{label} (STRING)" }
end
end
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Name")
should have_selector("label", :text => "Logo url")
should have_selector("label", :text => "Division")
should have_selector("label", :text => "Manager (STRING)")
should have_selector("label", :text => "Ballpark (STRING)")
end
end
describe "items' fields" do
it "shows all by default" do
visit new_path(:model_name => "team")
should have_selector("select#team_division_id")
should have_selector("input#team_name")
should have_selector("input#team_logo_url")
should have_selector("input#team_manager")
should have_selector("input#team_ballpark")
should have_selector("input#team_mascot")
should have_selector("input#team_founded")
should have_selector("input#team_wins")
should have_selector("input#team_losses")
should have_selector("input#team_win_percentage")
should have_selector("input#team_revenue")
should have_selector("select#team_player_ids")
should have_selector("select#team_fan_ids")
end
it "appears in order defined" do
RailsAdmin.config Team do
edit do
field :manager
field :division
field :name
end
end
visit new_path(:model_name => "team")
should have_selector(:xpath, "//*[contains(@class, 'field')][1]//*[@id='team_manager']")
should have_selector(:xpath, "//*[contains(@class, 'field')][2]//*[@id='team_division_id']")
should have_selector(:xpath, "//*[contains(@class, 'field')][3]//*[@id='team_name']")
end
it "only shows the defined fields if some fields are defined" do
RailsAdmin.config Team do
edit do
field :division
field :name
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Division")
should have_selector("label", :text => "Name")
should have_selector(".control-group", :count => 2)
end
describe "I18n awarly" do
after :each do
I18n.locale = :en
end
it "delegates the label option to the ActiveModel API and memoizes it" do
RailsAdmin.config Team do
edit do
field :manager
field :fans
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Team Manager")
should have_selector("label", :text => "Some Fans")
I18n.locale = :fr
visit new_path(:model_name => "team")
should have_selector("label", :text => "Manager de l'équipe")
should have_selector("label", :text => "Quelques fans")
end
end
it "is renameable" do
RailsAdmin.config Team do
edit do
field :manager do
label "Renamed field"
end
field :division
field :name
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Renamed field")
should have_selector("label", :text => "Division")
should have_selector("label", :text => "Name")
end
it "is renameable by type" do
RailsAdmin.config Team do
edit do
fields_of_type :string do
label { "#{label} (STRING)" }
end
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Division")
should have_selector("label", :text => "Name (STRING)")
should have_selector("label", :text => "Logo url (STRING)")
should have_selector("label", :text => "Manager (STRING)")
should have_selector("label", :text => "Ballpark (STRING)")
should have_selector("label", :text => "Mascot (STRING)")
should have_selector("label", :text => "Founded")
should have_selector("label", :text => "Wins")
should have_selector("label", :text => "Losses")
should have_selector("label", :text => "Win percentage")
should have_selector("label", :text => "Revenue")
should have_selector("label", :text => "Players")
should have_selector("label", :text => "Fans")
end
it "is globally renameable by type" do
RailsAdmin.config Team do
edit do
fields_of_type :string do
label { "#{label} (STRING)" }
end
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Division")
should have_selector("label", :text => "Name (STRING)")
should have_selector("label", :text => "Logo url (STRING)")
should have_selector("label", :text => "Manager (STRING)")
should have_selector("label", :text => "Ballpark (STRING)")
should have_selector("label", :text => "Mascot (STRING)")
should have_selector("label", :text => "Founded")
should have_selector("label", :text => "Wins")
should have_selector("label", :text => "Losses")
should have_selector("label", :text => "Win percentage")
should have_selector("label", :text => "Revenue")
should have_selector("label", :text => "Players")
should have_selector("label", :text => "Fans")
end
it "is flaggable as read only and be configurable with formatted_value" do
RailsAdmin.config Team do
edit do
field :name do
read_only true
formatted_value do
"I'm outputed in the form"
end
end
end
end
visit new_path(:model_name => "team")
should have_content("I'm outputed in the form")
end
it "is hideable" do
RailsAdmin.config Team do
edit do
field :manager do
hide
end
field :division
field :name
end
end
visit new_path(:model_name => "team")
should have_no_selector("#team_manager")
should have_selector("#team_division_id")
should have_selector("#team_name")
end
it "is hideable by type" do
RailsAdmin.config Team do
edit do
fields_of_type :string do
hide
end
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Division")
should have_no_selector("label", :text => "Name")
should have_no_selector("label", :text => "Logo url")
should have_no_selector("label", :text => "Manager")
should have_no_selector("label", :text => "Ballpark")
should have_no_selector("label", :text => "Mascot")
should have_selector("label", :text => "Founded")
should have_selector("label", :text => "Wins")
should have_selector("label", :text => "Losses")
should have_selector("label", :text => "Win percentage")
should have_selector("label", :text => "Revenue")
should have_selector("label", :text => "Players")
should have_selector("label", :text => "Fans")
end
it "is globally hideable by type" do
RailsAdmin.config Team do
edit do
fields_of_type :string do
hide
end
end
end
visit new_path(:model_name => "team")
should have_selector("label", :text => "Division")
should have_no_selector("label", :text => "Name")
should have_no_selector("label", :text => "Logo url")
should have_no_selector("label", :text => "Manager")
should have_no_selector("label", :text => "Ballpark")
should have_no_selector("label", :text => "Mascot")
should have_selector("label", :text => "Founded")
should have_selector("label", :text => "Wins")
should have_selector("label", :text => "Losses")
should have_selector("label", :text => "Win percentage")
should have_selector("label", :text => "Revenue")
should have_selector("label", :text => "Players")
should have_selector("label", :text => "Fans")
end
it "has option to customize the help text" do
RailsAdmin.config Team do
edit do
field :manager do
help "#{help} Additional help text for manager field."
end
field :division
field :name
end
end
visit new_path(:model_name => "team")
expect(find("#team_manager_field .help-block")).to have_content("Required. Length up to 100. Additional help text for manager field.")
expect(find("#team_division_id_field .help-block")).to have_content("Required")
expect(find("#team_name_field .help-block")).not_to have_content("Additional help text")
end
it "has option to override required status" do
RailsAdmin.config Team do
edit do
field :manager do
optional true
end
field :division do
optional true
end
field :name do
required true
end
end
end
visit new_path(:model_name => "team")
expect(find("#team_manager_field .help-block")).to have_content("Optional")
expect(find("#team_division_id_field .help-block")).to have_content("Optional")
expect(find("#team_name_field .help-block")).to have_content("Required")
end
end
describe "bindings" do
it "is present at creation time" do
RailsAdmin.config do |config|
config.excluded_models = []
end
RailsAdmin.config Category do
field :parent_category do
visible do
!bindings[:object].new_record?
end
end
end
visit new_path(:model_name => 'category')
should have_no_css('#category_parent_category_id')
click_button 'Save'
visit edit_path(:model_name => 'category', :id => Category.first)
should have_css('#category_parent_category_id')
click_button 'Save'
should have_content('Category successfully updated')
end
end
describe "nested form" do
it "works" do
@record = FactoryGirl.create :field_test
@record.nested_field_tests = [NestedFieldTest.create!(:title => 'title 1'), NestedFieldTest.create!(:title => 'title 2')]
visit edit_path(:model_name => "field_test", :id => @record.id)
fill_in "field_test_comment_attributes_content", :with => 'nested comment content'
fill_in "field_test_nested_field_tests_attributes_0_title", :with => 'nested field test title 1 edited'
page.find('#field_test_nested_field_tests_attributes_1__destroy').set('true')
click_button "Save"
@record.reload
expect(@record.comment.content).to eq('nested comment content')
expect(@record.nested_field_tests.length).to eq(1)
expect(@record.nested_field_tests[0].title).to eq('nested field test title 1 edited')
end
it "sets bindings[:object] to nested object" do
RailsAdmin.config(NestedFieldTest) do
nested do
field :title do
label do
bindings[:object].class.name
end
end
end
end
@record = FieldTest.create
@record.nested_field_tests << NestedFieldTest.create!(:title => 'title 1')
visit edit_path(:model_name => "field_test", :id => @record.id)
expect(find('#field_test_nested_field_tests_attributes_0_title_field')).to have_content('NestedFieldTest')
end
it "is desactivable" do
visit new_path(:model_name => "field_test")
should have_selector('#field_test_nested_field_tests_attributes_field .add_nested_fields')
RailsAdmin.config(FieldTest) do
configure :nested_field_tests do
nested_form false
end
end
visit new_path(:model_name => "field_test")
should have_no_selector('#field_test_nested_field_tests_attributes_field .add_nested_fields')
end
describe "with nested_attributes_options given" do
before do
FieldTest.nested_attributes_options.stub(:[]).with(any_args()).
and_return({:allow_destroy=>true, :update_only=>false})
end
it "does not show add button when :update_only is true" do
FieldTest.nested_attributes_options.stub(:[]).with(:nested_field_tests).
and_return({:allow_destroy=>true, :update_only=>true})
visit new_path(:model_name => "field_test")
should have_selector('.toggler')
should_not have_selector('#field_test_nested_field_tests_attributes_field .add_nested_fields')
end
it "does not show destroy button except for newly created when :allow_destroy is false" do
@record = FieldTest.create
@record.nested_field_tests << NestedFieldTest.create!(:title => 'nested title 1')
FieldTest.nested_attributes_options.stub(:[]).with(:nested_field_tests).
and_return({:allow_destroy=>false, :update_only=>false})
visit edit_path(:model_name => "field_test", :id => @record.id)
expect(find('#field_test_nested_field_tests_attributes_0_title').value).to eq('nested title 1')
should_not have_selector('form .remove_nested_fields')
should have_selector('[id$="_fields_blueprint"] .remove_nested_fields')
end
end
describe "when a field which have the same name of nested_in field's" do
it "does not hide fields which is not associated with nesting parent field's model" do
visit new_path(:model_name => "field_test")
should_not have_selector('select#field_test_nested_field_tests_attributes_new_nested_field_tests_field_test_id')
should have_selector('select#field_test_nested_field_tests_attributes_new_nested_field_tests_another_field_test_id')
end
end
end
describe "embedded model", :mongoid => true do
it "works" do
@record = FactoryGirl.create :field_test
2.times.each{|i| @record.embeds.create :name => "embed #{i}"}
visit edit_path(:model_name => "field_test", :id => @record.id)
fill_in "field_test_embeds_attributes_0_name", :with => 'embed 1 edited'
page.find('#field_test_embeds_attributes_1__destroy').set('true')
click_button "Save"
@record.reload
expect(@record.embeds.length).to eq(1)
expect(@record.embeds[0].name).to eq('embed 1 edited')
end
end
describe "fields which are nullable and have AR validations", :active_record => true do
it "is required" do
# draft.notes is nullable and has no validation
field = RailsAdmin::config("Draft").edit.fields.find{|f| f.name == :notes}
expect(field.properties[:nullable?]).to be_true
expect(field.required?).to be_false
# draft.date is nullable in the schema but has an AR
# validates_presence_of validation that makes it required
field = RailsAdmin::config("Draft").edit.fields.find{|f| f.name == :date}
expect(field.properties[:nullable?]).to be_true
expect(field.required?).to be_true
# draft.round is nullable in the schema but has an AR
# validates_numericality_of validation that makes it required
field = RailsAdmin::config("Draft").edit.fields.find{|f| f.name == :round}
expect(field.properties[:nullable?]).to be_true
expect(field.required?).to be_true
# team.revenue is nullable in the schema but has an AR
# validates_numericality_of validation that allows nil
field = RailsAdmin::config("Team").edit.fields.find{|f| f.name == :revenue}
expect(field.properties[:nullable?]).to be_true
expect(field.required?).to be_false
end
end
describe "CKEditor Support" do
it "starts with CKEditor disabled" do
field = RailsAdmin::config("Draft").edit.fields.find{|f| f.name == :notes}
expect(field.ckeditor).to be_false
end
it "adds Javascript to enable CKEditor" do
RailsAdmin.config Draft do
edit do
field :notes do
ckeditor true
end
end
end
visit new_path(:model_name => "draft")
should have_selector('textarea#draft_notes[data-richtext="ckeditor"]')
end
end
describe "CodeMirror Support" do
it "starts with CodeMirror disabled" do
field = RailsAdmin::config("Draft").edit.fields.find{|f| f.name == :notes}
expect(field.codemirror).to be_false
end
it "adds Javascript to enable CodeMirror" do
RailsAdmin.config Draft do
edit do
field :notes do
codemirror true
end
end
end
visit new_path(:model_name => "draft")
should have_selector('textarea#draft_notes[data-richtext="codemirror"]')
end
end
describe "bootstrap_wysihtml5 Support" do
it "starts with bootstrap_wysihtml5 disabled" do
field = RailsAdmin::config("Draft").edit.fields.find{|f| f.name == :notes}
expect(field.bootstrap_wysihtml5).to be_false
end
it "adds Javascript to enable bootstrap_wysihtml5" do
RailsAdmin.config Draft do
edit do
field :notes do
bootstrap_wysihtml5 true
end
end
end
visit new_path(:model_name => "draft")
should have_selector('textarea#draft_notes[data-richtext="bootstrap-wysihtml5"]')
end
it "should include custom wysihtml5 configuration" do
RailsAdmin.config Draft do
edit do
field :notes do
bootstrap_wysihtml5 true
bootstrap_wysihtml5_config_options :image => false
bootstrap_wysihtml5_css_location 'stub_css.css'
bootstrap_wysihtml5_js_location 'stub_js.js'
end
end
end
visit new_path(:model_name => "draft")
should have_selector("textarea#draft_notes[data-richtext=\"bootstrap-wysihtml5\"][data-options]")
end
end
describe "Paperclip Support" do
it "shows a file upload field" do
RailsAdmin.config User do
edit do
field :avatar
end
end
visit new_path(:model_name => "user")
should have_selector("input#user_avatar")
end
end
describe "Enum field support" do
describe "when object responds to '\#{method}_enum'" do
before do
Team.class_eval do
def color_enum
["blue", "green", "red"]
end
end
RailsAdmin.config Team do
edit do
field :color
end
end
visit new_path(:model_name => "team")
end
after do
Team.send(:remove_method, :color_enum)
end
it "auto-detects enumeration" do
should have_selector(".enum_type select")
should_not have_selector(".enum_type select[multiple]")
should have_content("green")
end
end
describe "when class responds to '\#{method}_enum'" do
before do
Team.instance_eval do
def color_enum
["blue", "green", "red"]
end
end
RailsAdmin.config Team do
edit do
field :color
end
end
visit new_path(:model_name => "team")
end
after do
Team.instance_eval { undef :color_enum }
end
it "auto-detects enumeration" do
should have_selector(".enum_type select")
should have_content("green")
end
end
describe "the enum instance method" do
before do
Team.class_eval do
def color_list
["blue", "green", "red"]
end
end
RailsAdmin.config Team do
edit do
field :color, :enum do
enum_method :color_list
end
end
end
visit new_path(:model_name => "team")
end
after do
Team.send(:remove_method, :color_list)
end
it "allows configuration" do
should have_selector(".enum_type select")
should have_content("green")
end
end
describe "the enum class method" do
before do
Team.instance_eval do
def color_list
["blue", "green", "red"]
end
end
RailsAdmin.config Team do
edit do
field :color, :enum do
enum_method :color_list
end
end
end
visit new_path(:model_name => "team")
end
after do
Team.instance_eval { undef :color_list }
end
it "allows configuration" do
should have_selector(".enum_type select")
should have_content("green")
end
end
describe "when overriding enum configuration" do
before do
Team.class_eval do
def color_list
["blue", "green", "red"]
end
end
RailsAdmin.config Team do
edit do
field :color, :enum do
enum_method :color_list
enum do
["yellow", "black"]
end
end
end
end
visit new_path(:model_name => "team")
end
after do
Team.send(:remove_method, :color_list)
end
it "allows direct listing of enumeration options and override enum method" do
should have_selector(".enum_type select")
should have_no_content("green")
should have_content("yellow")
end
end
describe "when serialize is enabled in ActiveRecord model", :active_record => true do
before do
Team.instance_eval do
serialize :color
def color_enum
["blue", "green", "red"]
end
end
visit new_path(:model_name => "team")
end
after do
Team.serialized_attributes.clear
Team.instance_eval { undef :color_enum }
end
it "makes enumeration multi-selectable" do
should have_selector(".enum_type select[multiple]")
end
end
describe "when serialize is enabled in Mongoid model", :mongoid => true do
before do
Team.instance_eval do
field :color, :type => Array
def color_enum
["blue", "green", "red"]
end
end
visit new_path(:model_name => "team")
end