Class: Elastictastic::ChildCollectionProxy

Inherits:
Scope
  • Object
show all
Defined in:
lib/elastictastic/child_collection_proxy.rb

Instance Attribute Summary collapse

Attributes inherited from Scope

#clazz, #index

Instance Method Summary collapse

Methods inherited from Scope

#all, #all_facets, #any?, #count, #destroy_all, #empty?, #find, #find_each, #find_in_batches, #inspect, #method_missing, #params, #scoped, #sync_mapping

Constructor Details

#initialize(association, parent) ⇒ ChildCollectionProxy

Returns a new instance of ChildCollectionProxy.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/elastictastic/child_collection_proxy.rb', line 5

def initialize(association, parent)
  super(
    parent.index,
    association.clazz,
    Search.new(
      'query' => {
        'constant_score' => {
          'filter' => { 'term' => { '_parent' => parent.id }}
        }
      }
    )
  )
  @parent = parent
  @parent_collection = self
  @transient_children = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Elastictastic::Scope

Instance Attribute Details

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/elastictastic/child_collection_proxy.rb', line 3

def parent
  @parent
end

#transient_children ⇒ Object (readonly)

Returns the value of attribute transient_children.



3
4
5
# File 'lib/elastictastic/child_collection_proxy.rb', line 3

def transient_children
  @transient_children
end

Instance Method Details

#<<(child) ⇒ Object



44
45
46
47
48
# File 'lib/elastictastic/child_collection_proxy.rb', line 44

def <<(child)
  child.parent_collection = self
  @transient_children << child
  self
end

#each(&block) ⇒ Object



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

def each(&block)
  if block
    super
    @transient_children.each(&block)
  else
    ::Enumerator.new(self, :each)
  end
end

#first ⇒ Object



27
28
29
# File 'lib/elastictastic/child_collection_proxy.rb', line 27

def first
  super || @transient_children.first
end

#initialize_instance(instance) ⇒ Object



22
23
24
25
# File 'lib/elastictastic/child_collection_proxy.rb', line 22

def initialize_instance(instance)
  super
  self << instance
end

#persisted!(child) ⇒ Object



40
41
42
# File 'lib/elastictastic/child_collection_proxy.rb', line 40

def persisted!(child)
  @transient_children.delete(child)
end