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

Merge pull request #37523 from kamipo/refactor_association_scoping

Refactor `association.scoping` not to rely on `klass.all`
上级 bc5c2c0d
......@@ -17,23 +17,23 @@ def ==(other)
def build(attributes = nil, &block)
block = _deprecated_scope_block("new", &block)
@association.enable_scoping do
scoping { @association.build(attributes, &block) }
@association.scoping(self) do
@association.build(attributes, &block)
end
end
alias new build
def create(attributes = nil, &block)
block = _deprecated_scope_block("create", &block)
@association.enable_scoping do
scoping { @association.create(attributes, &block) }
@association.scoping(self) do
@association.create(attributes, &block)
end
end
def create!(attributes = nil, &block)
block = _deprecated_scope_block("create!", &block)
@association.enable_scoping do
scoping { @association.create!(attributes, &block) }
@association.scoping(self) do
@association.create!(attributes, &block)
end
end
......
......@@ -43,7 +43,7 @@ def initialize(owner, reflection)
reflection.check_validity!
@owner, @reflection = owner, reflection
@enable_scoping = false
@_scope = nil
reset
reset_scope
......@@ -96,7 +96,7 @@ def target=(target)
end
def scope
target_scope.merge!(association_scope)
@_scope&.spawn || target_scope.merge!(association_scope)
end
def reset_scope
......@@ -196,11 +196,11 @@ def create!(attributes = {}, &block)
_create_record(attributes, true, &block)
end
def enable_scoping
@enable_scoping = true
yield
def scoping(relation, &block)
@_scope = relation
relation.scoping(&block)
ensure
@enable_scoping = false
@_scope = nil
end
private
......@@ -233,11 +233,7 @@ def association_scope
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
# through association's scope)
def target_scope
AssociationRelation.create(klass, self).merge!(scope_for_association)
end
def scope_for_association
@enable_scoping ? klass.all : klass.scope_for_association
AssociationRelation.create(klass, self).merge!(klass.scope_for_association)
end
def scope_for_create
......
......@@ -254,6 +254,15 @@ def test_build_and_create_from_association_should_respect_unscope_over_default_s
bulb = car.bulbs.unscope(where: :name).create!
assert_nil bulb.name
bulb = car.awesome_bulbs.unscope(where: :frickinawesome).build
assert_equal false, bulb.frickinawesome
bulb = car.awesome_bulbs.unscope(where: :frickinawesome).create
assert_equal false, bulb.frickinawesome
bulb = car.awesome_bulbs.unscope(where: :frickinawesome).create!
assert_equal false, bulb.frickinawesome
end
def test_build_from_association_should_respect_scope
......
......@@ -688,8 +688,10 @@ def test_associate_with_create
def test_through_record_is_built_when_created_with_where
assert_difference("posts(:thinking).readers.count", 1) do
posts(:thinking).people.where(first_name: "Jeb").create
posts(:thinking).people.where(readers: { skimmer: true }).create(first_name: "Jeb")
end
reader = posts(:thinking).readers.last
assert_equal true, reader.skimmer
end
def test_associate_with_create_and_no_options
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册