Sha256: 975f7be397f4430b72ac1bb0ed8d09f196e169817a4ac4d5375a399cfe93492d

Contents?: true

Size: 1.67 KB

Versions: 40

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  # 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
  module Suppressor
    extend ActiveSupport::Concern

    class << self
      def registry # :nodoc:
        ActiveSupport::IsolatedExecutionState[:active_record_suppresor_registry] ||= {}
      end
    end

    module ClassMethods
      def suppress(&block)
        previous_state = Suppressor.registry[name]
        Suppressor.registry[name] = true
        yield
      ensure
        Suppressor.registry[name] = previous_state
      end
    end

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

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

Version data entries

40 entries across 38 versions & 5 rubygems

Version Path
activerecord-7.0.8.7 lib/active_record/suppressor.rb
activerecord-7.0.8.6 lib/active_record/suppressor.rb
activerecord-7.0.8.5 lib/active_record/suppressor.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/suppressor.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/suppressor.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/suppressor.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/suppressor.rb
activerecord-7.0.8.4 lib/active_record/suppressor.rb
activerecord-7.0.8.1 lib/active_record/suppressor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/suppressor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/suppressor.rb
activerecord-7.0.8 lib/active_record/suppressor.rb
activerecord-7.0.7.2 lib/active_record/suppressor.rb
activerecord-7.0.7.1 lib/active_record/suppressor.rb
activerecord-7.0.7 lib/active_record/suppressor.rb
activerecord-7.0.6 lib/active_record/suppressor.rb
activerecord-7.0.5.1 lib/active_record/suppressor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/suppressor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/suppressor.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/suppressor.rb