123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Association::Referenced::WithPolymorphicCriteria Private

Do not use. This module is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/association/referenced/with_polymorphic_criteria.rb

Overview

Implements the with_polymorphic_criteria shared behavior.

Instance Method Summary

Instance Method Details

#with_polymorphic_criterion(criteria, base) ⇒ Mongoid::Criteria

If the receiver represents a polymorphic association, applies the polymorphic search criteria to the given criteria object.

Parameters:

  • criteria (Mongoid::Criteria)

    the criteria to append to if receiver is polymorphic.

  • base (Mongoid::Document)

    the document to use when resolving the polymorphic type keys.

Returns:

  • (Mongoid::Criteria)

    the resulting criteria, which may be the same as the input.

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/with_polymorphic_criteria.rb', line 20

def with_polymorphic_criterion(criteria, base)
  if polymorphic?
    # 1. get the resolver for the inverse association
    resolver = klass.reflect_on_association(as).resolver

    # 2. look up the list of keys from the resolver, given base
    keys = resolver.keys_for(base)

    # 3. use equality if there is just one key, `in` if there are multiple
    if keys.many?
      criteria.where(type => { :$in => keys })
    else
      criteria.where(type => keys.first)
    end
  else
    criteria
  end
end