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, #routing

Instance Method Summary collapse

Methods inherited from Scope

#[], #all, #all_facets, #any?, #count, #counts=, #destroy, #destroy_all, #empty?, #exists?, #find, #find_each, #find_in_batches, #find_many, #find_one, #inspect, #materialize_hit, #method_missing, #multi_get_params, #multi_search_headers, #params, #response=, #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 }}
        }
      }
    ),
    self
  )
  @parent = parent
  @transient_children = []
end

Dynamic Method Handling

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

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent


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

def parent
  @parent
end

Instance Method Details

#<<(child) ⇒ Object


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

def <<(child)
  child.parent = @parent
  @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 if @parent.persisted?
    transient_children.each(&block)
  else
    ::Enumerator.new(self, :each)
  end
end

#firstObject


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

#transient_childrenObject


46
47
48
49
50
51
52
# File 'lib/elastictastic/child_collection_proxy.rb', line 46

def transient_children
  @transient_children.tap do |children|
    children.reject! do |child|
      !child.transient?
    end
  end
end