def get_children(parent, options = {}, finder = :all, *args)
parent_value = parent_key.get(parent)
bind_values = [ parent_value ]
with_repository(child_model) do |r|
parent_identity_map = parent.repository.identity_map(parent_model)
child_identity_map = r.identity_map(child_model)
query_values = parent_identity_map.keys
query_values.reject! { |k| child_identity_map[k] }
bind_values = query_values unless query_values.empty?
query = child_key.zip(bind_values.transpose).to_hash
collection = child_model.send(finder, *(args.dup << @query.merge(options).merge(query)))
return collection unless collection.kind_of?(Collection) && collection.any?
grouped_collection = {}
collection.each do |resource|
child_value = child_key.get(resource)
parent_obj = parent_identity_map[child_value]
grouped_collection[parent_obj] ||= []
grouped_collection[parent_obj] << resource
end
association_accessor = "#{self.name}_association"
ret = nil
grouped_collection.each do |parent, children|
association = parent.send(association_accessor)
query = collection.query.dup
query.conditions.map! do |operator, property, bind_value|
if operator != :raw && child_key.has_property?(property.name)
bind_value = *children.map { |child| property.get(child) }.uniq
end
[ operator, property, bind_value ]
end
parents_children = Collection.new(query)
children.each { |child| parents_children.send(:add, child) }
if parent_key.get(parent) == parent_value
ret = parents_children
else
association.instance_variable_set(:@children, parents_children)
end
end
ret || child_model.send(finder, *(args.dup << @query.merge(options).merge(child_key.zip([ parent_value ]).to_hash)))
end
end