提交 ac002395 编写于 作者: G George Claghorn

Apply Active Record suppression to all saves

It was not being applied to creates and updates attempted through the
non-bang save methods. This means that, for example, creation of
records for singular associations through the `create_*` methods was
not appropriately ignored in .suppress blocks.
上级 ae5f2b4e
......@@ -37,8 +37,7 @@ def suppress(&block)
end
end
# Ignore saving events if we're in suppression mode.
def save!(*args) # :nodoc:
def create_or_update(*args) # :nodoc:
SuppressorRegistry.suppressed[self.class.name] ? true : super
end
end
......
......@@ -3,7 +3,38 @@
require 'models/user'
class SuppressorTest < ActiveRecord::TestCase
def test_suppresses_creation_of_record_generated_by_callback
def test_suppresses_create
assert_no_difference -> { Notification.count } do
Notification.suppress do
Notification.create
Notification.create!
Notification.new.save
Notification.new.save!
end
end
end
def test_suppresses_update
user = User.create! token: 'asdf'
User.suppress do
user.update token: 'ghjkl'
assert_equal 'asdf', user.reload.token
user.update! token: 'zxcvbnm'
assert_equal 'asdf', user.reload.token
user.token = 'qwerty'
user.save
assert_equal 'asdf', user.reload.token
user.token = 'uiop'
user.save!
assert_equal 'asdf', user.reload.token
end
end
def test_suppresses_create_in_callback
assert_difference -> { User.count } do
assert_no_difference -> { Notification.count } do
Notification.suppress { UserWithNotification.create! }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册