Skip to content

Commit

Permalink
match_to supports arrays!
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Dec 6, 2014
1 parent 27ffb20 commit 4f2a17d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/neo4j/active_node/query/query_proxy_methods.rb
Expand Up @@ -68,12 +68,16 @@ def delete_all(identifier = nil)
# When it's a node, it'll use the object's neo_id, which is fastest. When not nil, it'll figure out the
# primary key of that model. When nil, it uses `1 = 2` to prevent matching all records, which is the default
# behavior when nil is passed to `where` in QueryProxy.
# @param [#neo_id, String, Enumerable] node A node, a string representing a node's ID, or an enumerable of nodes or IDs.
# @return [Neo4j::ActiveNode::Query::QueryProxy] A QueryProxy object upon which you can build.
def match_to(node)
if node.respond_to?(:neo_id)
self.where(neo_id: node.neo_id)
elsif !node.nil?
id_key = self.association.nil? ? model.primary_key : self.association.target_class.primary_key
if node.is_a?(Array)
node = node.first.respond_to?(:id) ? node.map!(&:id) : node
end
self.where(id_key => node)
else
# support for null object pattern
Expand Down
20 changes: 20 additions & 0 deletions spec/e2e/query_proxy_methods_spec.rb
Expand Up @@ -261,6 +261,26 @@ class IncludeEnrolledIn
end
end

context 'with an array' do
context 'of nodes' do
after { @john.lessons.first_rel_to(@math).destroy }

it 'generates cypher using IN with the IDs of contained nodes' do
expect(@john.lessons.match_to([@history, @math]).to_cypher).to include ('AND result.uuid IN')
expect(@john.lessons.match_to([@history, @math]).to_a).to eq [@history]
@john.lessons << @math
expect(@john.lessons.match_to([@history, @math]).to_a.count).to eq 2
expect(@john.lessons.match_to([@history, @math]).to_a).to include(@history, @math)
end
end

context 'of IDs' do
it 'allows an array of IDs' do
expect(@john.lessons.match_to([@history.id]).to_a).to eq [@history]
end
end
end

context 'with a null object' do
it 'generates cypher with 1 = 2' do
expect(@john.lessons.match_to(nil).to_cypher).to include('AND 1 = 2')
Expand Down

0 comments on commit 4f2a17d

Please sign in to comment.