module Elastictastic::ParentChild

Public Class Methods

new(attributes = {}) click to toggle source
Calls superclass method
# File lib/elastictastic/parent_child.rb, line 45
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

Public Instance Methods

elasticsearch_doc=(doc) click to toggle source
Calls superclass method
# File lib/elastictastic/parent_child.rb, line 55
def elasticsearch_doc=(doc)
  @_parent_id = doc.delete('_parent')
  super
end
parent=(parent) click to toggle source
# File lib/elastictastic/parent_child.rb, line 85
def parent=(parent)
  if @_parent
    raise Elastictastic::IllegalModificationError,
      "Document is already a child of #{_parent}"
  end
  if persisted?
    raise Elastictastic::IllegalModificationError,
      "Can't change parent of persisted object"
  end
  #TODO - here's a piece of debugging to fix a problem where we get weird parents. remove after fixing
  if parent && !parent.respond_to?(:id)
    raise ArgumentError.new("Bad parent loaded from id #{parent_id} is a #{parent.class.name}.")
  end
  @_parent = parent
end
save(options = {}) click to toggle source
Calls superclass method
# File lib/elastictastic/parent_child.rb, line 101
def save(options = {})
  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

Protected Instance Methods

read_child(field_name) click to toggle source
# File lib/elastictastic/parent_child.rb, line 112
def read_child(field_name)
  @_children[field_name.to_s]
end