提交 c45c9cfb 编写于 作者: R Ryuta Kamizono

Cache the association proxy object

Some third party modules expects that association returns same proxy
object each time (e.g. for stubbing collection methods:
https://github.com/rspec/rspec-rails/issues/1817).
So I decided that cache the proxy object and reset scope in the proxy
object each time.

Related context: https://github.com/rails/rails/commit/c86a32d7451c5d901620ac58630460915292f88b#commitcomment-2784312
上级 9b78974b
......@@ -30,7 +30,8 @@ def reader
reload
end
CollectionProxy.create(klass, self)
@proxy ||= CollectionProxy.create(klass, self)
@proxy.reset_scope
end
# Implements the writer method, e.g. foo.items= for Foo.has_many :items
......
......@@ -1087,9 +1087,8 @@ def clear
# person.pets(true) # fetches pets from the database
# # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
def reload
@scope = nil
proxy_association.reload
self
reset_scope
end
# Unloads the association. Returns +self+.
......@@ -1109,9 +1108,13 @@ def reload
# person.pets # fetches pets from the database
# # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
def reset
@scope = nil
proxy_association.reset
proxy_association.reset_scope
reset_scope
end
def reset_scope # :nodoc:
@scope = nil
self
end
......
......@@ -220,6 +220,11 @@ def test_scoped_allows_conditions
assert_equal david.projects, david.projects.scope
end
test "proxy object is cached" do
david = developers(:david)
assert_same david.projects, david.projects
end
test "inverses get set of subsets of the association" do
man = Man.create
man.interests.create
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册