1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #28474 from kamipo/fix_extension_with_darty_target

Fix extension method with dirty target in has_many associations
This commit is contained in:
Kasper Timm Hansen 2017-03-20 19:32:07 +01:00 committed by GitHub
commit dd40c77053
3 changed files with 12 additions and 2 deletions

View file

@ -1154,8 +1154,9 @@ module ActiveRecord
end
def method_missing(method, *args, &block)
if scope.respond_to?(method)
scope.public_send(method, *args, &block)
if scope.respond_to?(method) && scope.extending_values.any?
extend(*scope.extending_values)
public_send(method, *args, &block)
else
super
end

View file

@ -36,6 +36,11 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase
assert_equal comments(:greetings), posts(:welcome).comments.not_again.find_most_recent
end
def test_extension_with_dirty_target
comment = posts(:welcome).comments.build(body: "New comment")
assert_equal comment, posts(:welcome).comments.with_content("New comment")
end
def test_marshalling_extensions
david = developers(:david)
assert_equal projects(:action_controller), david.projects.find_most_recent

View file

@ -59,6 +59,10 @@ class Post < ActiveRecord::Base
def the_association
proxy_association
end
def with_content(content)
self.detect { |comment| comment.body == content }
end
end
has_many :comments_with_extend, extend: NamedExtension, class_name: "Comment", foreign_key: "post_id" do