Module: ActsAsParanoid::AssociationReflection

Defined in:
lib/acts_as_paranoid/association_reflection.rb

Overview

Override for ActiveRecord::Reflection::AssociationReflection

This makes automatic finding of inverse associations work where the inverse is a belongs_to association with the :with_deleted option set.

Specifying :with_deleted for the belongs_to association would stop the inverse from being calculated because it sets scope where there was none, and normally an association having a scope means ActiveRecord will not automatically find the inverse association.

This override adds an exception to that rule only for the case where the scope was added just to support the :with_deleted option.

Instance Method Summary collapse

Instance Method Details

#can_find_inverse_of_automatically?(reflection) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/acts_as_paranoid/association_reflection.rb', line 18

def can_find_inverse_of_automatically?(reflection)
  options = reflection.options

  if reflection.macro == :belongs_to && options[:with_deleted]
    return false if options[:inverse_of] == false
    return false if options[:foreign_key]

    !options.fetch(:original_scope)
  else
    super
  end
end

#scope_allows_automatic_inverse_of?(reflection, inverse_reflection) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/acts_as_paranoid/association_reflection.rb', line 31

def scope_allows_automatic_inverse_of?(reflection, inverse_reflection)
  if reflection.scope
    options = reflection.options
    return true if options[:with_deleted] && !options.fetch(:original_scope)
  end

  super
end