Skip to content

Commit

Permalink
For logging cypher queries output association chain
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Aug 25, 2014
1 parent 7e89f55 commit 986f883
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/neo4j/active_node/has_n.rb
Expand Up @@ -68,7 +68,7 @@ def #{name}(node = nil, rel = nil)
query_proxy: self.query_proxy,
node: node,
rel: rel,
context: '#{self.name}##{name}'
context: (self.query_proxy ? self.query_proxy.context : '#{self.name}') + '##{name}'
})
end}, __FILE__, __LINE__)
end
Expand Down Expand Up @@ -101,18 +101,19 @@ def #{name}_rel
def #{name}(node = nil, rel = nil)
return nil unless self.persisted?
#{name}_query_proxy(node: node, rel: rel).first
#{name}_query_proxy(node: node, rel: rel, context: '#{self.name}##{name}').first
end}, __FILE__, __LINE__)

instance_eval(%Q{
def #{name}_query_proxy(options = {})
Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class_name},
@associations[#{name.inspect}],
{session: self.neo4j_session, context: '#{self.name}##{name}'}.merge(options))
{session: self.neo4j_session}.merge(options))
end
def #{name}(node = nil, rel = nil)
#{name}_query_proxy(query_proxy: self.query_proxy, node: node, rel: rel)
context = (self.query_proxy && self.query_proxy.context ? self.query_proxy.context : '#{self.name}') + '##{name}'
#{name}_query_proxy(query_proxy: self.query_proxy, node: node, rel: rel, context: context)
end}, __FILE__, __LINE__)
end

Expand Down
7 changes: 6 additions & 1 deletion lib/neo4j/active_node/query/query_proxy.rb
Expand Up @@ -8,6 +8,7 @@ class QueryProxy
def initialize(model, association = nil, options = {})
@model = model
@association = association
@context = options.delete(:context)
@options = options
@node_var = options[:node]
@rel_var = options[:rel] || _rel_chain_var
Expand Down Expand Up @@ -148,6 +149,8 @@ def method_missing(method_name, *args)
end
end

attr_reader :context

protected
# Methods are underscored to prevent conflict with user class methods

Expand All @@ -168,7 +171,7 @@ def _query_model_as(var)
else
var
end
_session.query(context: @options[:context]).match(match_arg)
_session.query(context: @context).match(match_arg)
end

def _session
Expand Down Expand Up @@ -213,6 +216,8 @@ def _rel_chain_var
:"rel#{_chain_level - 1}"
end

attr_writer :context

private

def call_class_method(method_name, *args)
Expand Down

0 comments on commit 986f883

Please sign in to comment.