def get_parent(child, parent = nil)
child_value = child_key.get(child)
return nil if child_value.any? { |v| v.nil? }
with_repository(parent || parent_model) do
parent_identity_map = (parent || parent_model).repository.identity_map(parent_model.base_model)
child_identity_map = child.repository.identity_map(child_model.base_model)
if parent = parent_identity_map[child_value]
return parent
end
children = child_identity_map.values
children << child unless child_identity_map[child.key]
bind_values = children.map { |c| child_key.get(c) }.uniq
query_values = bind_values.reject { |k| parent_identity_map[k] }
bind_values = query_values unless query_values.empty?
query = parent_key.zip(bind_values.transpose).to_hash
association_accessor = "#{self.name}_association"
collection = parent_model.send(:all, query)
unless collection.empty?
collection.send(:lazy_load)
children.each do |c|
c.send(association_accessor).instance_variable_set(:@parent, collection.get(*child_key.get(c)))
end
child.send(association_accessor).instance_variable_get(:@parent)
end
end
end