123456789_123456789_123456789_123456789_123456789_

Module: ActiveRecord::Suppressor

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: activerecord/lib/active_record/suppressor.rb

Overview

Suppressor prevents the receiver from being saved during a given block.

For example, here’s a pattern of creating notifications when new comments are posted. (The notification may in turn trigger an email, a push notification, or just appear in the UI somewhere):

class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
  after_create -> { Notification.create! comment: self,
    recipients: commentable.recipients }
end

That’s what you want the bulk of the time. New comment creates a new Notification. But there may well be off cases, like copying a commentable and its comments, where you don’t want that. So you’d have a concern something like this:

module Copyable
  def copy_to(destination)
    Notification.suppress do
      # Copy logic that creates new comments that we do not want
      # triggering notifications.
    end
  end
end

Class Method Summary

::ActiveSupport::Concern - Extended

class_methods

Define class methods from given block.

included

Evaluate given block in context of base class, so that you can write class macros here.

prepended

Evaluate given block in context of base class, so that you can write class macros here.

append_features, prepend_features

Instance Method Summary

Class Method Details

.registry

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/suppressor.rb', line 36

def registry # :nodoc:
  ActiveSupport::IsolatedExecutionState[:active_record_suppressor_registry] ||= {}
end

Instance Method Details

#save

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/suppressor.rb', line 51

def save(**) # :nodoc:
  Suppressor.registry[self.class.name] ? true : super
end

#save!

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/suppressor.rb', line 55

def save!(**) # :nodoc:
  Suppressor.registry[self.class.name] ? true : super
end