Sha256: 863454cbf9e1577fe86c9802522a161ef56f32d3d2182b94a14872781a73395e

Contents?: true

Size: 1.73 KB

Versions: 51

Compression:

Stored size: 1.73 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

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

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

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

  class SuppressorRegistry # :nodoc:
    extend ActiveSupport::PerThreadRegistry

    attr_reader :suppressed

    def initialize
      @suppressed = {}
    end
  end
end

Version data entries

51 entries across 51 versions & 7 rubygems

Version Path
activerecord-5.2.8.1 lib/active_record/suppressor.rb
activerecord-5.2.8 lib/active_record/suppressor.rb
activerecord-5.2.7.1 lib/active_record/suppressor.rb
activerecord-5.2.7 lib/active_record/suppressor.rb
activerecord-5.2.6.3 lib/active_record/suppressor.rb
activerecord-5.2.6.2 lib/active_record/suppressor.rb
activerecord-5.2.6.1 lib/active_record/suppressor.rb
activerecord-5.2.6 lib/active_record/suppressor.rb
activerecord-5.2.4.6 lib/active_record/suppressor.rb
activerecord-5.2.5 lib/active_record/suppressor.rb
activerecord-5.2.4.5 lib/active_record/suppressor.rb
activerecord-5.2.4.4 lib/active_record/suppressor.rb
activerecord-5.2.4.3 lib/active_record/suppressor.rb
activerecord-6.0.2.2 lib/active_record/suppressor.rb
activerecord-5.2.4.2 lib/active_record/suppressor.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/suppressor.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/suppressor.rb
activerecord-6.0.2.1 lib/active_record/suppressor.rb
activerecord-5.2.4.1 lib/active_record/suppressor.rb
activerecord-6.0.2 lib/active_record/suppressor.rb