From 25801cfe78a124e868d7be068febf628d5fc1247 Mon Sep 17 00:00:00 2001 From: Jahfer Husain Date: Fri, 13 May 2016 10:14:51 -0400 Subject: Keep state around for nested calls to #suppress If a call to #suppress from the same class occurred inside another #suppress block, the suppression state would be set to false before the outer block completes. This change keeps the previous state around in memory and unwinds it as the blocks exit. --- activerecord/lib/active_record/suppressor.rb | 3 ++- activerecord/test/cases/suppressor_test.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/suppressor.rb b/activerecord/lib/active_record/suppressor.rb index 8ec4b48d31..d9acb1a1dc 100644 --- a/activerecord/lib/active_record/suppressor.rb +++ b/activerecord/lib/active_record/suppressor.rb @@ -30,10 +30,11 @@ module ActiveRecord module ClassMethods def suppress(&block) + previous_state = SuppressorRegistry.suppressed[name] SuppressorRegistry.suppressed[name] = true yield ensure - SuppressorRegistry.suppressed[name] = false + SuppressorRegistry.suppressed[name] = previous_state end end diff --git a/activerecord/test/cases/suppressor_test.rb b/activerecord/test/cases/suppressor_test.rb index 7d44e36419..2f00241de2 100644 --- a/activerecord/test/cases/suppressor_test.rb +++ b/activerecord/test/cases/suppressor_test.rb @@ -60,4 +60,16 @@ class SuppressorTest < ActiveRecord::TestCase end end end + + def test_suppresses_when_nested_multiple_times + assert_no_difference -> { Notification.count } do + Notification.suppress do + Notification.suppress { } + Notification.create + Notification.create! + Notification.new.save + Notification.new.save! + end + end + end end -- cgit v1.2.3