Skip to content
Permalink
Browse files

fix the variable of the last link within branch

  • Loading branch information...
klobuczek committed Oct 25, 2018
1 parent 95db4f8 commit 2b2a5a19d67e4e1a9c27179756e7d0f6383dd7ed
Showing with 20 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +12 −4 lib/neo4j/active_node/query/query_proxy.rb
  3. +7 −1 spec/e2e/query_proxy_methods_spec.rb
@@ -22,6 +22,7 @@ b/
*~
.ruby-version
.ruby-gemset
.jrubyrc

# vim
.*.sw[a-z]
@@ -190,9 +190,13 @@ def branch(&block)
# `as(identity)` is here to make sure we get the right variable
# There might be a deeper problem of the variable changing when we
# traverse an association
as(identity).instance_eval(&block).query.proxy_as(self.model, identity).tap do |new_query_proxy|
propagate_context(new_query_proxy)
end
# branch_end required as otherwise the last variable of branch may coincide with the main result variable
as(identity).instance_eval(&block).tap(&:branch_end).query.proxy_as(self.model, identity)
.tap(&method(:propagate_context))
end

def branch_end
@node_var ||= default_var(_chain_level + 1)
end

def [](index)
@@ -336,7 +340,7 @@ def _association_chain_var
if start_object
:"#{start_object.class.name.gsub('::', '_').downcase}#{start_object.neo_id}"
else
@query_proxy.node_var || :"node#{_chain_level}"
@query_proxy.node_var || default_var
end
end

@@ -355,6 +359,10 @@ def _rel_chain_var

private

def default_var(index = _chain_level)
:"node#{index}"
end

def instance_vars_from_options!(options)
@node_var, @session, @source_object, @starting_query, @optional,
@start_object, @query_proxy, @chain_level, @association_labels,
@@ -809,9 +809,11 @@ def destroy_called
@john = Student.create(name: 'John')
@bill = Student.create(name: 'Bill')
@history = Lesson.create(name: 'history')
@math = Lesson.create(name: 'math')
@jim = Teacher.create(name: 'Jim', age: 40)
3.times { @john.lessons << @history }
@john.lessons << @history
@history.teachers << @jim
@math.teachers << @jim
end

it 'returns a QueryProxy object' do
@@ -850,6 +852,10 @@ def destroy_called
expect(result).to eq [jummy]
end

it 'returns johns lessons whose teachers not only teach history' do
expect(@john.lessons.branch { teachers.lessons.where_not(name: 'history')}).to include(@history)
end

it 'raises LocalJumpError when no block is passed' do
expect { @john.lessons.branch }.to raise_error LocalJumpError
end

0 comments on commit 2b2a5a1

Please sign in to comment.
You can’t perform that action at this time.