Skip to content

Commit

Permalink
Add test to verify that the new :inverse_of association option will i…
Browse files Browse the repository at this point in the history
…ndeed fix the validation problem for a belongs_to relationship that validates_presence_of the parent, when both the parent and the child are new (in-memory) records. Also check that this works when the parents adds child via nested_attributes_for.

Lastly, add a require 'models/pet' to association_validation_test.rb, so that test can be run independently (was failing due to that missing dependency). [#2815 status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Wolfram Arnold authored and josevalim committed Aug 8, 2009
1 parent fbe6c3c commit 73f2d37
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
37 changes: 37 additions & 0 deletions activerecord/test/cases/nested_attributes_test.rb
Expand Up @@ -4,6 +4,8 @@
require "models/bird"
require "models/parrot"
require "models/treasure"
require "models/man"
require "models/interest"
require 'active_support/hash_with_indifferent_access'

module AssertRaiseWithMessage
Expand Down Expand Up @@ -470,6 +472,41 @@ def test_should_automatically_enable_autosave_on_the_association
assert Pirate.reflect_on_association(@association_name).options[:autosave]
end

def test_validate_presence_of_parent__works_with_inverse_of
Man.accepts_nested_attributes_for(:interests)
assert_equal :man, Man.reflect_on_association(:interests).options[:inverse_of]
assert_equal :interests, Interest.reflect_on_association(:man).options[:inverse_of]

repair_validations(Interest) do
Interest.validates_presence_of(:man)
assert_difference 'Man.count' do
assert_difference 'Interest.count', 2 do
man = Man.create!(:name => 'John',
:interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
assert_equal 2, man.interests.count
end
end
end
end

def test_validate_presence_of_parent__fails_without_inverse_of
Man.accepts_nested_attributes_for(:interests)
Man.reflect_on_association(:interests).options.delete(:inverse_of)
Interest.reflect_on_association(:man).options.delete(:inverse_of)

repair_validations(Interest) do
Interest.validates_presence_of(:man)
assert_no_difference ['Man.count', 'Interest.count'] do
man = Man.create(:name => 'John',
:interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
assert !man.errors[:interests_man].empty?
end
end
# restore :inverse_of
Man.reflect_on_association(:interests).options[:inverse_of] = :man
Interest.reflect_on_association(:man).options[:inverse_of] = :interests
end

private

def association_setter
Expand Down
23 changes: 23 additions & 0 deletions activerecord/test/cases/validations/association_validation_test.rb
Expand Up @@ -3,6 +3,9 @@
require 'models/topic'
require 'models/reply'
require 'models/owner'
require 'models/pet'
require 'models/man'
require 'models/interest'

class AssociationValidationTest < ActiveRecord::TestCase
fixtures :topics, :owners
Expand Down Expand Up @@ -98,4 +101,24 @@ def test_validates_size_of_association_utf8
end
end
end

def test_validates_presence_of_belongs_to_association__parent_is_new_record
repair_validations(Interest) do
# Note that Interest and Man have the :inverse_of option set
Interest.validates_presence_of(:man)
man = Man.new(:name => 'John')
interest = man.interests.build(:topic => 'Airplanes')
assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a man object associated"
end
end

def test_validates_presence_of_belongs_to_association__existing_parent
repair_validations(Interest) do
Interest.validates_presence_of(:man)
man = Man.create!(:name => 'John')
interest = man.interests.build(:topic => 'Airplanes')
assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a man object associated"
end
end

end

1 comment on commit 73f2d37

@mislav
Copy link
Member

@mislav mislav commented on 73f2d37 Aug 8, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read tips for good commit message formatting http://www.tpope.net/node/106

Please sign in to comment.