Skip to content

Commit

Permalink
Merge pull request #426 from subvertallchris/helpful_methods
Browse files Browse the repository at this point in the history
adds exists? method
  • Loading branch information
subvertallchris committed Aug 21, 2014
2 parents ad6cd9a + 0b9653b commit 488820d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
8 changes: 7 additions & 1 deletion lib/neo4j/active_node/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ def count
end

def include?(other)
self.query_as(:n).limit(1).where("ID(n) = #{other.neo_id}").return("count(n) AS count").first.count == 1
self.query_as(:n).where("ID(n) = #{other.neo_id}").return("count(n) AS count").first.count > 0
end

def exists?(node_id=nil)
start_q = self.query_as(:n)
end_q = node_id.nil? ? start_q : start_q.where("ID(n) = #{node_id}")
end_q.return("COUNT(n) AS count").first.count > 0
end

# Returns the object with the specified neo4j id.
Expand Down
6 changes: 5 additions & 1 deletion lib/neo4j/active_node/query/query_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ def count
end

def include?(other)
call_class_method(:include?, *other)
call_class_method(:include?, other)
end

def exists?(node_id=nil)
call_class_method(:exists?, node_id)
end

# QueryProxy objects act as a representation of a model at the class level so we pass through calls
Expand Down
47 changes: 36 additions & 11 deletions spec/e2e/active_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class MyRelClass
end
end

describe 'include?' do
describe 'include?, exists?' do
#goofy names to differentiate from same classes used elsewhere
before(:all) do
class IncludeLesson; end
Expand Down Expand Up @@ -548,16 +548,41 @@ class IncludeTeacher
let!(:mr_jones) { IncludeTeacher.create }
let!(:mr_adams) { IncludeTeacher.create }

it 'correctly reports when a node is included in a query result' do
jimmy.lessons << science
science.teachers << mr_adams
expect(jimmy.lessons.include?(science)).to be_truthy
expect(jimmy.lessons.include?(math)).to be_falsey
expect(jimmy.lessons.teachers.include?(mr_jones)).to be_falsey
expect(jimmy.lessons.where(name: 'science').teachers.include?(mr_jones)).to be_falsey
expect(jimmy.lessons.where(name: 'science').teachers.include?(mr_adams)).to be_truthy
expect(IncludeTeacher.include?(mr_jones)).to be_truthy
expect(IncludeTeacher.include?(math)).to be_falsey
describe 'include?' do
it 'correctly reports when a node is included in a query result' do
jimmy.lessons << science
science.teachers << mr_adams
expect(jimmy.lessons.include?(science)).to be_truthy
expect(jimmy.lessons.include?(math)).to be_falsey
expect(jimmy.lessons.teachers.include?(mr_jones)).to be_falsey
expect(jimmy.lessons.where(name: 'science').teachers.include?(mr_jones)).to be_falsey
expect(jimmy.lessons.where(name: 'science').teachers.include?(mr_adams)).to be_truthy
expect(IncludeTeacher.include?(mr_jones)).to be_truthy
expect(IncludeTeacher.include?(math)).to be_falsey
end

it 'works with multiple relationships to the same object' do
jimmy.lessons << science
jimmy.lessons << science
expect(jimmy.lessons.include?(science)).to be_truthy
end
end

describe 'exists?' do
it 'can be run on a query' do
expect(IncludeLesson.where(name: 'math').exists?).to be_truthy
expect(IncludeLesson.where(name: 'history').exists?).to be_falsey
end

it 'can be run with a neo_id' do
expect(IncludeLesson.where(name: 'math').exists?(math.neo_id)).to be_truthy
expect(IncludeLesson.where(name: 'math').exists?(science.neo_id)).to be_falsey
end

it 'can be called by the class with a neo_id' do
expect(IncludeLesson.exists?(math.neo_id)).to be_truthy
expect(IncludeLesson.exists?(8675309)).to be_falsey
end
end
end

Expand Down

0 comments on commit 488820d

Please sign in to comment.