Sha256: c0c3d3a7298fd9950fa21f646ad1f22d5d8ad1e2990211b3b1e87187c88a644f

Contents?: true

Size: 1.51 KB

Versions: 75

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  # = Active Record No Touching
  module NoTouching
    extend ActiveSupport::Concern

    module ClassMethods
      # Lets you selectively disable calls to +touch+ for the
      # duration of a block.
      #
      # ==== Examples
      #   ActiveRecord::Base.no_touching do
      #     Project.first.touch  # does nothing
      #     Message.first.touch  # does nothing
      #   end
      #
      #   Project.no_touching do
      #     Project.first.touch  # does nothing
      #     Message.first.touch  # works, but does not touch the associated project
      #   end
      #
      def no_touching(&block)
        NoTouching.apply_to(self, &block)
      end
    end

    class << self
      def apply_to(klass) # :nodoc:
        klasses.push(klass)
        yield
      ensure
        klasses.pop
      end

      def applied_to?(klass) # :nodoc:
        klasses.any? { |k| k >= klass }
      end

      private
        def klasses
          ActiveSupport::IsolatedExecutionState[:active_record_no_touching_classes] ||= []
        end
    end

    # Returns +true+ if the class has +no_touching+ set, +false+ otherwise.
    #
    #   Project.no_touching do
    #     Project.first.no_touching? # true
    #     Message.first.no_touching? # false
    #   end
    #
    def no_touching?
      NoTouching.applied_to?(self.class)
    end

    def touch_later(*) # :nodoc:
      super unless no_touching?
    end

    def touch(*, **) # :nodoc:
      super unless no_touching?
    end
  end
end

Version data entries

75 entries across 72 versions & 7 rubygems

Version Path
activerecord-7.1.2 lib/active_record/no_touching.rb
activerecord-7.1.1 lib/active_record/no_touching.rb
activerecord-7.1.0 lib/active_record/no_touching.rb
activerecord-7.1.0.rc2 lib/active_record/no_touching.rb
activerecord-7.1.0.rc1 lib/active_record/no_touching.rb
activerecord-7.1.0.beta1 lib/active_record/no_touching.rb
activerecord-7.0.8 lib/active_record/no_touching.rb
activerecord-7.0.7.2 lib/active_record/no_touching.rb
activerecord-7.0.7.1 lib/active_record/no_touching.rb
activerecord-7.0.7 lib/active_record/no_touching.rb
activerecord-7.0.6 lib/active_record/no_touching.rb
activerecord-7.0.5.1 lib/active_record/no_touching.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/no_touching.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/no_touching.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/no_touching.rb
activerecord-7.0.5 lib/active_record/no_touching.rb
activerecord-7.0.4.3 lib/active_record/no_touching.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/no_touching.rb
activerecord-7.0.4.2 lib/active_record/no_touching.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/no_touching.rb