with_recursive_tree 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/with_recursive_tree/version.rb +1 -1
- data/lib/with_recursive_tree.rb +55 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e14936619fe555cbfb5024c1284f48645ce74ce15ea4f8d0986ba29181b4bd9d
|
4
|
+
data.tar.gz: a1edfad9f5e95ba7aa8a812703b8c73288840bf6aeb56a2334a47b7bbdd58d76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3eab819caf4873be68501e1f0c4458d1e7c3dfdadc76331935be5ef93840ba57938dbbbdae48666e2e7bb63c5e7cca6106cf6b757fc51ae3214acc761138d974
|
7
|
+
data.tar.gz: deb8c83129e99787021939d4176bbef60b45e1edcf1f572d3076abe24e361205284e346f0320f68437e8eac0f13484b55d9975b6047af1bae5a21768ee3cd238
|
data/lib/with_recursive_tree.rb
CHANGED
@@ -6,8 +6,22 @@ module WithRecursiveTree
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
included do
|
9
|
-
scope :bfs, -> {
|
10
|
-
|
9
|
+
scope :bfs, -> {
|
10
|
+
if defined?(ActiveRecord::ConnectionAdapters::MySQL)
|
11
|
+
order :depth, with_recursive_tree_order
|
12
|
+
else
|
13
|
+
order :depth
|
14
|
+
end
|
15
|
+
}
|
16
|
+
scope :dfs, -> do
|
17
|
+
if defined?(ActiveRecord::ConnectionAdapters::MySQL)
|
18
|
+
order with_recursive_tree_order, :path
|
19
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::PostgreSQL)
|
20
|
+
order :path
|
21
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::SQLite3)
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
11
25
|
end
|
12
26
|
|
13
27
|
class_methods do
|
@@ -24,6 +38,14 @@ module WithRecursiveTree
|
|
24
38
|
def roots
|
25
39
|
where with_recursive_tree_foreign_key => nil
|
26
40
|
end
|
41
|
+
|
42
|
+
def with_recursive_tree_order_column
|
43
|
+
if with_recursive_tree_order.is_a?(Hash)
|
44
|
+
with_recursive_tree_order.keys.first
|
45
|
+
else
|
46
|
+
with_recursive_tree_order.to_s.split(" ").first
|
47
|
+
end
|
48
|
+
end
|
27
49
|
end
|
28
50
|
|
29
51
|
def ancestors
|
@@ -52,21 +74,43 @@ module WithRecursiveTree
|
|
52
74
|
end
|
53
75
|
|
54
76
|
def self_and_ancestors
|
55
|
-
self.class.
|
56
|
-
|
77
|
+
self.class.with_recursive(
|
78
|
+
tree: [
|
57
79
|
self.class.where(self.class.with_recursive_tree_primary_key => send(self.class.with_recursive_tree_primary_key)),
|
58
|
-
self.class.joins("JOIN
|
80
|
+
self.class.joins("JOIN tree ON #{self.class.table_name}.#{self.class.with_recursive_tree_primary_key} = tree.#{self.class.with_recursive_tree_foreign_key}")
|
59
81
|
]
|
60
|
-
).select("*").from("
|
82
|
+
).select("*").from("tree AS #{self.class.table_name}")
|
61
83
|
end
|
62
84
|
|
63
85
|
def self_and_descendants
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
86
|
+
anchor_path = if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
87
|
+
"ARRAY[#{self.class.with_recursive_tree_order_column}]::text[]"
|
88
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::MySQL)
|
89
|
+
"CAST(CONCAT('/', #{self.class.with_recursive_tree_primary_key}, '/') AS CHAR(512))"
|
90
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
|
91
|
+
"'/' || #{self.class.with_recursive_tree_primary_key} || '/'"
|
92
|
+
end
|
93
|
+
|
94
|
+
recursive_path = if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
95
|
+
"tree.path || #{self.class.table_name}.#{self.class.with_recursive_tree_order_column}::text"
|
96
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::MySQL)
|
97
|
+
"CONCAT(tree.path, #{self.class.table_name}.#{self.class.with_recursive_tree_primary_key}, '/')"
|
98
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::SQLite3)
|
99
|
+
"tree.path || #{self.class.table_name}.#{self.class.with_recursive_tree_primary_key} || '/'"
|
100
|
+
end
|
101
|
+
|
102
|
+
recursive_query = self.class.joins("JOIN tree ON #{self.class.table_name}.#{self.class.with_recursive_tree_foreign_key} = tree.#{self.class.with_recursive_tree_primary_key}").select("#{self.class.table_name}.*, #{recursive_path} AS path, depth + 1 AS depth")
|
103
|
+
|
104
|
+
unless defined?(ActiveRecord::ConnectionAdapters::MySQL)
|
105
|
+
recursive_query = recursive_query.order(self.class.with_recursive_tree_order)
|
106
|
+
end
|
107
|
+
|
108
|
+
self.class.with_recursive(
|
109
|
+
tree: [
|
110
|
+
self.class.where(self.class.with_recursive_tree_primary_key => send(self.class.with_recursive_tree_primary_key)).select("*, #{anchor_path} AS path, 0 AS depth"),
|
111
|
+
Arel.sql(recursive_query.to_sql)
|
68
112
|
]
|
69
|
-
).select("*").from("
|
113
|
+
).select("*").from("tree AS #{self.class.table_name}")
|
70
114
|
end
|
71
115
|
|
72
116
|
def self_and_siblings
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: with_recursive_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patricio Mac Adden
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|