Sha256: 5633a8f01c0f7fa15cba1474d569f6f94924d356bf438f4a7ec4435d41686c3f

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 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(&get_ids)
      ids = nil
      get_ids_with_cache = ->{ ids ||= get_ids.call }
      ActiveModelCachers::ActiveRecord::Extension.global_callbacks.on_nullify.exec(
        self,
        reflection.klass,
        reflection.foreign_key,
        get_ids_with_cache,
      )
    end
  end
end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_model_cachers-2.1.7 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.1.5 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.1.4 lib/active_model_cachers/hook/associations.rb
active_model_cachers-2.1.3 lib/active_model_cachers/hook/associations.rb