architect4r 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/ReleaseNotes.md +4 -0
- data/lib/architect4r/model/queries.rb +23 -5
- data/lib/architect4r/version.rb +1 -1
- data/spec/model/queries_spec.rb +15 -0
- metadata +3 -3
data/ReleaseNotes.md
CHANGED
@@ -16,15 +16,33 @@ module Architect4r
|
|
16
16
|
|
17
17
|
# Fetch a record of the specified model based on its id
|
18
18
|
#
|
19
|
-
def find(
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def find(*ids)
|
20
|
+
# This code is taken from / inspired by activerecord:
|
21
|
+
# activerecord/lib/active_record/base.rb, line 1589
|
22
|
+
expects_array = ids.first.kind_of?(Array)
|
23
|
+
return ids.first if expects_array && ids.first.empty?
|
24
|
+
|
25
|
+
ids = ids.flatten.compact.uniq
|
26
|
+
|
27
|
+
case ids.size
|
28
|
+
when 0
|
29
|
+
raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} without an id!"))
|
30
|
+
when 1
|
31
|
+
id = ids.first.to_i
|
32
|
+
data = connection.execute_cypher("start s=node(#{self.model_root.id}), d=node(#{id}) match s<-[r:model_type]-d return d")
|
33
|
+
data &&= data['data'] && data['data'].flatten.first
|
34
|
+
result = self.build_from_database(data)
|
35
|
+
|
36
|
+
expects_array ? [ result ] : result
|
37
|
+
else
|
38
|
+
ids = ids.map { |i| i.to_i }.uniq.join(',')
|
39
|
+
find_by_cypher("start s=node(#{self.model_root.id}), d=node(#{ids}) match s<-[r:model_type]-d return d", 'd')
|
40
|
+
end
|
23
41
|
end
|
24
42
|
alias :find_by_id :find
|
25
43
|
|
26
44
|
def find!(id)
|
27
|
-
self.
|
45
|
+
self.find(id) || raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} with id #{id}!"))
|
28
46
|
end
|
29
47
|
alias :find_by_id! :find!
|
30
48
|
|
data/lib/architect4r/version.rb
CHANGED
data/spec/model/queries_spec.rb
CHANGED
@@ -36,6 +36,21 @@ describe "Node Queries" do
|
|
36
36
|
lambda { Person.find_by_id!(person.id) }.should_not raise_error(Architect4r::RecordNotFound)
|
37
37
|
end
|
38
38
|
|
39
|
+
it "should allow fetching multiple nodes by id" do
|
40
|
+
person1 = Person.create(:name => 'Agent 1', :human => false)
|
41
|
+
person2 = Person.create(:name => 'Agent 2', :human => false)
|
42
|
+
|
43
|
+
result = Person.find(person1.id, person2.id)
|
44
|
+
result.should have(2).persons
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return an array if the input was one" do
|
48
|
+
person = Person.create(:name => 'Agent X', :human => false)
|
49
|
+
|
50
|
+
result = Person.find([person.id])
|
51
|
+
result.should have(1).persons
|
52
|
+
end
|
53
|
+
|
39
54
|
end
|
40
55
|
|
41
56
|
describe "counting records" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: architect4r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 3
|
10
|
+
version: 0.4.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Maximilian Schulz
|