Skip to content

Commit

Permalink
pretty preloader
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Jan 29, 2015
1 parent f89b200 commit f2c18ce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
12 changes: 6 additions & 6 deletions lib/neo4j/active_node/query/query_proxy_methods.rb
Expand Up @@ -124,19 +124,19 @@ def optional(association, node_id = nil)
self.query.proxy_as(model, var, true)
end

def includes(association_name, given_child_id = nil, given_rel_id = nil)
prepopulate(false, association_name, given_child_id, given_rel_id)
def includes(association_name, given_child_id = nil, rel_id = nil)
prepopulate(false, association_name, given_child_id, rel_id)
end

def includes_filtered(association_name, given_child_id = nil, given_rel_id = nil)
prepopulate(true, association_name, given_child_id, given_rel_id)
def includes_filtered(association_name, given_child_id = nil, rel_id = nil)
prepopulate(true, association_name, given_child_id, rel_id)
end

protected

def prepopulate(filtered, association_name, child_id, rel_id)
def prepopulate(filtered, association_name, given_child_id, rel_id)
preloader_class = filtered ? Neo4j::ActiveNode::Query::QueryProxyFilteredPreloader : Neo4j::ActiveNode::Query::QueryProxyPreloader
preloader_class.new(self, child_id).tap { |p| p.initial_queue(association_name, child_id, rel_id) }
preloader_class.new(self, given_child_id).tap { |p| p.initial_queue(association_name, given_child_id, rel_id) }
end

private
Expand Down
5 changes: 2 additions & 3 deletions lib/neo4j/active_node/query/query_proxy_preloader.rb
Expand Up @@ -5,19 +5,18 @@ class QueryProxyPreloader
attr_reader :queued_methods, :caller, :target_id, :child_id
delegate :each, :each_with_rel, :each_rel, :to_a, :first, :last, :to_cypher, to: :caller

def initialize(query_proxy, child_id)
def initialize(query_proxy, given_child_id)
@caller = query_proxy
@target_id = caller.identity
@child_id = child_id || :"#{target_id}_child"
@queued_methods = {}
end

def initial_queue(association_name, given_child_id, rel_id)
@child_id = given_child_id || :"#{target_id}_child"
@child_id = given_child_id || child_id
@caller = caller.query.proxy_as_optional(caller.model, target_id).send(association_name, child_id, rel_id)
caller.instance_variable_set(:@preloader, self)
queue association_name
self
end

def queue(method_name, *args)
Expand Down

0 comments on commit f2c18ce

Please sign in to comment.