Sha256: b1a6850c78aa44cfa7f56b86b578b3b8abf86b0749935021395c43b6f8fc6592

Contents?: true

Size: 1.25 KB

Versions: 34

Compression:

Stored size: 1.25 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
          Thread.current[:no_touching_classes] ||= []
        end
    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

34 entries across 34 versions & 4 rubygems

Version Path
activerecord-5.2.8.1 lib/active_record/no_touching.rb
activerecord-5.2.8 lib/active_record/no_touching.rb
activerecord-5.2.7.1 lib/active_record/no_touching.rb
activerecord-5.2.7 lib/active_record/no_touching.rb
activerecord-5.2.6.3 lib/active_record/no_touching.rb
activerecord-5.2.6.2 lib/active_record/no_touching.rb
activerecord-5.2.6.1 lib/active_record/no_touching.rb
activerecord-5.2.6 lib/active_record/no_touching.rb
activerecord-5.2.4.6 lib/active_record/no_touching.rb
activerecord-5.2.5 lib/active_record/no_touching.rb
activerecord-5.2.4.5 lib/active_record/no_touching.rb
activerecord-5.2.4.4 lib/active_record/no_touching.rb
activerecord-5.2.4.3 lib/active_record/no_touching.rb
activerecord-5.2.4.2 lib/active_record/no_touching.rb
activerecord-5.2.4.1 lib/active_record/no_touching.rb
activerecord-5.2.4 lib/active_record/no_touching.rb
activerecord-5.2.4.rc1 lib/active_record/no_touching.rb
spiral_form-0.1.1 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/no_touching.rb
spiral_form-0.1.0 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/no_touching.rb
activerecord-5.2.3 lib/active_record/no_touching.rb