From f4cd52798b2cc1e4bf952b38712180e64cc91d51 Mon Sep 17 00:00:00 2001 From: Ruy Asan Date: Thu, 5 Jun 2008 14:30:27 -0700 Subject: [PATCH] Ensure that save on child object fails for invalid belongs_to association. TAKE TWO! - now with more unit tests! (which pass anyway for some reason!) --- activerecord/lib/active_record/associations.rb | 4 ++- .../associations/belongs_to_associations_test.rb | 26 ++++++++++++++++++++ activerecord/test/fixtures/juliet.yml | 3 ++ activerecord/test/fixtures/romeo.yml | 3 ++ activerecord/test/models/circular_belonging.rb | 12 +++++++++ activerecord/test/schema/schema.rb | 8 ++++++ 6 files changed, 55 insertions(+), 1 deletions(-) create mode 100644 activerecord/test/fixtures/juliet.yml create mode 100644 activerecord/test/fixtures/romeo.yml create mode 100644 activerecord/test/models/circular_belonging.rb diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index a3d1bbb..b041fb1 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -936,7 +936,9 @@ module ActiveRecord "#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)" ) end - + + add_single_associated_save_callbacks(reflection.name) + configure_dependency_for_belongs_to(reflection) end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index e0da8bf..db5c639 100755 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -1,6 +1,7 @@ require "cases/helper" require 'models/developer' require 'models/project' +require 'models/circular_belonging' require 'models/company' require 'models/topic' require 'models/reply' @@ -383,6 +384,31 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! } assert companies(:first_client).readonly_firm.readonly? end + + def test_save_fails_for_invalid_belongs_to + assert log = AuditLog.create(:developer_id=>0,:message=>"") + + log.developer = Developer.new + + assert !log.developer.valid? + assert !log.valid? + assert !log.save + assert_equal "is invalid", log.errors.on("developer") + end + + # This is supposed to test against the problem described here: + # http://groups.google.com/group/rubyonrails-core/browse_thread/thread/652d0d7e6d455c08 + # (which reverted this patch the first time) + # However, I cannot reproduce the problem. Perhaps changes in the core + # have made it go away? + def test_circular_save_for_belongs_to + assert romeo = Romeo.create(:montague=>true) + romeo.juliet = Juliet.new + + assert !romeo.valid? + assert !romeo.save + assert_equal "is invalid", romeo.errors.on("juliet") + end def test_polymorphic_assignment_foreign_type_field_updating # should update when assigning a saved record diff --git a/activerecord/test/fixtures/juliet.yml b/activerecord/test/fixtures/juliet.yml new file mode 100644 index 0000000..0989343 --- /dev/null +++ b/activerecord/test/fixtures/juliet.yml @@ -0,0 +1,3 @@ +juliet: + id:1 + romeo_id:1 \ No newline at end of file diff --git a/activerecord/test/fixtures/romeo.yml b/activerecord/test/fixtures/romeo.yml new file mode 100644 index 0000000..ea727e4 --- /dev/null +++ b/activerecord/test/fixtures/romeo.yml @@ -0,0 +1,3 @@ +romeo: + id:1 + juliet_id:1 \ No newline at end of file diff --git a/activerecord/test/models/circular_belonging.rb b/activerecord/test/models/circular_belonging.rb new file mode 100644 index 0000000..30895f8 --- /dev/null +++ b/activerecord/test/models/circular_belonging.rb @@ -0,0 +1,12 @@ +class Romeo < ActiveRecord::Base + attr_accessor :montague + validates_presence_of :montague + has_many :juliets, :dependent=>:destroy + belongs_to :juliet +end + +class Juliet < ActiveRecord::Base + attr_accessor :capulet + validates_presence_of :capulet + belongs_to :romeo +end \ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 423929f..5932f37 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -166,6 +166,10 @@ ActiveRecord::Schema.define do create_table :jobs, :force => true do |t| t.integer :ideal_reference_id end + + create_table :juliets, :force => true do |t| + t.integer :romeo_id + end create_table :keyboards, :force => true, :id => false do |t| t.primary_key :key_number @@ -321,6 +325,10 @@ ActiveRecord::Schema.define do t.integer :post_id, :null => false t.integer :person_id, :null => false end + + create_table :romeos, :force => true do |t| + t.integer :juliet_id + end create_table :shape_expressions, :force => true do |t| t.string :paint_type -- 1.5.5.1 From 54cb6ce3d6ffdd25245ec74229f6f2e8b1fb7c9d Mon Sep 17 00:00:00 2001 From: Ruy Asan Date: Wed, 11 Jun 2008 19:25:10 -0700 Subject: [PATCH] added circular validatio to test models --- activerecord/test/models/circular_belonging.rb | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/activerecord/test/models/circular_belonging.rb b/activerecord/test/models/circular_belonging.rb index 30895f8..1e94e6b 100644 --- a/activerecord/test/models/circular_belonging.rb +++ b/activerecord/test/models/circular_belonging.rb @@ -2,11 +2,11 @@ class Romeo < ActiveRecord::Base attr_accessor :montague validates_presence_of :montague has_many :juliets, :dependent=>:destroy - belongs_to :juliet + belongs_to :juliet, :validate=>true end class Juliet < ActiveRecord::Base attr_accessor :capulet validates_presence_of :capulet - belongs_to :romeo + belongs_to :romeo, :validate=>true end \ No newline at end of file -- 1.5.5.1