with_recursive_tree 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c1ccdf38306a7ddff768b00b73f3d3ff58bf094f7524e55b63e0502f4d757dd
4
- data.tar.gz: 5572ccbc0023f7d7226b41f24ffa8513084dc2e8780075f48904dac3aa510a96
3
+ metadata.gz: e14936619fe555cbfb5024c1284f48645ce74ce15ea4f8d0986ba29181b4bd9d
4
+ data.tar.gz: a1edfad9f5e95ba7aa8a812703b8c73288840bf6aeb56a2334a47b7bbdd58d76
5
5
  SHA512:
6
- metadata.gz: 0a9003bb5a943e7a4d48abed8db293a292dd21dfae943ab1c4899673920fa1c943b5f949d4caf9351c763b3f62e0a14bbfca0ff3e9dfa8fe7daa8e9f4ea6462e
7
- data.tar.gz: 378da103d4c78855cc5c2acef7557e19a27eb2c3f11a4c63d26fa1e29dadff551d433d84060b0f7ef5bd2b711808d97b5c071e80e8f1ff5b61801ddca7a8bd38
6
+ metadata.gz: 3eab819caf4873be68501e1f0c4458d1e7c3dfdadc76331935be5ef93840ba57938dbbbdae48666e2e7bb63c5e7cca6106cf6b757fc51ae3214acc761138d974
7
+ data.tar.gz: deb8c83129e99787021939d4176bbef60b45e1edcf1f572d3076abe24e361205284e346f0320f68437e8eac0f13484b55d9975b6047af1bae5a21768ee3cd238
@@ -1,3 +1,3 @@
1
1
  module WithRecursiveTree
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -6,8 +6,22 @@ module WithRecursiveTree
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- scope :bfs, -> { order :depth }
10
- scope :dfs, -> { self }
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.with(search_tree: self.class.with_recursive(
56
- search_tree: [
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 search_tree ON #{self.class.table_name}.#{self.class.with_recursive_tree_primary_key} = search_tree.#{self.class.with_recursive_tree_foreign_key}")
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("search_tree")).from("search_tree AS #{self.class.table_name}")
82
+ ).select("*").from("tree AS #{self.class.table_name}")
61
83
  end
62
84
 
63
85
  def self_and_descendants
64
- self.class.with(search_tree: self.class.with_recursive(
65
- search_tree: [
66
- self.class.where(self.class.with_recursive_tree_primary_key => send(self.class.with_recursive_tree_primary_key)).select("*, '/' || #{self.class.with_recursive_tree_primary_key} || '/' AS path, 0 AS depth"),
67
- Arel.sql(self.class.joins("JOIN search_tree ON #{self.class.table_name}.#{self.class.with_recursive_tree_foreign_key} = search_tree.#{self.class.with_recursive_tree_primary_key}").select("#{self.class.table_name}.*, search_tree.path || #{self.class.table_name}.#{self.class.with_recursive_tree_primary_key} || '/' AS path, depth + 1 AS depth").order(self.class.with_recursive_tree_order).to_sql)
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("search_tree")).from("search_tree AS #{self.class.table_name}")
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.0
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-09 00:00:00.000000000 Z
11
+ date: 2024-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord