Sha256: b99839969ef9963815ae0cda6914e79cf3b6656230196f79f6f183f38e6929a5

Contents?: true

Size: 889 Bytes

Versions: 7

Compression:

Stored size: 889 Bytes

Contents

# frozen_string_literal: true
require 'active_record'
require 'active_record/associations/has_many_association'
require 'active_model_cachers/hook/on_model_delete'

module ActiveModelCachers::Hook
  module Associations
    def delete_count(method, scope)
      if method == :delete_all
        # TODO:
      else # nullify
        call_hooks{ scope.pluck(:id) }
      end
      super
    end

    def delete_records(records, method)
      case method
      when :destroy
      when :delete_all
        # TODO:
      else
        call_hooks{ records.map(&:id) }
      end
      super
    end

    private

    def call_hooks
      return if (hooks = reflection.klass.nullify_hooks_at(reflection.foreign_key)).blank?
      ids = yield
      hooks.each{|s| s.call(ids) }
    end
  end
end

ActiveRecord::Associations::HasManyAssociation.send(:prepend, ActiveModelCachers::Hook::Associations)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_model_cachers-2.1.2 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.1.1 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.1.0 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.0.3 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.0.2 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.0.1 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.0.0 lib/active_model_cachers/hook/associations.rb