aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/autosave_association_test.rb
diff options
context:
space:
mode:
authorEdgars Beigarts <edgars.beigarts@gmail.com>2016-12-06 08:09:10 +0200
committerEdgars Beigarts <edgars.beigarts@gmail.com>2016-12-06 08:09:10 +0200
commite7f4c45bdc178c41face92e4be9ec027a07daeda (patch)
tree56e0b54715d3791182a4811bacced49f1b240288 /activerecord/test/cases/autosave_association_test.rb
parentadebb207805cb573b2387f478b4a5549badab5e9 (diff)
downloadrails-e7f4c45bdc178c41face92e4be9ec027a07daeda.tar.gz
rails-e7f4c45bdc178c41face92e4be9ec027a07daeda.tar.bz2
rails-e7f4c45bdc178c41face92e4be9ec027a07daeda.zip
Improve test without using anonymous classes
Diffstat (limited to 'activerecord/test/cases/autosave_association_test.rb')
-rw-r--r--activerecord/test/cases/autosave_association_test.rb51
1 files changed, 24 insertions, 27 deletions
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
index 336cf9f28f..77ee3ca2d7 100644
--- a/activerecord/test/cases/autosave_association_test.rb
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -649,33 +649,6 @@ class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCa
assert_equal 2, firm.clients.length
assert_includes firm.clients, Client.find_by_name("New Client")
end
-
- def test_inverse_association_within_autosave_after_save_callback
- posts = Class.new(ActiveRecord::Base) do
- self.table_name = "posts"
- end
- comments = Class.new(ActiveRecord::Base) do
- self.table_name = "comments"
- end
- posts.class_eval do
- has_many :comments, inverse_of: :post, foreign_key: :post_id, anonymous_class: comments
- end
- comments.class_eval do
- belongs_to :post, inverse_of: :comments, anonymous_class: posts
-
- attr_accessor :post_comments_count
- after_save do
- self.post_comments_count = post.comments.count
- end
- end
-
- post = posts.new(title: "Test", body: "...")
- comment = post.comments.build(body: "...")
- post.save!
-
- assert_equal 1, post.comments.count
- assert_equal 1, comment.post_comments_count
- end
end
class TestDefaultAutosaveAssociationOnNewRecord < ActiveRecord::TestCase
@@ -1726,3 +1699,27 @@ class TestAutosaveAssociationWithTouch < ActiveRecord::TestCase
assert_nothing_raised { invoice.line_items.create(amount: 10) }
end
end
+
+class TestAutosaveAssociationOnAHasManyAssociationWithInverse < ActiveRecord::TestCase
+ class Post < ActiveRecord::Base
+ has_many :comments, inverse_of: :post
+ end
+
+ class Comment < ActiveRecord::Base
+ belongs_to :post, inverse_of: :comments
+
+ attr_accessor :post_comments_count
+ after_save do
+ self.post_comments_count = post.comments.count
+ end
+ end
+
+ def test_after_save_callback_with_autosave
+ post = Post.new(title: "Test", body: "...")
+ comment = post.comments.build(body: "...")
+ post.save!
+
+ assert_equal 1, post.comments.count
+ assert_equal 1, comment.post_comments_count
+ end
+end