@@ -1307,6 +1307,18 @@ def test_create_with_array
1307
1307
green_birds . each { |bird | assert_predicate bird , :persisted? }
1308
1308
end
1309
1309
1310
+ def test_create_with_block
1311
+ sparrow = Bird . create do |bird |
1312
+ bird . name = "sparrow"
1313
+ bird . color = "grey"
1314
+ end
1315
+
1316
+ assert_kind_of Bird , sparrow
1317
+ assert_predicate sparrow , :persisted?
1318
+ assert_equal "sparrow" , sparrow . name
1319
+ assert_equal "grey" , sparrow . color
1320
+ end
1321
+
1310
1322
def test_create_bang_with_array
1311
1323
green_birds = Bird . where ( color : "green" ) . create! ( [ { name : "parrot" } , { name : "canary" } ] )
1312
1324
assert_equal [ "parrot" , "canary" ] , green_birds . map ( &:name )
@@ -1481,6 +1493,19 @@ def test_find_or_create_by_with_create_with
1481
1493
assert_equal bird , Bird . create_with ( color : "blue" ) . find_or_create_by ( name : "bob" )
1482
1494
end
1483
1495
1496
+ def test_find_or_create_by_with_block
1497
+ assert_nil Bird . find_by ( name : "bob" )
1498
+
1499
+ bird = Bird . find_or_create_by ( name : "bob" ) do |record |
1500
+ record . color = "blue"
1501
+ end
1502
+ assert_predicate bird , :persisted?
1503
+ assert_equal bird . name , "bob"
1504
+ assert_equal bird . color , "blue"
1505
+
1506
+ assert_equal bird , Bird . find_or_create_by ( name : "bob" , color : "blue" )
1507
+ end
1508
+
1484
1509
def test_find_or_create_by!
1485
1510
assert_raises ( ActiveRecord ::RecordInvalid ) { Bird . find_or_create_by! ( color : "green" ) }
1486
1511
end
@@ -1494,6 +1519,20 @@ def test_create_or_find_by
1494
1519
assert_not_equal subscriber , Subscriber . create_or_find_by ( nick : "cat" )
1495
1520
end
1496
1521
1522
+ def test_create_or_find_by_with_block
1523
+ assert_nil Subscriber . find_by ( nick : "bob" )
1524
+
1525
+ subscriber = Subscriber . create_or_find_by ( nick : "bob" ) do |record |
1526
+ record . name = "the builder"
1527
+ end
1528
+
1529
+ assert_equal "bob" , subscriber . nick
1530
+ assert_equal "the builder" , subscriber . name
1531
+ assert_predicate subscriber , :persisted?
1532
+ assert_equal subscriber , Subscriber . create_or_find_by ( nick : "bob" )
1533
+ assert_not_equal subscriber , Subscriber . create_or_find_by ( nick : "cat" )
1534
+ end
1535
+
1497
1536
def test_create_or_find_by_should_not_raise_due_to_validation_errors
1498
1537
assert_nothing_raised do
1499
1538
bird = Bird . create_or_find_by ( color : "green" )
@@ -1562,6 +1601,20 @@ def test_find_or_initialize_by
1562
1601
assert_equal bird , Bird . find_or_initialize_by ( name : "bob" )
1563
1602
end
1564
1603
1604
+ def test_find_or_initialize_by_with_block
1605
+ assert_nil Bird . find_by ( name : "bob" )
1606
+
1607
+ bird = Bird . find_or_initialize_by ( name : "bob" ) do |record |
1608
+ record . color = "blue"
1609
+ end
1610
+ assert_predicate bird , :new_record?
1611
+ assert_equal bird . name , "bob"
1612
+ assert_equal bird . color , "blue"
1613
+ bird . save!
1614
+
1615
+ assert_equal bird , Bird . find_or_initialize_by ( name : "bob" , color : "blue" )
1616
+ end
1617
+
1565
1618
def test_explicit_create_with
1566
1619
hens = Bird . where ( name : "hen" )
1567
1620
assert_equal "hen" , hens . new . name
0 commit comments