module Suppressor

Ruby on Rails edge

Since v5.0.7.2

Available in: v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Active Record Suppressor

ActiveRecord::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 “‘

Namespace

Modules

Extends

Used by

Included by (1)

Methods (inherited)

From ActiveSupport::Concern (3)

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close