mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Revert "Merge pull request #35186 from kamipo/fix_leaking_scope_on_relation_create"
This reverts commitb67d5c6ded
, reversing changes made to2e018361c7
. Reason: #35186 may cause that silently leaking information when people upgrade the app. We need deprecation first before making this.
This commit is contained in:
parent
79b8b62621
commit
cdb8697b4a
6 changed files with 41 additions and 40 deletions
|
@ -1,9 +1,3 @@
|
|||
* Fix `relation.create` to avoid leaking scope to initialization block and callbacks.
|
||||
|
||||
Fixes #9894, #17577.
|
||||
|
||||
*Ryuta Kamizono*
|
||||
|
||||
* Allow applications to automatically switch connections.
|
||||
|
||||
Adds a middleware and configuration options that can be used in your
|
||||
|
|
|
@ -67,7 +67,6 @@ module ActiveRecord
|
|||
# user = users.new { |user| user.name = 'Oscar' }
|
||||
# user.name # => Oscar
|
||||
def new(attributes = nil, &block)
|
||||
block = klass.current_scope_restoring_block(&block)
|
||||
scoping { klass.new(attributes, &block) }
|
||||
end
|
||||
|
||||
|
@ -93,11 +92,7 @@ module ActiveRecord
|
|||
# users.create(name: nil) # validation on name
|
||||
# # => #<User id: nil, name: nil, ...>
|
||||
def create(attributes = nil, &block)
|
||||
if attributes.is_a?(Array)
|
||||
attributes.collect { |attr| create(attr, &block) }
|
||||
else
|
||||
new(attributes, &block).tap(&:save)
|
||||
end
|
||||
scoping { klass.create(attributes, &block) }
|
||||
end
|
||||
|
||||
# Similar to #create, but calls
|
||||
|
@ -107,11 +102,7 @@ module ActiveRecord
|
|||
# Expects arguments in the same format as
|
||||
# {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!].
|
||||
def create!(attributes = nil, &block)
|
||||
if attributes.is_a?(Array)
|
||||
attributes.collect { |attr| create!(attr, &block) }
|
||||
else
|
||||
new(attributes, &block).tap(&:save!)
|
||||
end
|
||||
scoping { klass.create!(attributes, &block) }
|
||||
end
|
||||
|
||||
def first_or_create(attributes = nil, &block) # :nodoc:
|
||||
|
|
|
@ -30,14 +30,6 @@ module ActiveRecord
|
|||
def current_scope=(scope)
|
||||
ScopeRegistry.set_value_for(:current_scope, self, scope)
|
||||
end
|
||||
|
||||
def current_scope_restoring_block(&block)
|
||||
current_scope = self.current_scope(true)
|
||||
-> *args do
|
||||
self.current_scope = current_scope
|
||||
yield(*args) if block_given?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def populate_with_current_scope_attributes # :nodoc:
|
||||
|
|
|
@ -1536,7 +1536,7 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
Bird.create!(name: "Bluejay")
|
||||
|
||||
ActiveRecord::Base.connection.while_preventing_writes do
|
||||
assert_nothing_raised { Bird.where(name: "Bluejay").explain }
|
||||
assert_queries(2) { Bird.where(name: "Bluejay").explain }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1167,13 +1167,21 @@ class RelationTest < ActiveRecord::TestCase
|
|||
assert_equal "green", parrot.color
|
||||
end
|
||||
|
||||
def test_first_or_create_with_block
|
||||
canary = Bird.create!(color: "yellow", name: "canary")
|
||||
def test_first_or_create_with_after_initialize
|
||||
Bird.create!(color: "yellow", name: "canary")
|
||||
parrot = Bird.where(color: "green").first_or_create do |bird|
|
||||
bird.name = "parrot"
|
||||
assert_equal canary, Bird.find_by!(name: "canary")
|
||||
bird.enable_count = true
|
||||
end
|
||||
assert_equal 0, parrot.total_count
|
||||
end
|
||||
|
||||
def test_first_or_create_with_block
|
||||
Bird.create!(color: "yellow", name: "canary")
|
||||
parrot = Bird.where(color: "green").first_or_create do |bird|
|
||||
bird.name = "parrot"
|
||||
assert_equal 0, Bird.count
|
||||
end
|
||||
assert_equal 1, parrot.total_count
|
||||
assert_kind_of Bird, parrot
|
||||
assert_predicate parrot, :persisted?
|
||||
assert_equal "green", parrot.color
|
||||
|
@ -1214,13 +1222,21 @@ class RelationTest < ActiveRecord::TestCase
|
|||
assert_raises(ActiveRecord::RecordInvalid) { Bird.where(color: "green").first_or_create! }
|
||||
end
|
||||
|
||||
def test_first_or_create_bang_with_valid_block
|
||||
canary = Bird.create!(color: "yellow", name: "canary")
|
||||
def test_first_or_create_bang_with_after_initialize
|
||||
Bird.create!(color: "yellow", name: "canary")
|
||||
parrot = Bird.where(color: "green").first_or_create! do |bird|
|
||||
bird.name = "parrot"
|
||||
assert_equal canary, Bird.find_by!(name: "canary")
|
||||
bird.enable_count = true
|
||||
end
|
||||
assert_equal 0, parrot.total_count
|
||||
end
|
||||
|
||||
def test_first_or_create_bang_with_valid_block
|
||||
Bird.create!(color: "yellow", name: "canary")
|
||||
parrot = Bird.where(color: "green").first_or_create! do |bird|
|
||||
bird.name = "parrot"
|
||||
assert_equal 0, Bird.count
|
||||
end
|
||||
assert_equal 1, parrot.total_count
|
||||
assert_kind_of Bird, parrot
|
||||
assert_predicate parrot, :persisted?
|
||||
assert_equal "green", parrot.color
|
||||
|
@ -1269,13 +1285,21 @@ class RelationTest < ActiveRecord::TestCase
|
|||
assert_equal "green", parrot.color
|
||||
end
|
||||
|
||||
def test_first_or_initialize_with_block
|
||||
canary = Bird.create!(color: "yellow", name: "canary")
|
||||
def test_first_or_initialize_with_after_initialize
|
||||
Bird.create!(color: "yellow", name: "canary")
|
||||
parrot = Bird.where(color: "green").first_or_initialize do |bird|
|
||||
bird.name = "parrot"
|
||||
assert_equal canary, Bird.find_by!(name: "canary")
|
||||
bird.enable_count = true
|
||||
end
|
||||
assert_equal 0, parrot.total_count
|
||||
end
|
||||
|
||||
def test_first_or_initialize_with_block
|
||||
Bird.create!(color: "yellow", name: "canary")
|
||||
parrot = Bird.where(color: "green").first_or_initialize do |bird|
|
||||
bird.name = "parrot"
|
||||
assert_equal 0, Bird.count
|
||||
end
|
||||
assert_equal 1, parrot.total_count
|
||||
assert_kind_of Bird, parrot
|
||||
assert_not_predicate parrot, :persisted?
|
||||
assert_predicate parrot, :valid?
|
||||
|
|
|
@ -17,8 +17,8 @@ class Bird < ActiveRecord::Base
|
|||
throw(:abort)
|
||||
end
|
||||
|
||||
attr_accessor :total_count
|
||||
attr_accessor :total_count, :enable_count
|
||||
after_initialize do
|
||||
self.total_count = Bird.count
|
||||
self.total_count = Bird.count if enable_count
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue