diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-09-13 15:50:38 -0400 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-09-13 15:50:38 -0400 |
commit | 7454cbf7a15556f1d78e61c7d9d64c93cee79ad3 (patch) | |
tree | e3e82615457bb9db3dde40f0044ac0d47933c55d | |
parent | f89e2c2cba429efd62e9d6e2c4c96afa205c5135 (diff) | |
parent | 35ee756a366d3ac80128a017cf8ba197a0fdd4a1 (diff) | |
download | rails-7454cbf7a15556f1d78e61c7d9d64c93cee79ad3.tar.gz rails-7454cbf7a15556f1d78e61c7d9d64c93cee79ad3.tar.bz2 rails-7454cbf7a15556f1d78e61c7d9d64c93cee79ad3.zip |
Merge pull request #33378 from numbata/subclass-redefine-autosave-callbacks
Allow subclasses to redefine autosave callbacks for associated records
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 19 | ||||
-rw-r--r-- | activerecord/test/models/company.rb | 6 |
4 files changed, 32 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 942a6ca282..bfdc978005 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Allow subclasses to redefine autosave callbacks for associated records. + + Fixes #33305. + + *Andrey Subbota* + * Bump minimum MySQL version to 5.5.8. *Yasuo Honda* diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 783a8366ce..d77d76cb1e 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -149,7 +149,7 @@ module ActiveRecord private def define_non_cyclic_method(name, &block) - return if method_defined?(name) + return if instance_methods(false).include?(name) define_method(name) do |*args| result = true; @_already_called ||= {} # Loop prevention for validation of associations diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index fa618735d7..db3a58eba9 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -14,6 +14,7 @@ require "models/line_item" require "models/order" require "models/parrot" require "models/pirate" +require "models/project" require "models/ship" require "models/ship_part" require "models/tag" @@ -1787,3 +1788,21 @@ class TestAutosaveAssociationOnAHasManyAssociationWithInverse < ActiveRecord::Te assert_equal 1, comment.post_comments_count end end + +class TestAutosaveAssociationOnAHasManyAssociationDefinedInSubclassWithAcceptsNestedAttributes < ActiveRecord::TestCase + def test_should_update_children_when_asssociation_redefined_in_subclass + agency = Agency.create!(name: "Agency") + valid_project = Project.create!(firm: agency, name: "Initial") + agency.update!( + "projects_attributes" => { + "0" => { + "name" => "Updated", + "id" => valid_project.id + } + } + ) + valid_project.reload + + assert_equal "Updated", valid_project.name + end +end diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 485b35d58b..838f515aad 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -122,6 +122,12 @@ class RestrictedWithErrorFirm < Company has_many :companies, -> { order("id") }, foreign_key: "client_of", dependent: :restrict_with_error end +class Agency < Firm + has_many :projects, foreign_key: :firm_id + + accepts_nested_attributes_for :projects +end + class Client < Company belongs_to :firm, foreign_key: "client_of" belongs_to :firm_with_basic_id, class_name: "Firm", foreign_key: "firm_id" |