18
18
19
19
class TestNestedAttributesInGeneral < ActiveRecord ::TestCase
20
20
teardown do
21
+ Pirate . nested_attributes_options . delete :ship
21
22
Pirate . accepts_nested_attributes_for :ship , allow_destroy : true , reject_if : proc ( &:empty? )
22
23
end
23
24
@@ -74,6 +75,7 @@ def test_should_raise_an_UnknownAttributeError_for_non_existing_nested_attribute
74
75
end
75
76
76
77
def test_should_disable_allow_destroy_by_default
78
+ Pirate . nested_attributes_options . delete :ship
77
79
Pirate . accepts_nested_attributes_for :ship
78
80
79
81
pirate = Pirate . create! ( catchphrase : "Don' botharrr talkin' like one, savvy?" )
@@ -92,6 +94,7 @@ def test_a_model_should_respond_to_underscore_destroy_and_return_if_it_is_marked
92
94
end
93
95
94
96
def test_reject_if_method_without_arguments
97
+ Pirate . nested_attributes_options . delete :ship
95
98
Pirate . accepts_nested_attributes_for :ship , reject_if : :new_record?
96
99
97
100
pirate = Pirate . new ( catchphrase : "Stop wastin' me time" )
@@ -100,6 +103,7 @@ def test_reject_if_method_without_arguments
100
103
end
101
104
102
105
def test_reject_if_method_with_arguments
106
+ Pirate . nested_attributes_options . delete :ship
103
107
Pirate . accepts_nested_attributes_for :ship , reject_if : :reject_empty_ships_on_create
104
108
105
109
pirate = Pirate . new ( catchphrase : "Stop wastin' me time" )
@@ -113,6 +117,7 @@ def test_reject_if_method_with_arguments
113
117
end
114
118
115
119
def test_reject_if_with_indifferent_keys
120
+ Pirate . nested_attributes_options . delete :ship
116
121
Pirate . accepts_nested_attributes_for :ship , reject_if : proc { |attributes | attributes [ :name ] . blank? }
117
122
118
123
pirate = Pirate . new ( catchphrase : "Stop wastin' me time" )
@@ -121,6 +126,7 @@ def test_reject_if_with_indifferent_keys
121
126
end
122
127
123
128
def test_reject_if_with_a_proc_which_returns_true_always_for_has_one
129
+ Pirate . nested_attributes_options . delete :ship
124
130
Pirate . accepts_nested_attributes_for :ship , reject_if : proc { |attributes | true }
125
131
pirate = Pirate . create ( catchphrase : "Stop wastin' me time" )
126
132
ship = pirate . create_ship ( name : "s1" )
@@ -143,6 +149,7 @@ def test_do_not_allow_assigning_foreign_key_when_reusing_existing_new_record
143
149
end
144
150
145
151
def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
152
+ Human . nested_attributes_options . delete :interests
146
153
Human . accepts_nested_attributes_for :interests , reject_if : proc { |attributes | true }
147
154
human = Human . create ( name : "John" )
148
155
interest = human . interests . create ( topic : "photography" )
@@ -151,6 +158,7 @@ def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
151
158
end
152
159
153
160
def test_destroy_works_independent_of_reject_if
161
+ Human . nested_attributes_options . delete :interests
154
162
Human . accepts_nested_attributes_for :interests , reject_if : proc { |attributes | true } , allow_destroy : true
155
163
human = Human . create ( name : "Jon" )
156
164
interest = human . interests . create ( topic : "the ladies" )
@@ -159,6 +167,7 @@ def test_destroy_works_independent_of_reject_if
159
167
end
160
168
161
169
def test_reject_if_is_not_short_circuited_if_allow_destroy_is_false
170
+ Pirate . nested_attributes_options . delete :ship
162
171
Pirate . accepts_nested_attributes_for :ship , reject_if : -> ( a ) { a [ :name ] == "The Golden Hind" } , allow_destroy : false
163
172
164
173
pirate = Pirate . create! ( catchphrase : "Stop wastin' me time" , ship_attributes : { name : "White Pearl" , _destroy : "1" } )
@@ -172,6 +181,7 @@ def test_reject_if_is_not_short_circuited_if_allow_destroy_is_false
172
181
end
173
182
174
183
def test_has_many_association_updating_a_single_record
184
+ Human . nested_attributes_options . delete ( :interests )
175
185
Human . accepts_nested_attributes_for ( :interests )
176
186
human = Human . create ( name : "John" )
177
187
interest = human . interests . create ( topic : "photography" )
@@ -180,6 +190,7 @@ def test_has_many_association_updating_a_single_record
180
190
end
181
191
182
192
def test_reject_if_with_blank_nested_attributes_id
193
+ Pirate . nested_attributes_options . delete :ship
183
194
# When using a select list to choose an existing 'ship' id, with include_blank: true
184
195
Pirate . accepts_nested_attributes_for :ship , reject_if : proc { |attributes | attributes [ :id ] . blank? }
185
196
@@ -189,6 +200,7 @@ def test_reject_if_with_blank_nested_attributes_id
189
200
end
190
201
191
202
def test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record
203
+ Human . nested_attributes_options . delete ( :interests )
192
204
Human . accepts_nested_attributes_for ( :interests )
193
205
human = Human . create ( name : "John" )
194
206
interest = human . interests . create topic : "gardening"
@@ -198,6 +210,7 @@ def test_first_and_array_index_zero_methods_return_the_same_value_when_nested_at
198
210
end
199
211
200
212
def test_allows_class_to_override_setter_and_call_super
213
+ Pirate . nested_attributes_options . delete :parrot
201
214
mean_pirate_class = Class . new ( Pirate ) do
202
215
accepts_nested_attributes_for :parrot
203
216
def parrot_attributes = ( attrs )
@@ -222,6 +235,7 @@ def test_accepts_nested_attributes_for_can_be_overridden_in_subclasses
222
235
end
223
236
224
237
def test_should_not_create_duplicates_with_create_with
238
+ Human . nested_attributes_options . delete ( :interests )
225
239
Human . accepts_nested_attributes_for ( :interests )
226
240
227
241
assert_difference ( "Interest.count" , 1 ) do
@@ -338,12 +352,14 @@ def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
338
352
end
339
353
340
354
def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
355
+ Pirate . nested_attributes_options . delete :ship
341
356
Pirate . accepts_nested_attributes_for :ship , allow_destroy : false , reject_if : proc ( &:empty? )
342
357
343
358
@pirate . update ( ship_attributes : { id : @pirate . ship . id , _destroy : "1" } )
344
359
345
360
assert_equal @ship , @pirate . reload . ship
346
361
362
+ Pirate . nested_attributes_options . delete :ship
347
363
Pirate . accepts_nested_attributes_for :ship , allow_destroy : true , reject_if : proc ( &:empty? )
348
364
end
349
365
@@ -411,6 +427,7 @@ def test_should_update_existing_when_update_only_is_true_and_id_is_given
411
427
end
412
428
413
429
def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
430
+ Pirate . nested_attributes_options . delete :update_only_ship
414
431
Pirate . accepts_nested_attributes_for :update_only_ship , update_only : true , allow_destroy : true
415
432
@ship . delete
416
433
@ship = @pirate . create_update_only_ship ( name : "Nights Dirty Lightning" )
@@ -420,6 +437,7 @@ def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is
420
437
assert_nil @pirate . reload . ship
421
438
assert_raise ( ActiveRecord ::RecordNotFound ) { Ship . find ( @ship . id ) }
422
439
440
+ Pirate . nested_attributes_options . delete :update_only_ship
423
441
Pirate . accepts_nested_attributes_for :update_only_ship , update_only : true , allow_destroy : false
424
442
end
425
443
end
@@ -532,11 +550,13 @@ def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
532
550
end
533
551
534
552
def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
553
+ Ship . nested_attributes_options . delete :pirate
535
554
Ship . accepts_nested_attributes_for :pirate , allow_destroy : false , reject_if : proc ( &:empty? )
536
555
537
556
@ship . update ( pirate_attributes : { id : @ship . pirate . id , _destroy : "1" } )
538
557
assert_nothing_raised { @ship . pirate . reload }
539
558
ensure
559
+ Ship . nested_attributes_options . delete :pirate
540
560
Ship . accepts_nested_attributes_for :pirate , allow_destroy : true , reject_if : proc ( &:empty? )
541
561
end
542
562
@@ -588,6 +608,7 @@ def test_should_update_existing_when_update_only_is_true_and_id_is_given
588
608
end
589
609
590
610
def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
611
+ Ship . nested_attributes_options . delete :update_only_pirate
591
612
Ship . accepts_nested_attributes_for :update_only_pirate , update_only : true , allow_destroy : true
592
613
@pirate . delete
593
614
@pirate = @ship . create_update_only_pirate ( catchphrase : "Aye" )
@@ -596,6 +617,7 @@ def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is
596
617
597
618
assert_raise ( ActiveRecord ::RecordNotFound ) { @pirate . reload }
598
619
620
+ Ship . nested_attributes_options . delete :update_only_pirate
599
621
Ship . accepts_nested_attributes_for :update_only_pirate , update_only : true , allow_destroy : false
600
622
end
601
623
end
@@ -820,6 +842,7 @@ def test_should_automatically_enable_autosave_on_the_association
820
842
end
821
843
822
844
def test_validate_presence_of_parent_works_with_inverse_of
845
+ Human . nested_attributes_options . delete ( :interests )
823
846
Human . accepts_nested_attributes_for ( :interests )
824
847
assert_equal :human , Human . reflect_on_association ( :interests ) . options [ :inverse_of ]
825
848
assert_equal :interests , Interest . reflect_on_association ( :human ) . options [ :inverse_of ]
@@ -842,6 +865,7 @@ def test_can_use_symbols_as_object_identifier
842
865
end
843
866
844
867
def test_numeric_column_changes_from_zero_to_no_empty_string
868
+ Human . nested_attributes_options . delete ( :interests )
845
869
Human . accepts_nested_attributes_for ( :interests )
846
870
847
871
repair_validations ( Interest ) do
@@ -909,6 +933,7 @@ def setup
909
933
910
934
module NestedAttributesLimitTests
911
935
def teardown
936
+ Pirate . nested_attributes_options . delete :parrots
912
937
Pirate . accepts_nested_attributes_for :parrots , allow_destroy : true , reject_if : proc ( &:empty? )
913
938
end
914
939
@@ -933,6 +958,7 @@ def test_limit_with_exceeding_records
933
958
934
959
class TestNestedAttributesLimitNumeric < ActiveRecord ::TestCase
935
960
def setup
961
+ Pirate . nested_attributes_options . delete :parrots
936
962
Pirate . accepts_nested_attributes_for :parrots , limit : 2
937
963
938
964
@pirate = Pirate . create! ( catchphrase : "Don' botharrr talkin' like one, savvy?" )
@@ -943,6 +969,7 @@ def setup
943
969
944
970
class TestNestedAttributesLimitSymbol < ActiveRecord ::TestCase
945
971
def setup
972
+ Pirate . nested_attributes_options . delete :parrots
946
973
Pirate . accepts_nested_attributes_for :parrots , limit : :parrots_limit
947
974
948
975
@pirate = Pirate . create! ( catchphrase : "Don' botharrr talkin' like one, savvy?" , parrots_limit : 2 )
@@ -953,6 +980,7 @@ def setup
953
980
954
981
class TestNestedAttributesLimitProc < ActiveRecord ::TestCase
955
982
def setup
983
+ Pirate . nested_attributes_options . delete :parrots
956
984
Pirate . accepts_nested_attributes_for :parrots , limit : proc { 2 }
957
985
958
986
@pirate = Pirate . create! ( catchphrase : "Don' botharrr talkin' like one, savvy?" )
@@ -965,6 +993,7 @@ class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase
965
993
fixtures :owners , :pets
966
994
967
995
def setup
996
+ Owner . nested_attributes_options . delete :pets
968
997
Owner . accepts_nested_attributes_for :pets , allow_destroy : true
969
998
970
999
@owner = owners ( :ashley )
@@ -1132,3 +1161,22 @@ def test_should_build_a_new_record_based_on_the_delegated_type
1132
1161
assert_equal "Hello world!" , @entry . entryable . subject
1133
1162
end
1134
1163
end
1164
+
1165
+ class TestPreDeclaredNestedAttributesAssociation < ActiveRecord ::TestCase
1166
+ setup do
1167
+ assert @current_options = Developer . nested_attributes_options [ :projects ]
1168
+ end
1169
+
1170
+ def test_should_raise_an_argument_error_with_similar_options
1171
+ assert_raises ArgumentError do
1172
+ Developer . accepts_nested_attributes_for :projects , **@current_options
1173
+ end
1174
+ end
1175
+
1176
+ def test_should_raise_an_argument_error_with_varying_options
1177
+ assert_equal false , @current_options [ :update_only ]
1178
+ assert_raises ArgumentError do
1179
+ Developer . accepts_nested_attributes_for :projects , **@current_options . merge ( update_only : true )
1180
+ end
1181
+ end
1182
+ end
0 commit comments