-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinclusion.rb
More file actions
65 lines (54 loc) · 2.19 KB
/
inclusion.rb
File metadata and controls
65 lines (54 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Mongoid
module Includes
# Public: Represents a relation that needs to be eager loaded.
class Inclusion < SimpleDelegator
# Public: Convenience getter for the wrapped metadata.
alias_method :metadata, :__getobj__
def initialize(metadata, options = {})
super(metadata)
@options = options
end
# Public: Returns true if the relation is not direct.
def nested?
!!from
end
# Public: Checks if the collection already has an inclusion with the
# specified metadata.
def eql?(other)
metadata == other && (!other.respond_to?(:from) || from == other.from)
end
# Public: Returns true if the relation is a polymorphic belongs_to.
def polymorphic_belongs_to?
metadata.polymorphic? && metadata.relation == Mongoid::Association::Referenced::BelongsTo::Proxy
end
# Public: Name of the relation from which a nested inclusion is performed.
def from
@from ||= @options[:from]
end
# Internal: Proc that will modify the documents to include in the relation.
def modifier
@modifier ||= @options[:with]
end
# Public: Preloads the documents for the relation. Uses a custom block
# if one was provided, or fetches them using the class and the foreign key.
def load_documents_for(foreign_key, foreign_key_values)
# NOTE: We assume the upstream code in Mongoid is removing nil keys.
return klass.none if foreign_key_values.empty?
docs = klass.any_in(foreign_key => foreign_key_values)
modifier ? modifier.call(docs) : docs
end
# Public: Clones the inclusion and changes the Mongoid::Metadata::Relation
# that it wraps to make it non polymorphic and target a particular class.
#
# Returns an Inclusion that can be eager loaded as usual.
def for_class_name(class_name)
Inclusion.new metadata.clone.instance_eval { |relation_metadata|
@options = @options.dup
@options[:class_name] = @class_name = class_name
@options[:polymorphic], @options[:as], @polymorphic, @klass = nil
self
}, with: @modifier
end
end
end
end