Module: Elastictastic::ParentChild::InstanceMethods

Defined in:
lib/elastictastic/parent_child.rb

Instance Method Summary collapse

Instance Method Details

#_parent ⇒ Object

:nodoc:



61
62
63
64
65
66
67
# File 'lib/elastictastic/parent_child.rb', line 61

def _parent #:nodoc:
  return @parent if defined? @parent
  @parent =
    if @parent_id
      self.class.parent_association.clazz.find(@parent_id)
    end
end

#_parent_id ⇒ Object

:nodoc:



69
70
71
72
73
74
75
# File 'lib/elastictastic/parent_child.rb', line 69

def _parent_id #:nodoc:
  if @parent
    @parent_id = @parent.id
  elsif @parent_id
    @parent_id
  end
end

#elasticsearch_doc=(doc) ⇒ Object



56
57
58
59
# File 'lib/elastictastic/parent_child.rb', line 56

def elasticsearch_doc=(doc)
  @parent_id = doc.delete('_parent')
  super
end

#initialize(attributes = {}) ⇒ Object



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

def initialize(attributes = {})
  super
  @children = Hash.new do |hash, child_association_name|
    hash[child_association_name] = Elastictastic::ChildCollectionProxy.new(
      self.class.child_association(child_association_name.to_s),
      self
    )
  end
end

#parent_collection=(parent_collection) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/elastictastic/parent_child.rb', line 77

def parent_collection=(parent_collection)
  if @parent_collection
    raise Elastictastic::IllegalModificationError,
      "Document is already a child of #{_parent}"
  end
  if persisted?
    raise Elastictastic::IllegalModificationError,
      "Can't change parent of persisted object"
  end
  @parent_collection = parent_collection
  @parent = parent_collection.parent
end

#persisted! ⇒ Object



99
100
101
102
103
104
105
# File 'lib/elastictastic/parent_child.rb', line 99

def persisted!
  was_persisted = @persisted
  super
  if @parent_collection && !was_persisted
    @parent_collection.persisted!(self)
  end
end

#save ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/elastictastic/parent_child.rb', line 90

def save
  super
  self.class.child_associations.each_pair do |name, association|
    association.extract(self).transient_children.each do |child|
      child.save unless child.pending_save?
    end
  end
end