Sha256: e205c46b8b11c4be34fd91b303e138b7ed8c0e8a0174b3b546bf4631bbb91e27

Contents?: true

Size: 777 Bytes

Versions: 4

Compression:

Stored size: 777 Bytes

Contents

module ActiverecordTouchy
  module Associations
    def self.included(base)
      class << base
        prepend(ClassMethods)
      end
    end

    module ClassMethods
      def has_many(name, scope = nil, **options, &extension)
        touch = (scope.is_a?(Hash) ? scope : options).delete(:touch)
        result = super

        if touch
          after_commit do
            public_send(name).update_all(updated_at: Time.now.utc)
          end
        end

        result
      end

      def has_one(name, scope = nil, **options)
        touch = (scope.is_a?(Hash) ? scope : options).delete(:touch)
        result = super

        if touch
          after_commit do
            public_send(name)&.touch
          end
        end

        result
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activerecord_touchy-0.1.4 lib/activerecord_touchy/associations.rb
activerecord_touchy-0.1.3 lib/activerecord_touchy/associations.rb
activerecord_touchy-0.1.2 lib/activerecord_touchy/associations.rb
activerecord_touchy-0.1.1 lib/activerecord_touchy/associations.rb