edge 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +3 -3
- data/edge.gemspec +1 -1
- data/lib/edge/forest.rb +6 -3
- data/lib/edge/version.rb +1 -1
- data/spec/forest_spec.rb +31 -28
- metadata +21 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1eb4aa8563c1847f2ecd36c8507834a80f81a80c
|
4
|
+
data.tar.gz: 1a08d6bca601260f9c1f4bad477357d2f6770cb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9460fc7955f919c88b240e4bd426d228d8b1451241530ced75b02d950093ca6c65d336f15baa501df85d9d87422eae9141a493117feee1f69c54d07565a69cd
|
7
|
+
data.tar.gz: 3634da19a17379187e192470ba7e73ee991cc197fe32347ab47a6b061f571689d5066026259e72239d31df555c5b56492e63e0564c7191e3df181a438f78d1ed
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -44,11 +44,11 @@ acts_as_forest.
|
|
44
44
|
Location.find_forest # [usa, canada] with all children and parents preloaded
|
45
45
|
Location.find_tree usa.id # load a single tree.
|
46
46
|
|
47
|
-
It also provides the
|
47
|
+
It also provides the with_descendants scope to get all currently selected
|
48
48
|
nodes and all their descendents. It can be chained after where scopes, but
|
49
49
|
must not be used after any other type of scope.
|
50
50
|
|
51
|
-
Location.where(name: "Illinois").
|
51
|
+
Location.where(name: "Illinois").with_descendants.all # [illinois, chicago]
|
52
52
|
|
53
53
|
## Benchmarks
|
54
54
|
|
@@ -94,4 +94,4 @@ Even on slower machines entire trees can be loaded quickly.
|
|
94
94
|
|
95
95
|
## License
|
96
96
|
|
97
|
-
MIT
|
97
|
+
MIT
|
data/edge.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
|
|
19
19
|
|
20
20
|
gem.add_development_dependency 'pg'
|
21
21
|
gem.add_development_dependency 'pry'
|
22
|
-
gem.add_development_dependency 'rspec', "~>
|
22
|
+
gem.add_development_dependency 'rspec', "~> 3.1.0"
|
23
23
|
gem.add_development_dependency 'guard', ">= 0.10.0"
|
24
24
|
gem.add_development_dependency 'guard-rspec', ">= 0.6.0"
|
25
25
|
end
|
data/lib/edge/forest.rb
CHANGED
@@ -50,7 +50,8 @@ module Edge
|
|
50
50
|
manager = recursive_manager.project(Arel.star)
|
51
51
|
manager.order(forest_order) if forest_order
|
52
52
|
|
53
|
-
|
53
|
+
bind_values = current_scope ? current_scope.bind_values : []
|
54
|
+
records = find_by_sql manager.to_sql, bind_values
|
54
55
|
|
55
56
|
records_by_id = records.each_with_object({}) { |r, h| h[r.id] = r }
|
56
57
|
|
@@ -95,7 +96,9 @@ module Edge
|
|
95
96
|
# Only where scopes can precede this in a scope chain
|
96
97
|
def with_descendants
|
97
98
|
manager = recursive_manager.project(arel_table[:id])
|
98
|
-
unscoped.where("#{table_name}.id in (#{manager.to_sql})")
|
99
|
+
scope = unscoped.where("#{table_name}.id in (#{manager.to_sql})")
|
100
|
+
scope.bind_values = current_scope.bind_values if current_scope
|
101
|
+
scope
|
99
102
|
end
|
100
103
|
|
101
104
|
private
|
@@ -152,7 +155,7 @@ module Edge
|
|
152
155
|
# Returns all descendants
|
153
156
|
def descendants
|
154
157
|
if children.present?
|
155
|
-
children + children.
|
158
|
+
children + children.flat_map(&:descendants)
|
156
159
|
else
|
157
160
|
[]
|
158
161
|
end
|
data/lib/edge/version.rb
CHANGED
data/spec/forest_spec.rb
CHANGED
@@ -17,19 +17,19 @@ describe "Edge::Forest" do
|
|
17
17
|
describe "root?" do
|
18
18
|
context "of root node" do
|
19
19
|
it "should be true" do
|
20
|
-
usa.root
|
20
|
+
expect(usa.root?).to eq true
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
context "of child node" do
|
25
25
|
it "should be false" do
|
26
|
-
illinois.root
|
26
|
+
expect(illinois.root?).to eq false
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
context "of leaf node" do
|
31
31
|
it "should be root node" do
|
32
|
-
chicago.root
|
32
|
+
expect(chicago.root?).to eq false
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
@@ -37,19 +37,19 @@ describe "Edge::Forest" do
|
|
37
37
|
describe "root" do
|
38
38
|
context "of root node" do
|
39
39
|
it "should be self" do
|
40
|
-
usa.root.
|
40
|
+
expect(usa.root).to eq usa
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
context "of child node" do
|
45
45
|
it "should be root node" do
|
46
|
-
illinois.root.
|
46
|
+
expect(illinois.root).to eq usa
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
context "of leaf node" do
|
51
51
|
it "should be root node" do
|
52
|
-
chicago.root.
|
52
|
+
expect(chicago.root).to eq usa
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
@@ -57,19 +57,19 @@ describe "Edge::Forest" do
|
|
57
57
|
describe "parent" do
|
58
58
|
context "of root node" do
|
59
59
|
it "should be nil" do
|
60
|
-
usa.parent.
|
60
|
+
expect(usa.parent).to eq nil
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
context "of child node" do
|
65
65
|
it "should be parent" do
|
66
|
-
illinois.parent.
|
66
|
+
expect(illinois.parent).to eq usa
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
70
|
context "of leaf node" do
|
71
71
|
it "should be parent" do
|
72
|
-
chicago.parent.
|
72
|
+
expect(chicago.parent).to eq illinois
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -77,13 +77,13 @@ describe "Edge::Forest" do
|
|
77
77
|
describe "ancestors" do
|
78
78
|
context "of root node" do
|
79
79
|
it "should be empty" do
|
80
|
-
usa.ancestors.
|
80
|
+
expect(usa.ancestors).to be_empty
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
84
|
context "of leaf node" do
|
85
85
|
it "should be ancestors ordered by ascending distance" do
|
86
|
-
chicago.ancestors.
|
86
|
+
expect(chicago.ancestors).to eq [illinois, usa]
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
@@ -91,49 +91,49 @@ describe "Edge::Forest" do
|
|
91
91
|
describe "siblings" do
|
92
92
|
context "of root node" do
|
93
93
|
it "should be empty" do
|
94
|
-
usa.siblings.
|
94
|
+
expect(usa.siblings).to be_empty
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
98
|
context "of child node" do
|
99
99
|
it "should be other children of parent" do
|
100
|
-
illinois.siblings.
|
100
|
+
expect(illinois.siblings).to include(indiana)
|
101
101
|
end
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
105
|
describe "children" do
|
106
106
|
it "should be children" do
|
107
|
-
usa.children.
|
107
|
+
expect(usa.children).to include(illinois, indiana)
|
108
108
|
end
|
109
109
|
|
110
110
|
it "should be ordered" do
|
111
111
|
alabama = Location.create! :parent => usa, :name => "Alabama"
|
112
|
-
usa.children.
|
112
|
+
expect(usa.children).to eq [alabama, illinois, indiana]
|
113
113
|
end
|
114
114
|
|
115
115
|
context "of leaf" do
|
116
116
|
it "should be empty" do
|
117
|
-
chicago.children.
|
117
|
+
expect(chicago.children).to be_empty
|
118
118
|
end
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
122
122
|
describe "descendants" do
|
123
123
|
it "should be all descendants" do
|
124
|
-
usa.descendants.
|
124
|
+
expect(usa.descendants).to include(illinois, indiana, chicago)
|
125
125
|
end
|
126
126
|
|
127
127
|
context "of leaf" do
|
128
128
|
it "should be empty" do
|
129
|
-
chicago.descendants.
|
129
|
+
expect(chicago.descendants).to be_empty
|
130
130
|
end
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
134
134
|
describe "root scope" do
|
135
135
|
it "returns only root nodes" do
|
136
|
-
Location.root.
|
136
|
+
expect(Location.root).to include(usa, canada)
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
@@ -144,8 +144,8 @@ describe "Edge::Forest" do
|
|
144
144
|
Location.where("purposely fail if any Location find happens here").scoping do
|
145
145
|
forest.each do |tree|
|
146
146
|
tree.descendants.each do |node|
|
147
|
-
node.parent.
|
148
|
-
node.children.
|
147
|
+
expect(node.parent).to be
|
148
|
+
expect(node.children).to be_kind_of(ActiveRecord::Associations::CollectionProxy)
|
149
149
|
end
|
150
150
|
end
|
151
151
|
end
|
@@ -153,14 +153,14 @@ describe "Edge::Forest" do
|
|
153
153
|
|
154
154
|
it "works when scoped" do
|
155
155
|
forest = Location.where(:name => "USA").find_forest
|
156
|
-
forest.
|
156
|
+
expect(forest).to include(usa)
|
157
157
|
end
|
158
158
|
|
159
159
|
it "preloads children in proper order" do
|
160
160
|
alabama = Location.create! :parent => usa, :name => "Alabama"
|
161
161
|
forest = Location.find_forest
|
162
162
|
tree = forest.find { |l| l.id == usa.id }
|
163
|
-
tree.children.
|
163
|
+
expect(tree.children).to eq [alabama, illinois, indiana]
|
164
164
|
end
|
165
165
|
|
166
166
|
context "with an infinite loop" do
|
@@ -177,12 +177,12 @@ describe "Edge::Forest" do
|
|
177
177
|
describe "find_tree" do
|
178
178
|
it "finds by id" do
|
179
179
|
tree = Location.find_tree usa.id
|
180
|
-
tree.
|
180
|
+
expect(tree).to eq usa
|
181
181
|
end
|
182
182
|
|
183
183
|
it "finds multiple trees by id" do
|
184
184
|
trees = Location.find_tree [indiana.id, illinois.id]
|
185
|
-
trees.
|
185
|
+
expect(trees).to include(indiana, illinois)
|
186
186
|
end
|
187
187
|
|
188
188
|
it "raises ActiveRecord::RecordNotFound when id is not found" do
|
@@ -198,17 +198,20 @@ describe "Edge::Forest" do
|
|
198
198
|
describe "with_descendants" do
|
199
199
|
context "unscoped" do
|
200
200
|
it "returns all records" do
|
201
|
-
Location.with_descendants.to_a
|
201
|
+
rows = Location.with_descendants.to_a
|
202
|
+
expect(rows).to match_array Location.all
|
202
203
|
end
|
203
204
|
end
|
204
205
|
|
205
206
|
context "scoped" do
|
206
207
|
it "returns a new scope that includes previously scoped records and their descendants" do
|
207
|
-
Location.where(id: canada.id).with_descendants.to_a
|
208
|
+
rows = Location.where(id: canada.id).with_descendants.to_a
|
209
|
+
expect(rows).to match_array [canada, british_columbia]
|
208
210
|
end
|
209
211
|
|
210
212
|
it "is not commutative" do
|
211
|
-
Location.with_descendants.where(id: canada.id).to_a
|
213
|
+
rows = Location.with_descendants.where(id: canada.id).to_a
|
214
|
+
expect(rows).to eq [canada]
|
212
215
|
end
|
213
216
|
end
|
214
217
|
end
|
metadata
CHANGED
@@ -1,97 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: edge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Christensen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.2.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 3.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: pg
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: pry
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 3.1.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 3.1.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: guard
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: 0.10.0
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.10.0
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: guard-rspec
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: 0.6.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 0.6.0
|
97
97
|
description: Graph functionality for ActiveRecord
|
@@ -101,8 +101,8 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
-
- .gitignore
|
105
|
-
- .rspec
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
106
|
- CHANGELOG.md
|
107
107
|
- Gemfile
|
108
108
|
- Guardfile
|
@@ -130,17 +130,17 @@ require_paths:
|
|
130
130
|
- lib
|
131
131
|
required_ruby_version: !ruby/object:Gem::Requirement
|
132
132
|
requirements:
|
133
|
-
- -
|
133
|
+
- - ">="
|
134
134
|
- !ruby/object:Gem::Version
|
135
135
|
version: '0'
|
136
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
137
|
requirements:
|
138
|
-
- -
|
138
|
+
- - ">="
|
139
139
|
- !ruby/object:Gem::Version
|
140
140
|
version: '0'
|
141
141
|
requirements: []
|
142
142
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.
|
143
|
+
rubygems_version: 2.2.2
|
144
144
|
signing_key:
|
145
145
|
specification_version: 4
|
146
146
|
summary: Graph functionality for ActiveRecord. Provides tree/forest modeling structure
|