From 5b86c3e5bb2bb54003d8f211b46a7b992355dbf5 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 22 Oct 2010 10:28:53 +0200 Subject: has_one maintains the association with separate after_create/after_update This way parent models can get their own after_create and after_update callbacks fired after has_one has done its job. --- .../test/cases/autosave_association_test.rb | 20 ++++++++++++ activerecord/test/models/eye.rb | 37 ++++++++++++++++++++++ activerecord/test/schema/schema.rb | 8 +++++ 3 files changed, 65 insertions(+) create mode 100644 activerecord/test/models/eye.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 52382f5afc..89be94c81f 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -17,6 +17,7 @@ require 'models/tag' require 'models/tagging' require 'models/treasure' require 'models/company' +require 'models/eye' class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase def test_autosave_should_be_a_valid_option_for_has_one @@ -170,6 +171,25 @@ class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCas firm.account = Account.find(:first).clone assert_queries(2) { firm.save! } end + + def test_callbacks_firing_order_on_create + eye = Eye.create(:iris_attributes => {:color => 'honey'}) + assert_equal [true, false], eye.after_create_callbacks_stack + end + + def test_callbacks_firing_order_on_update + eye = Eye.create(:iris_attributes => {:color => 'honey'}) + eye.update_attributes(:iris_attributes => {:color => 'green'}) + assert_equal [true, false], eye.after_update_callbacks_stack + end + + def test_callbacks_firing_order_on_save + eye = Eye.create(:iris_attributes => {:color => 'honey'}) + assert_equal [false, false], eye.after_save_callbacks_stack + + eye.update_attributes(:iris_attributes => {:color => 'blue'}) + assert_equal [false, false, false, false], eye.after_save_callbacks_stack + end end class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase diff --git a/activerecord/test/models/eye.rb b/activerecord/test/models/eye.rb new file mode 100644 index 0000000000..77f17b578e --- /dev/null +++ b/activerecord/test/models/eye.rb @@ -0,0 +1,37 @@ +class Eye < ActiveRecord::Base + attr_reader :after_create_callbacks_stack + attr_reader :after_update_callbacks_stack + attr_reader :after_save_callbacks_stack + + # Callbacks configured before the ones has_one sets up. + after_create :trace_after_create + after_update :trace_after_update + after_save :trace_after_save + + has_one :iris + accepts_nested_attributes_for :iris + + # Callbacks configured after the ones has_one sets up. + after_create :trace_after_create2 + after_update :trace_after_update2 + after_save :trace_after_save2 + + def trace_after_create + (@after_create_callbacks_stack ||= []) << iris.new_record? + end + alias trace_after_create2 trace_after_create + + def trace_after_update + (@after_update_callbacks_stack ||= []) << iris.changed? + end + alias trace_after_update2 trace_after_update + + def trace_after_save + (@after_save_callbacks_stack ||= []) << iris.changed? + end + alias trace_after_save2 trace_after_save +end + +class Iris < ActiveRecord::Base + belongs_to :eye +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index ea62833d81..fe59d8aeec 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -156,6 +156,11 @@ ActiveRecord::Schema.define do t.integer :company_id end + create_table :iris, :force => true do |t| + t.integer :eye + t.string :color + end + create_table :customers, :force => true do |t| t.string :name t.integer :balance, :default => 0 @@ -194,6 +199,9 @@ ActiveRecord::Schema.define do t.integer :car_id end + create_table :eyes, :force => true do |t| + end + create_table :tyres, :force => true do |t| t.integer :car_id end -- cgit v1.2.3