aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-10-22 10:28:53 +0200
committerXavier Noria <fxn@hashref.com>2010-10-22 10:28:53 +0200
commit5b86c3e5bb2bb54003d8f211b46a7b992355dbf5 (patch)
tree1375ec5b4b1bfc68b8255b82980aa6bb45b6da3b
parentfc8c0729881fff59e916f7ab180dbfa03ee1b891 (diff)
downloadrails-5b86c3e5bb2bb54003d8f211b46a7b992355dbf5.tar.gz
rails-5b86c3e5bb2bb54003d8f211b46a7b992355dbf5.tar.bz2
rails-5b86c3e5bb2bb54003d8f211b46a7b992355dbf5.zip
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.
-rw-r--r--activerecord/CHANGELOG3
-rw-r--r--activerecord/lib/active_record/autosave_association.rb11
-rw-r--r--activerecord/test/cases/autosave_association_test.rb20
-rw-r--r--activerecord/test/models/eye.rb37
-rw-r--r--activerecord/test/schema/schema.rb8
5 files changed, 78 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 75657cb6ee..d3530e0da9 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,8 @@
*Rails 3.1.0 (unreleased)*
+* has_one maintains the association with separate after_create/after_update instead
+ of a single after_save. [fxn]
+
* The following code:
Model.limit(10).scoping { Model.count }
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index 21a9a1f2cb..4a2c078e91 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -167,7 +167,16 @@ module ActiveRecord
else
if reflection.macro == :has_one
define_method(save_method) { save_has_one_association(reflection) }
- after_save save_method
+ # Configures two callbacks instead of a single after_save so that
+ # the model may rely on their execution order relative to its
+ # own callbacks.
+ #
+ # For example, given that after_creates run before after_saves, if
+ # we configured instead an after_save there would be no way to fire
+ # a custom after_create callback after the child association gets
+ # created.
+ after_create save_method
+ after_update save_method
else
define_method(save_method) { save_belongs_to_association(reflection) }
before_save save_method
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