Sha256: 2a1d6e810d0ecffe133b3545739c18997423812939e7d745c925e2ea06fca159

Contents?: true

Size: 1.15 KB

Versions: 55

Compression:

Stored size: 1.15 KB

Contents

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(*) # :nodoc:
      super unless no_touching?
    end
  end
end

Version data entries

55 entries across 54 versions & 9 rubygems

Version Path
activerecord-5.0.0.rc1 lib/active_record/no_touching.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/no_touching.rb
activerecord-5.0.0.beta4 lib/active_record/no_touching.rb
activerecord-4.2.6 lib/active_record/no_touching.rb
activerecord-4.2.6.rc1 lib/active_record/no_touching.rb
activerecord-4.2.5.2 lib/active_record/no_touching.rb
activerecord-5.0.0.beta3 lib/active_record/no_touching.rb
activerecord-5.0.0.beta2 lib/active_record/no_touching.rb
activejob-lock-0.0.2 rails/activerecord/lib/active_record/no_touching.rb
activerecord-5.0.0.beta1.1 lib/active_record/no_touching.rb
activerecord-4.2.5.1 lib/active_record/no_touching.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activerecord-4.2.4/lib/active_record/no_touching.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activerecord-4.2.4/lib/active_record/no_touching.rb
activerecord-5.0.0.beta1 lib/active_record/no_touching.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activerecord-4.2.4/lib/active_record/no_touching.rb
activerecord-4.2.5 lib/active_record/no_touching.rb
activerecord-4.2.5.rc2 lib/active_record/no_touching.rb
activerecord-4.2.5.rc1 lib/active_record/no_touching.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/no_touching.rb
activerecord-4.2.4 lib/active_record/no_touching.rb