Module: Elastictastic::ParentChild

Extended by:
ActiveSupport::Concern
Defined in:
lib/elastictastic/parent_child.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_parentObject

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/elastictastic/parent_child.rb', line 60

def _parent #:nodoc:
  return @_parent if defined? @_parent
  @_parent =
    if @_parent_id
      self.class.parent_association.clazz.in_index(index).find(@_parent_id)
    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
end

#_parent_idObject

:nodoc:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/elastictastic/parent_child.rb', line 73

def _parent_id #:nodoc:
  if @_parent_id
    @_parent_id
  elsif @_parent
    unless @_parent.respond_to?(:id)
      raise ArgumentError,
        "@_parent is incorrectly set to #{Object.instance_method(:inspect).bind(@_parent).call}"
    end
    @_parent_id = @_parent.id
  end
end

#elasticsearch_doc=(doc) ⇒ Object



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

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

#initialize(attributes = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# 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

#parent=(parent) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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 = {}) ⇒ Object



101
102
103
104
105
106
107
108
# 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