Sha256: 2001efe376f10b1f075260a9c60e115b889edf48983591ca57c020798211d0dd

Contents?: true

Size: 1.14 KB

Versions: 51

Compression:

Stored size: 1.14 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(*)
      super unless no_touching?
    end
  end
end

Version data entries

51 entries across 51 versions & 2 rubygems

Version Path
activerecord-4.1.16 lib/active_record/no_touching.rb
activerecord-4.1.16.rc1 lib/active_record/no_touching.rb
activerecord-4.1.15 lib/active_record/no_touching.rb
activerecord-4.1.15.rc1 lib/active_record/no_touching.rb
activerecord-4.1.14.2 lib/active_record/no_touching.rb
activerecord-4.1.14.1 lib/active_record/no_touching.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activerecord-4.1.13/lib/active_record/no_touching.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activerecord-4.1.13/lib/active_record/no_touching.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activerecord-4.1.13/lib/active_record/no_touching.rb
activerecord-4.1.14 lib/active_record/no_touching.rb
activerecord-4.1.14.rc2 lib/active_record/no_touching.rb
activerecord-4.1.14.rc1 lib/active_record/no_touching.rb
activerecord-4.1.13 lib/active_record/no_touching.rb
activerecord-4.1.13.rc1 lib/active_record/no_touching.rb
activerecord-4.1.12 lib/active_record/no_touching.rb
activerecord-4.1.12.rc1 lib/active_record/no_touching.rb
activerecord-4.1.11 lib/active_record/no_touching.rb
activerecord-4.1.10 lib/active_record/no_touching.rb
activerecord-4.1.10.rc4 lib/active_record/no_touching.rb
activerecord-4.1.10.rc3 lib/active_record/no_touching.rb