提交 8e5f07da 编写于 作者: A Aaron Patterson

Merge pull request #5535 from markmcspadden/issue_5527_rollbacks

Allow manual rollbacks in after_save to reset object correctly
......@@ -290,7 +290,15 @@ def with_transaction_returning_status
status = nil
self.class.transaction do
add_to_transaction
status = yield
begin
status = yield
rescue ActiveRecord::Rollback
if defined?(@_start_transaction_state)
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
end
status = nil
end
raise ActiveRecord::Rollback unless status
end
status
......
......@@ -287,8 +287,45 @@ def test_after_rollback_called
raise ActiveRecord::Rollback
end
assert topic.id.nil?
assert !topic.persisted?
assert_equal %w{ after_rollback }, topic.history
end
class TopicWithManualRollbackObserverAttached < ActiveRecord::Base
self.table_name = :topics
def history
@history ||= []
end
end
class TopicWithManualRollbackObserverAttachedObserver < ActiveRecord::Observer
def after_save(record)
record.history.push "after_save"
raise ActiveRecord::Rollback
end
end
def test_after_save_called_with_manual_rollback
assert TopicWithManualRollbackObserverAttachedObserver.instance, 'should have observer'
topic = TopicWithManualRollbackObserverAttached.new
assert !topic.save
assert_equal nil, topic.id
assert !topic.persisted?
assert_equal %w{ after_save }, topic.history
end
def test_after_save_called_with_manual_rollback_bang
assert TopicWithManualRollbackObserverAttachedObserver.instance, 'should have observer'
topic = TopicWithManualRollbackObserverAttached.new
topic.save!
assert_equal nil, topic.id
assert !topic.persisted?
assert_equal %w{ after_save }, topic.history
end
end
class SaveFromAfterCommitBlockTest < ActiveRecord::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册