|
4 | 4 | require "models/bird"
|
5 | 5 | require "models/parrot"
|
6 | 6 | require "models/treasure"
|
| 7 | +require "models/man" |
| 8 | +require "models/interest" |
7 | 9 | require 'active_support/hash_with_indifferent_access'
|
8 | 10 |
|
9 | 11 | module AssertRaiseWithMessage
|
@@ -470,6 +472,41 @@ def test_should_automatically_enable_autosave_on_the_association
|
470 | 472 | assert Pirate.reflect_on_association(@association_name).options[:autosave]
|
471 | 473 | end
|
472 | 474 |
|
| 475 | + def test_validate_presence_of_parent__works_with_inverse_of |
| 476 | + Man.accepts_nested_attributes_for(:interests) |
| 477 | + assert_equal :man, Man.reflect_on_association(:interests).options[:inverse_of] |
| 478 | + assert_equal :interests, Interest.reflect_on_association(:man).options[:inverse_of] |
| 479 | + |
| 480 | + repair_validations(Interest) do |
| 481 | + Interest.validates_presence_of(:man) |
| 482 | + assert_difference 'Man.count' do |
| 483 | + assert_difference 'Interest.count', 2 do |
| 484 | + man = Man.create!(:name => 'John', |
| 485 | + :interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}]) |
| 486 | + assert_equal 2, man.interests.count |
| 487 | + end |
| 488 | + end |
| 489 | + end |
| 490 | + end |
| 491 | + |
| 492 | + def test_validate_presence_of_parent__fails_without_inverse_of |
| 493 | + Man.accepts_nested_attributes_for(:interests) |
| 494 | + Man.reflect_on_association(:interests).options.delete(:inverse_of) |
| 495 | + Interest.reflect_on_association(:man).options.delete(:inverse_of) |
| 496 | + |
| 497 | + repair_validations(Interest) do |
| 498 | + Interest.validates_presence_of(:man) |
| 499 | + assert_no_difference ['Man.count', 'Interest.count'] do |
| 500 | + man = Man.create(:name => 'John', |
| 501 | + :interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}]) |
| 502 | + assert !man.errors[:interests_man].empty? |
| 503 | + end |
| 504 | + end |
| 505 | + # restore :inverse_of |
| 506 | + Man.reflect_on_association(:interests).options[:inverse_of] = :man |
| 507 | + Interest.reflect_on_association(:man).options[:inverse_of] = :interests |
| 508 | + end |
| 509 | + |
473 | 510 | private
|
474 | 511 |
|
475 | 512 | def association_setter
|
|
0 commit comments