pg_ltree 1.2.1 → 2.0.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 +69 -0
- data/README.md +166 -0
- data/Rakefile +10 -1
- data/lib/pg_ltree/base.rb +2 -36
- data/lib/pg_ltree/callbacks.rb +1 -18
- data/lib/pg_ltree/configuration.rb +46 -0
- data/lib/pg_ltree/model/cascade.rb +33 -0
- data/lib/pg_ltree/model/path_info.rb +64 -0
- data/lib/pg_ltree/model/predicates.rb +23 -0
- data/lib/pg_ltree/model/querying.rb +58 -0
- data/lib/pg_ltree/model/sql.rb +29 -0
- data/lib/pg_ltree/model/traversal.rb +88 -0
- data/lib/pg_ltree/model.rb +13 -211
- data/lib/pg_ltree/options.rb +66 -0
- data/lib/pg_ltree/version.rb +1 -1
- data/lib/pg_ltree.rb +7 -0
- metadata +38 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: de79a86fd9facd1ee959583bf4108ffa1124624ce1b1bc8cfe377f9bda091c5c
|
|
4
|
+
data.tar.gz: c10962904d55ca0639923c680fe6e81db7c21a233cc1ff98224296532f9e33a2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d85e3e06bec82a0e0d4737a82aba759d3b3d93f252f9a5771a1dd0fb5f461f9f335b6de9eba7d470a5da68c818a37b59a90d3b0dd574fceabe024bc34c99fbbc
|
|
7
|
+
data.tar.gz: 8c6aec44ea07b43e5ec242e64a859a38adbb0e796e23cbbe0a7c002390e4ea6c305f3e1fafa338c90b30d5dfc4e74f8b5bb66142bf59c4485dfca17a6bd8aa36
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
## 2.0.0
|
|
2
|
+
|
|
3
|
+
* [IMPROVE] Refactor `PgLtree::Model` and configuration into modular concerns (`PgLtree::Options`, `PgLtree::Configuration`, `PgLtree::Model::Sql`, `Querying`, `Traversal`, `Predicates`, `PathInfo`, `Cascade`)
|
|
4
|
+
* [IMPROVE] Add full Rails 8.1 (`activerecord ~> 8.1`), `pg ~> 1.7`, and PostgreSQL 18 support
|
|
5
|
+
* [IMPROVE] Replace deprecated `ActiveSupport::Deprecation.warn` with `PgLtree.deprecator`
|
|
6
|
+
* [IMPROVE] Replace `ActiveRecord::Base.connection` with `connection_pool.with_connection`, wrap `NLEVEL` in `Arel.sql`, and quote table/column names consistently
|
|
7
|
+
* [ADDED] Add declarative `scope:` option (`Symbol`, `Array<Symbol>`, `Proc`) to `ltree`
|
|
8
|
+
* [OPTIMIZE] Compute `#depth` in memory without DB round-trips, optimize `#leaf?` with `EXISTS`, `#leaves` with `NOT EXISTS`, `#parent` short-circuit for root nodes, and skip `cascade_update` when path did not change
|
|
9
|
+
* [FIXED] Fix `spec/pg_ltree/model_spec.rb`, `spec/pg_ltree/callbacks_spec.rb`, and `spec/pg_ltree/scope_spec.rb`
|
|
10
|
+
|
|
11
|
+
## 1.2.2
|
|
12
|
+
|
|
13
|
+
* [IMPROVE] Add Rails 7.1, 7.2, 8 support ([#50](https://github.com/sjke/pg_ltree/pull/50) by [@olivier-thatch](https://github.com/olivier-thatch))
|
|
14
|
+
* [IMPROVE] Improve Readme ([#45](https://github.com/sjke/pg_ltree/pull/45) by [@mweitzel](https://github.com/mweitzel))
|
|
15
|
+
|
|
16
|
+
## 1.2.1
|
|
17
|
+
|
|
18
|
+
* [FIXED] Fix `undefined method` issue with STI models ([#42](https://github.com/sjke/pg_ltree/pull/42) by [@mweitzel](https://github.com/mweitzel))
|
|
19
|
+
|
|
20
|
+
## 1.2.0
|
|
21
|
+
|
|
22
|
+
* [IMPROVE] Fully rewrote gem using rails autoload, modules and Rspec as main test framework ([#38](https://github.com/sjke/pg_ltree/pull/38))
|
|
23
|
+
|
|
24
|
+
## 1.1.9
|
|
25
|
+
|
|
26
|
+
* [IMPROVE] Add Rails 7 version support ([#29](https://github.com/sjke/pg_ltree/pull/33) by [@chrisortman](https://github.com/chrisortman))
|
|
27
|
+
* [IMPROVE] Add Ruby 3 version support ([#29](https://github.com/sjke/pg_ltree/pull/33) by [@chrisortman](https://github.com/chrisortman))
|
|
28
|
+
|
|
29
|
+
## 1.1.8
|
|
30
|
+
|
|
31
|
+
* [IMPROVE] Update rails/pg version support (rails 6, pg 1.0)
|
|
32
|
+
* [FIXED] Fix #leaves used with a custom relation ([#29](https://github.com/sjke/pg_ltree/pull/29) by [@attilahorvath](https://github.com/attilahorvath))
|
|
33
|
+
|
|
34
|
+
## 1.1.7
|
|
35
|
+
|
|
36
|
+
* [IMPROVE] Update rails/pg version support (rails 5.2, pg 1.0)
|
|
37
|
+
|
|
38
|
+
## 1.1.6
|
|
39
|
+
|
|
40
|
+
* [IMPROVE] Adds `table_name` prefix for a generating SQL queries ([#21](https://github.com/sjke/pg_ltree/pull/21) by [@arjan0307](https://github.com/arjan0307))
|
|
41
|
+
|
|
42
|
+
## 1.1.5
|
|
43
|
+
|
|
44
|
+
* [IMPROVE] Update pg version support ([#19](https://github.com/sjke/pg_ltree/pull/19) by [@askamist](https://github.com/askamist))
|
|
45
|
+
* [IMPROVE] Handler Rails 5.1 deprecation warnings ([#18](https://github.com/sjke/pg_ltree/pull/18) by [@HoyaBoya](https://github.com/HoyaBoya) and [#20](https://github.com/sjke/pg_ltree/pull/20) by [@sjke](https://github.com/sjke))
|
|
46
|
+
|
|
47
|
+
## 1.1.4
|
|
48
|
+
|
|
49
|
+
* [ADDED] Adds method to support ltxtquery value for searching ([#17](https://github.com/sjke/pg_ltree/pull/17) by [@sb1752](https://github.com/sb1752))
|
|
50
|
+
* [IMPROVE] Updated dependency version (Rails 5.1 and Pg adapter 0.21) ([#16](https://github.com/sjke/pg_ltree/pull/16) by [@sb1752](https://github.com/sb1752))
|
|
51
|
+
|
|
52
|
+
## 1.1.3
|
|
53
|
+
|
|
54
|
+
* [ADDED] node instance `height` method ([#6](https://github.com/sjke/pg_ltree/pull/6) by [@caulfield](https://github.com/caulfield))
|
|
55
|
+
* [FIXED] Fix #depth for a new record ([#9](https://github.com/sjke/pg_ltree/pull/9) by [@arjan0307](https://github.com/arjan0307))
|
|
56
|
+
* [IMPROVE] Use 'descendants' instead of 'descendents' ([#8](https://github.com/sjke/pg_ltree/pull/8) by [@CUnknown](https://github.com/CUnknown))
|
|
57
|
+
|
|
58
|
+
## 1.1.2
|
|
59
|
+
|
|
60
|
+
* [FIXED] Fix original #leaves method when merging with previous conditions was invalid. Overrided method in ScopedFor is useless now and it was deleted.
|
|
61
|
+
|
|
62
|
+
## 1.1.1
|
|
63
|
+
|
|
64
|
+
* [FIXED] `#cascade_update` method
|
|
65
|
+
|
|
66
|
+
## 1.1.0
|
|
67
|
+
|
|
68
|
+
* [ADDED] cascade update/destroy (Enabled by default)
|
|
69
|
+
* [FIXED] destroy object
|
data/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# PgLtree
|
|
2
|
+
|
|
3
|
+
[](http://badge.fury.io/rb/pg_ltree)
|
|
4
|
+
[](https://github.com/rubocop/rubocop)
|
|
5
|
+
[](https://github.com/sjke/pg_ltree/actions/workflows/tests.yml?query=branch%3Amaster)
|
|
6
|
+
[](http://www.rubydoc.info/github/sjke/pg_ltree/)
|
|
7
|
+
|
|
8
|
+
Adds PostgreSQL's [ltree](https://www.postgresql.org/docs/current/ltree.html) support for `ActiveRecord` models.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
Add this line to your application's Gemfile:
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
gem "pg_ltree", "~> 2.0"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
And then execute:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bundle install
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Or install it yourself as:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
gem install pg_ltree
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Requirements
|
|
31
|
+
|
|
32
|
+
* **Ruby** >= 2.7 (tested up to 4.0)
|
|
33
|
+
* **Rails (ActiveRecord)** >= 5.2, < 9.0 (including 8.1)
|
|
34
|
+
* **pg** >= 0.19, < 2.0 (including 1.7+)
|
|
35
|
+
* **PostgreSQL** with the `ltree` extension enabled
|
|
36
|
+
|
|
37
|
+
## Setup
|
|
38
|
+
|
|
39
|
+
Enable the `ltree` extension and add an `ltree` column with a GiST index in your migrations:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
class AddLtreeExtension < ActiveRecord::Migration[8.1]
|
|
43
|
+
def change
|
|
44
|
+
enable_extension "ltree" unless extension_enabled?("ltree")
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class CreateNodes < ActiveRecord::Migration[8.1]
|
|
49
|
+
def change
|
|
50
|
+
create_table :nodes do |t|
|
|
51
|
+
t.ltree :path, null: false
|
|
52
|
+
t.timestamps
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
add_index :nodes, :path, using: :gist
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Initialize `ltree` in your model:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
class Node < ActiveRecord::Base
|
|
64
|
+
ltree :path
|
|
65
|
+
# Options:
|
|
66
|
+
# ltree :custom_path, cascade_update: false, cascade_destroy: false
|
|
67
|
+
# ltree :path, scope: :account_id
|
|
68
|
+
# ltree :path, scope: [:account_id, :kind]
|
|
69
|
+
# ltree :path, scope: ->(record) { where(account_id: record.account_id) }
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Configuration options
|
|
74
|
+
|
|
75
|
+
| Option | Default | Description |
|
|
76
|
+
| --- | --- | --- |
|
|
77
|
+
| `column` (1st arg) | `:path` | Name of the PostgreSQL `ltree` column. |
|
|
78
|
+
| `cascade_update:` | `true` | Automatically update descendant paths via `after_commit` when the node's path changes. |
|
|
79
|
+
| `cascade_destroy:` | `true` | Automatically destroy descendant records via `after_commit` when the node is destroyed. |
|
|
80
|
+
| `scope:` | `nil` | Cluster trees by attribute (`Symbol`), attributes (`Array<Symbol>`), or `Proc` (`->(record) { ... }` or `-> { ... }`). |
|
|
81
|
+
|
|
82
|
+
### Scoping trees (`scope:` / `ltree_scope`)
|
|
83
|
+
|
|
84
|
+
You can cluster independent trees in the same table either declaratively with `scope:` or by overriding `ltree_scope`:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
class LessonPlan < ActiveRecord::Base
|
|
88
|
+
has_many :tutorials
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
class Tutorial < ActiveRecord::Base
|
|
92
|
+
belongs_to :lesson_plan
|
|
93
|
+
|
|
94
|
+
ltree :path, scope: :lesson_plan_id
|
|
95
|
+
end
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Or with a custom `ltree_scope` method (useful for STI or complex scopes):
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
class Tutorial < ActiveRecord::Base
|
|
102
|
+
belongs_to :lesson_plan
|
|
103
|
+
|
|
104
|
+
ltree :path
|
|
105
|
+
|
|
106
|
+
def ltree_scope
|
|
107
|
+
self.class.base_class.where(lesson_plan_id: lesson_plan_id)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Using this pattern, traversal methods (`siblings`, `parent`, `descendants`, etc.) as well as cascading updates and destroys are scoped to records matching that `lesson_plan_id`.
|
|
113
|
+
|
|
114
|
+
## Usage
|
|
115
|
+
|
|
116
|
+
### Class / Relation querying
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
Node.roots # All root nodes (depth == 1)
|
|
120
|
+
Node.at_depth(2) # All nodes at depth 2
|
|
121
|
+
Node.leaves # All leaf nodes (no descendants)
|
|
122
|
+
Node.where_path_liked("*{2}.Astronomy|Pictures") # Match using lquery (~)
|
|
123
|
+
Node.where_path_matches_ltxtquery("Astro*% & !pictures@") # Match using ltxtquery (@)
|
|
124
|
+
Node.ltree_path_column # Configured ltree column name (:path)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Instance traversal & info
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
node.root # Root node of this tree (or nil)
|
|
131
|
+
node.parent # Direct parent node (or nil for root)
|
|
132
|
+
node.children # ActiveRecord::Relation of direct children
|
|
133
|
+
node.leaves # ActiveRecord::Relation of descendant leaf nodes
|
|
134
|
+
node.ancestors # ActiveRecord::Relation of ancestors (excluding self)
|
|
135
|
+
node.self_and_ancestors # ActiveRecord::Relation of self and ancestors
|
|
136
|
+
node.descendants # ActiveRecord::Relation of descendants (excluding self)
|
|
137
|
+
node.self_and_descendants # ActiveRecord::Relation of self and descendants
|
|
138
|
+
node.siblings # ActiveRecord::Relation of siblings (excluding self)
|
|
139
|
+
node.self_and_siblings # ActiveRecord::Relation of self and siblings
|
|
140
|
+
|
|
141
|
+
node.depth # Integer depth (1 for root, 0 if path is nil/empty)
|
|
142
|
+
node.height # Integer height (0 for leaf)
|
|
143
|
+
node.root? # true if depth == 1
|
|
144
|
+
node.leaf? # true if node has no descendants
|
|
145
|
+
node.ltree_path # Current path value
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Contributing
|
|
149
|
+
|
|
150
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sjke/pg_ltree.
|
|
151
|
+
|
|
152
|
+
Run linter and tests locally:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
bundle exec rake lint
|
|
156
|
+
bundle exec rspec
|
|
157
|
+
bundle exec appraisal rake spec
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Changelog
|
|
161
|
+
|
|
162
|
+
See [CHANGELOG](CHANGELOG.md) for details.
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
|
|
166
|
+
The gem is available as open source under the terms of the [MIT License](MIT-LICENSE).
|
data/Rakefile
CHANGED
|
@@ -3,6 +3,15 @@ require "standard/rake"
|
|
|
3
3
|
require "rspec/core/rake_task"
|
|
4
4
|
require "appraisal"
|
|
5
5
|
|
|
6
|
+
desc "Run all tests"
|
|
6
7
|
RSpec::Core::RakeTask.new(:spec)
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
desc "Run linting"
|
|
10
|
+
task lint: :standard
|
|
11
|
+
|
|
12
|
+
desc "Run all tests across all appraisals"
|
|
13
|
+
task :test_all do
|
|
14
|
+
sh "bundle exec appraisal rake spec"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
task default: [:lint, :spec]
|
data/lib/pg_ltree/base.rb
CHANGED
|
@@ -1,44 +1,10 @@
|
|
|
1
1
|
require_relative "model"
|
|
2
2
|
require_relative "callbacks"
|
|
3
|
+
require_relative "configuration"
|
|
3
4
|
|
|
4
5
|
module PgLtree
|
|
5
6
|
module Base
|
|
6
7
|
extend ActiveSupport::Concern
|
|
7
|
-
|
|
8
|
-
class_methods do
|
|
9
|
-
attr_reader :ltree_options
|
|
10
|
-
|
|
11
|
-
# Initialize ltree module for the model
|
|
12
|
-
#
|
|
13
|
-
# @param column [String] lTree column name
|
|
14
|
-
# @param cascade_update [Boolean] Update all child nodes when the self path is changed
|
|
15
|
-
# @param cascade_destroy [Boolean] Destroy all child nodes on self-destroying
|
|
16
|
-
def ltree(column = :path, cascade_update: true, cascade_destroy: true, cascade: nil)
|
|
17
|
-
if cascade
|
|
18
|
-
ActiveSupport::Deprecation.warn("'cascade' param is deprecated. Use 'cascade_update' and 'cascade_destroy' instead.")
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
@ltree_options = {
|
|
22
|
-
column: column,
|
|
23
|
-
cascade_update: cascade.nil? ? cascade_update : cascade,
|
|
24
|
-
cascade_destroy: cascade.nil? ? cascade_destroy : cascade
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
send(:include, PgLtree::Model)
|
|
28
|
-
send(:include, PgLtree::Callbacks)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def ltree_options
|
|
32
|
-
@ltree_options || superclass.ltree_options
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def ltree_option_for(key)
|
|
36
|
-
ltree_options[key]
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
included do
|
|
41
|
-
delegate :ltree_option_for, to: :class
|
|
42
|
-
end
|
|
8
|
+
include Configuration
|
|
43
9
|
end
|
|
44
10
|
end
|
data/lib/pg_ltree/callbacks.rb
CHANGED
|
@@ -3,25 +3,8 @@ module PgLtree
|
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
4
|
|
|
5
5
|
included do
|
|
6
|
-
after_commit :cascade_update, on: :update, if: -> { ltree_option_for
|
|
6
|
+
after_commit :cascade_update, on: :update, if: -> { ltree_option_for(:cascade_update) && saved_change_to_attribute?(ltree_path_column) }
|
|
7
7
|
after_commit :cascade_destroy, on: :destroy, if: -> { ltree_option_for :cascade_destroy }
|
|
8
|
-
|
|
9
|
-
# Update child nodes path
|
|
10
|
-
#
|
|
11
|
-
# @return [ActiveRecord::Relation]
|
|
12
|
-
def cascade_update
|
|
13
|
-
ltree_scope
|
|
14
|
-
.where(["#{self.class.table_name}.#{ltree_path_column} <@ ?", ltree_path_before_last_save])
|
|
15
|
-
.where(["#{self.class.table_name}.#{ltree_path_column} != ?", ltree_path])
|
|
16
|
-
.update_all ["#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path, ltree_path_before_last_save]
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# Destroy child nodes
|
|
20
|
-
#
|
|
21
|
-
# @return [ActiveRecord::Relation]
|
|
22
|
-
def cascade_destroy
|
|
23
|
-
ltree_scope.where("#{self.class.table_name}.#{ltree_path_column} <@ ?", ltree_path_in_database).destroy_all
|
|
24
|
-
end
|
|
25
8
|
end
|
|
26
9
|
end
|
|
27
10
|
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
module Configuration
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
class_methods do
|
|
6
|
+
# Initialize ltree module for the model
|
|
7
|
+
#
|
|
8
|
+
# @param column [Symbol, String] lTree column name
|
|
9
|
+
# @param cascade_update [Boolean] Update all child nodes when the self path is changed
|
|
10
|
+
# @param cascade_destroy [Boolean] Destroy all child nodes on self-destroying
|
|
11
|
+
# @param scope [Symbol, Array<Symbol>, Proc, nil] Optional scope attribute(s) or lambda
|
|
12
|
+
def ltree(column = :path, cascade_update: true, cascade_destroy: true, cascade: nil, scope: nil)
|
|
13
|
+
unless cascade.nil?
|
|
14
|
+
PgLtree.deprecator.warn("'cascade' param is deprecated. Use 'cascade_update' and 'cascade_destroy' instead.")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
@ltree_config = PgLtree::Options.new(
|
|
18
|
+
column: column,
|
|
19
|
+
cascade_update: cascade_update,
|
|
20
|
+
cascade_destroy: cascade_destroy,
|
|
21
|
+
cascade: cascade,
|
|
22
|
+
scope: scope
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
include PgLtree::Model unless include?(PgLtree::Model)
|
|
26
|
+
include PgLtree::Callbacks unless include?(PgLtree::Callbacks)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def ltree_config
|
|
30
|
+
@ltree_config || (superclass.respond_to?(:ltree_config) ? superclass.ltree_config : nil)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def ltree_options
|
|
34
|
+
ltree_config&.to_h
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def ltree_option_for(key)
|
|
38
|
+
ltree_config&.[](key)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
included do
|
|
43
|
+
delegate :ltree_config, :ltree_option_for, to: :class
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
module Model
|
|
3
|
+
module Cascade
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
# Update all children for current path
|
|
8
|
+
#
|
|
9
|
+
# @return [Integer, nil]
|
|
10
|
+
def cascade_update
|
|
11
|
+
old_path = ltree_path_before_last_save
|
|
12
|
+
new_path = ltree_path
|
|
13
|
+
return if old_path.nil? || new_path.nil? || old_path == new_path
|
|
14
|
+
|
|
15
|
+
ltree_scope
|
|
16
|
+
.where(["#{ltree_qualified_column} <@ ?", old_path])
|
|
17
|
+
.where(["#{ltree_qualified_column} != ?", new_path])
|
|
18
|
+
.update_all(["#{ltree_quoted_column} = ? || subpath(#{ltree_quoted_column}, nlevel(?))", new_path, old_path])
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Delete all children for current path
|
|
22
|
+
#
|
|
23
|
+
# @return [Array, nil]
|
|
24
|
+
def cascade_destroy
|
|
25
|
+
db_path = ltree_path_in_database
|
|
26
|
+
return if db_path.nil?
|
|
27
|
+
|
|
28
|
+
ltree_scope.where(["#{ltree_qualified_column} <@ ?", db_path]).destroy_all
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
module Model
|
|
3
|
+
module PathInfo
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
# Get current scope for ltree queries
|
|
8
|
+
#
|
|
9
|
+
# @return [Class, ActiveRecord::Relation]
|
|
10
|
+
def ltree_scope
|
|
11
|
+
ltree_config ? ltree_config.resolve_scope(self) : self.class
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Get lTree column
|
|
15
|
+
#
|
|
16
|
+
# @return [Symbol, String] ltree column name
|
|
17
|
+
delegate :ltree_path_column, to: :ltree_scope
|
|
18
|
+
|
|
19
|
+
# Get lTree value
|
|
20
|
+
#
|
|
21
|
+
# @return [String, nil] ltree current value
|
|
22
|
+
def ltree_path
|
|
23
|
+
public_send ltree_path_column
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Get ltree original value before the save just occurred
|
|
27
|
+
#
|
|
28
|
+
# @return [String, nil] ltree previous value
|
|
29
|
+
def ltree_path_before_last_save
|
|
30
|
+
public_send :attribute_before_last_save, ltree_path_column
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Get lTree value in database
|
|
34
|
+
#
|
|
35
|
+
# @return [String, nil] ltree value in database
|
|
36
|
+
def ltree_path_in_database
|
|
37
|
+
public_send :attribute_in_database, ltree_path_column
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Get node height
|
|
41
|
+
#
|
|
42
|
+
# @return [Integer] height of the given node.
|
|
43
|
+
def height
|
|
44
|
+
return 0 if ltree_path.nil?
|
|
45
|
+
|
|
46
|
+
max_level = self_and_descendants.maximum(Arel.sql("NLEVEL(#{ltree_qualified_column})"))
|
|
47
|
+
return 0 if max_level.nil?
|
|
48
|
+
|
|
49
|
+
max_level.to_i - depth
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Get node depth
|
|
53
|
+
#
|
|
54
|
+
# @return [Integer] node depth
|
|
55
|
+
def depth
|
|
56
|
+
path = ltree_path.to_s
|
|
57
|
+
return 0 if path.empty?
|
|
58
|
+
|
|
59
|
+
path.count(".") + 1
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
module Model
|
|
3
|
+
module Predicates
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
# Check what current node is root
|
|
8
|
+
#
|
|
9
|
+
# @return [Boolean] True - for root node, False - for childen node
|
|
10
|
+
def root?
|
|
11
|
+
depth == 1
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Check what current node is a leaf
|
|
15
|
+
#
|
|
16
|
+
# @return [Boolean] True - if node has no descendants, False - if node has descendants
|
|
17
|
+
def leaf?
|
|
18
|
+
!descendants.exists?
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
module Model
|
|
3
|
+
module Querying
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
class_methods do
|
|
7
|
+
def ltree_path_column
|
|
8
|
+
ltree_option_for :column
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Get roots
|
|
12
|
+
#
|
|
13
|
+
# @return [ActiveRecord::Relation] relations of node's roots
|
|
14
|
+
def roots
|
|
15
|
+
at_depth 1
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Get nodes on the level
|
|
19
|
+
#
|
|
20
|
+
# @param depth [Integer] Depth of the nodes
|
|
21
|
+
# @return [ActiveRecord::Relation] relations of nodes for the depth
|
|
22
|
+
def at_depth(depth)
|
|
23
|
+
where(["NLEVEL(#{ltree_qualified_column}) = ?", depth])
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Get all leaves
|
|
27
|
+
#
|
|
28
|
+
# @return [ActiveRecord::Relation] relations of node's leaves
|
|
29
|
+
def leaves
|
|
30
|
+
alias_name = connection_pool.with_connection { |connection| connection.quote_table_name("subquery") }
|
|
31
|
+
subquery_column = "#{alias_name}.#{ltree_quoted_column}"
|
|
32
|
+
|
|
33
|
+
where(
|
|
34
|
+
"NOT EXISTS (SELECT 1 FROM #{quoted_table_name} AS #{alias_name} " \
|
|
35
|
+
"WHERE #{ltree_qualified_column} <> #{subquery_column} " \
|
|
36
|
+
"AND #{ltree_qualified_column} @> #{subquery_column})"
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Get all with nodes when path liked the lquery
|
|
41
|
+
#
|
|
42
|
+
# @param lquery [String] ltree query
|
|
43
|
+
# @return [ActiveRecord::Relation] relations of node'
|
|
44
|
+
def where_path_liked(lquery)
|
|
45
|
+
where(["#{ltree_qualified_column} ~ ?", lquery])
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Get all nodes with path matching full-text-search-like pattern
|
|
49
|
+
#
|
|
50
|
+
# @param ltxtquery [String] ltree search query
|
|
51
|
+
# @return [ActiveRecord::Relation] of matching nodes
|
|
52
|
+
def where_path_matches_ltxtquery(ltxtquery)
|
|
53
|
+
where(["#{ltree_qualified_column} @ ?", ltxtquery])
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
module Model
|
|
3
|
+
module Sql
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
class_methods do
|
|
7
|
+
# Quoted ltree column name for the current model's connection adapter
|
|
8
|
+
#
|
|
9
|
+
# @return [String]
|
|
10
|
+
def ltree_quoted_column
|
|
11
|
+
connection_pool.with_connection do |connection|
|
|
12
|
+
connection.quote_column_name(ltree_path_column)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Table-qualified, quoted ltree column expression
|
|
17
|
+
#
|
|
18
|
+
# @return [String]
|
|
19
|
+
def ltree_qualified_column
|
|
20
|
+
"#{quoted_table_name}.#{ltree_quoted_column}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
included do
|
|
25
|
+
delegate :ltree_quoted_column, :ltree_qualified_column, to: :class
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
module Model
|
|
3
|
+
module Traversal
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
# Get root of the node
|
|
8
|
+
#
|
|
9
|
+
# @return [Object, nil] root node
|
|
10
|
+
def root
|
|
11
|
+
return nil if ltree_path.nil?
|
|
12
|
+
|
|
13
|
+
ltree_scope.where(["#{ltree_qualified_column} = SUBPATH(?, 0, 1)", ltree_path]).first
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Get parent of the node
|
|
17
|
+
#
|
|
18
|
+
# @return [Object, nil] parent node
|
|
19
|
+
def parent
|
|
20
|
+
return nil if ltree_path.nil? || root?
|
|
21
|
+
|
|
22
|
+
ltree_scope.find_by(["#{ltree_qualified_column} = SUBPATH(?, 0, NLEVEL(?) - 1)", ltree_path, ltree_path])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Get leaves of the node
|
|
26
|
+
#
|
|
27
|
+
# @return [ActiveRecord::Relation]
|
|
28
|
+
def leaves
|
|
29
|
+
ltree_scope.leaves.where(["#{ltree_qualified_column} <@ ?", ltree_path]).where.not(ltree_path_column => ltree_path)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Get self and ancestors
|
|
33
|
+
#
|
|
34
|
+
# @return [ActiveRecord::Relation]
|
|
35
|
+
def self_and_ancestors
|
|
36
|
+
ltree_scope.where(["#{ltree_qualified_column} @> ?", ltree_path])
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Get ancestors
|
|
40
|
+
#
|
|
41
|
+
# @return [ActiveRecord::Relation]
|
|
42
|
+
def ancestors
|
|
43
|
+
self_and_ancestors.where.not ltree_path_column => ltree_path
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Get self and descendants
|
|
47
|
+
#
|
|
48
|
+
# @return [ActiveRecord::Relation]
|
|
49
|
+
def self_and_descendants
|
|
50
|
+
ltree_scope.where(["#{ltree_qualified_column} <@ ?", ltree_path])
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Get descendants
|
|
54
|
+
#
|
|
55
|
+
# @return [ActiveRecord::Relation]
|
|
56
|
+
def descendants
|
|
57
|
+
self_and_descendants.where.not ltree_path_column => ltree_path
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Get self and siblings
|
|
61
|
+
#
|
|
62
|
+
# @return [ActiveRecord::Relation]
|
|
63
|
+
def self_and_siblings
|
|
64
|
+
ltree_scope.where(
|
|
65
|
+
[
|
|
66
|
+
"SUBPATH(?, 0, NLEVEL(?) - 1) @> #{ltree_qualified_column} AND nlevel(#{ltree_qualified_column}) = NLEVEL(?)",
|
|
67
|
+
ltree_path, ltree_path, ltree_path
|
|
68
|
+
]
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Get siblings
|
|
73
|
+
#
|
|
74
|
+
# @return [ActiveRecord::Relation]
|
|
75
|
+
def siblings
|
|
76
|
+
self_and_siblings.where.not ltree_path_column => ltree_path
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Get children
|
|
80
|
+
#
|
|
81
|
+
# @return [ActiveRecord::Relation]
|
|
82
|
+
def children
|
|
83
|
+
ltree_scope.where(["? @> #{ltree_qualified_column} AND nlevel(#{ltree_qualified_column}) = NLEVEL(?) + 1", ltree_path, ltree_path])
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
data/lib/pg_ltree/model.rb
CHANGED
|
@@ -1,219 +1,21 @@
|
|
|
1
|
+
require_relative "model/sql"
|
|
2
|
+
require_relative "model/querying"
|
|
3
|
+
require_relative "model/traversal"
|
|
4
|
+
require_relative "model/predicates"
|
|
5
|
+
require_relative "model/path_info"
|
|
6
|
+
require_relative "model/cascade"
|
|
7
|
+
|
|
1
8
|
module PgLtree
|
|
2
9
|
module Model
|
|
3
10
|
extend ActiveSupport::Concern
|
|
4
11
|
|
|
5
|
-
class_methods do
|
|
6
|
-
def ltree_path_column
|
|
7
|
-
ltree_option_for :column
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
# Get roots
|
|
11
|
-
#
|
|
12
|
-
# @return [ActiveRecord::Relation] relations of node's roots
|
|
13
|
-
def roots
|
|
14
|
-
at_depth 1
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
# Get nodes on the level
|
|
18
|
-
#
|
|
19
|
-
# @param depth [Integer] Depth of the nodes
|
|
20
|
-
# @return [ActiveRecord::Relation] relations of nodes for the depth
|
|
21
|
-
def at_depth(depth)
|
|
22
|
-
where "NLEVEL(#{table_name}.#{ltree_path_column}) = ?", depth
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# Get all leaves
|
|
26
|
-
#
|
|
27
|
-
# @return [ActiveRecord::Relation] relations of node's leaves
|
|
28
|
-
def leaves
|
|
29
|
-
subquery = unscoped.select("#{table_name}.#{ltree_path_column}")
|
|
30
|
-
.from("#{table_name} AS subquery")
|
|
31
|
-
.where("#{table_name}.#{ltree_path_column} <> subquery.#{ltree_path_column}")
|
|
32
|
-
.where("#{table_name}.#{ltree_path_column} @> subquery.#{ltree_path_column}")
|
|
33
|
-
|
|
34
|
-
where.not ltree_path_column => subquery
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# Get all with nodes when path liked the lquery
|
|
38
|
-
#
|
|
39
|
-
# @param lquery [String] ltree query
|
|
40
|
-
# @return [ActiveRecord::Relation] relations of node'
|
|
41
|
-
def where_path_liked(lquery)
|
|
42
|
-
where "#{table_name}.#{ltree_path_column} ~ ?", lquery
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
# Get all nodes with path matching full-text-search-like pattern
|
|
46
|
-
#
|
|
47
|
-
# @param ltxtquery [String] ltree search query
|
|
48
|
-
# @return [ActiveRecord::Relation] of matching nodes
|
|
49
|
-
def where_path_matches_ltxtquery(ltxtquery)
|
|
50
|
-
where "#{table_name}.#{ltree_path_column} @ ?", ltxtquery
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
12
|
included do
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
# Get lTree column
|
|
63
|
-
#
|
|
64
|
-
# @return [String] ltree column name
|
|
65
|
-
delegate :ltree_path_column, to: :ltree_scope
|
|
66
|
-
|
|
67
|
-
# Get lTree value
|
|
68
|
-
#
|
|
69
|
-
# @return [String] ltree current value
|
|
70
|
-
def ltree_path
|
|
71
|
-
public_send ltree_path_column
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# Get ltree original value before the save just occurred
|
|
75
|
-
# https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html#method-i-attribute_before_last_save
|
|
76
|
-
#
|
|
77
|
-
# @return [String] ltree previous value
|
|
78
|
-
def ltree_path_before_last_save
|
|
79
|
-
public_send :attribute_before_last_save, ltree_path_column
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
# Get lTree previous value
|
|
83
|
-
# originally +attribute_was+ used in before create/update, destroy won't call +save+ so this work
|
|
84
|
-
# https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html#method-i-attribute_in_database
|
|
85
|
-
#
|
|
86
|
-
# @return [String] ltree value in database
|
|
87
|
-
|
|
88
|
-
def ltree_path_in_database
|
|
89
|
-
public_send :attribute_in_database, ltree_path_column
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
# Check what current node is root
|
|
93
|
-
#
|
|
94
|
-
# @return [Boolean] True - for root node, False - for childen node
|
|
95
|
-
def root?
|
|
96
|
-
depth == 1
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
# Get node height
|
|
100
|
-
#
|
|
101
|
-
# The height of a node is the number of edges
|
|
102
|
-
# on the longest downward path between that node and a leaf. The leaf nodes have height zero,
|
|
103
|
-
# and a tree with only a single node (hence both a root and leaf) has height zero.
|
|
104
|
-
# Conventionally, an empty tree (tree with no nodes, if such are allowed) has depth and height −1
|
|
105
|
-
#
|
|
106
|
-
# @return [Number] height of the given node. Height of the tree for root node.
|
|
107
|
-
def height
|
|
108
|
-
self_and_descendants.maximum("NLEVEL(#{ltree_path_column})") - depth.to_i
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
# Get node depth
|
|
112
|
-
#
|
|
113
|
-
# @return [Integer] node depth
|
|
114
|
-
def depth
|
|
115
|
-
ActiveRecord::Base.connection.select_all("SELECT NLEVEL('#{ltree_path}')").rows.flatten.first.to_i
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
# Get root of the node
|
|
119
|
-
#
|
|
120
|
-
# return [Object] root node
|
|
121
|
-
def root
|
|
122
|
-
ltree_scope.where("#{self.class.table_name}.#{ltree_path_column} = SUBPATH(?, 0, 1)", ltree_path).first
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
# Get parent of the node
|
|
126
|
-
#
|
|
127
|
-
# return [Object] root node
|
|
128
|
-
def parent
|
|
129
|
-
ltree_scope.find_by "#{self.class.table_name}.#{ltree_path_column} = SUBPATH(?, 0, NLEVEL(?) - 1)", ltree_path,
|
|
130
|
-
ltree_path
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
# Get leaves of the node
|
|
134
|
-
#
|
|
135
|
-
# @return [ActiveRecord::Relation]
|
|
136
|
-
def leaves
|
|
137
|
-
ltree_scope.leaves.where("#{self.class.table_name}.#{ltree_path_column} <@ ?",
|
|
138
|
-
ltree_path).where.not ltree_path_column => ltree_path
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
# Check what current node have leaves
|
|
142
|
-
#
|
|
143
|
-
# @return [Boolean] True - if node have leaves, False - if node doesn't have leaves
|
|
144
|
-
def leaf?
|
|
145
|
-
leaves.count == 0
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
# Get self and ancestors
|
|
149
|
-
#
|
|
150
|
-
# @return [ActiveRecord::Relation]
|
|
151
|
-
def self_and_ancestors
|
|
152
|
-
ltree_scope.where("#{self.class.table_name}.#{ltree_path_column} @> ?", ltree_path)
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
# Get ancestors
|
|
156
|
-
#
|
|
157
|
-
# @return [ActiveRecord::Relation]
|
|
158
|
-
def ancestors
|
|
159
|
-
self_and_ancestors.where.not ltree_path_column => ltree_path
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
# Get self and descendants
|
|
163
|
-
#
|
|
164
|
-
# @return [ActiveRecord::Relation]
|
|
165
|
-
def self_and_descendants
|
|
166
|
-
ltree_scope.where("#{self.class.table_name}.#{ltree_path_column} <@ ?", ltree_path)
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
# Get descendants
|
|
170
|
-
#
|
|
171
|
-
# @return [ActiveRecord::Relation]
|
|
172
|
-
def descendants
|
|
173
|
-
self_and_descendants.where.not ltree_path_column => ltree_path
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
# Get self and siblings
|
|
177
|
-
#
|
|
178
|
-
# @return [ActiveRecord::Relation]
|
|
179
|
-
def self_and_siblings
|
|
180
|
-
ltree_scope.where(
|
|
181
|
-
"SUBPATH(?, 0, NLEVEL(?) - 1) @> #{self.class.table_name}.#{ltree_path_column} AND nlevel(#{self.class.table_name}.#{ltree_path_column}) = NLEVEL(?)",
|
|
182
|
-
ltree_path, ltree_path, ltree_path
|
|
183
|
-
)
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
# Get siblings
|
|
187
|
-
#
|
|
188
|
-
# @return [ActiveRecord::Relation]
|
|
189
|
-
def siblings
|
|
190
|
-
self_and_siblings.where.not ltree_path_column => ltree_path
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
# Get children
|
|
194
|
-
#
|
|
195
|
-
# @return [ActiveRecord::Relation]
|
|
196
|
-
def children
|
|
197
|
-
ltree_scope.where "? @> #{self.class.table_name}.#{ltree_path_column} AND nlevel(#{self.class.table_name}.#{ltree_path_column}) = NLEVEL(?) + 1",
|
|
198
|
-
ltree_path, ltree_path
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
# Update all childen for current path
|
|
202
|
-
#
|
|
203
|
-
# @return [ActiveRecord::Relation]
|
|
204
|
-
def cascade_update
|
|
205
|
-
ltree_scope
|
|
206
|
-
.where("#{self.class.table_name}.#{ltree_path_column} <@ ?", ltree_path_before_last_save)
|
|
207
|
-
.where("#{self.class.table_name}.#{ltree_path_column} != ?", ltree_path)
|
|
208
|
-
.update_all("#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path, ltree_path_before_last_save)
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
# Delete all children for current path
|
|
212
|
-
#
|
|
213
|
-
# @return [ActiveRecord::Relation]
|
|
214
|
-
def cascade_destroy
|
|
215
|
-
ltree_scope.where("#{self.class.table_name}.#{ltree_path_column} <@ ?", ltree_path_in_database).destroy_all
|
|
216
|
-
end
|
|
13
|
+
include Sql
|
|
14
|
+
include PathInfo
|
|
15
|
+
include Querying
|
|
16
|
+
include Traversal
|
|
17
|
+
include Predicates
|
|
18
|
+
include Cascade
|
|
217
19
|
end
|
|
218
20
|
end
|
|
219
21
|
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
module PgLtree
|
|
2
|
+
class Options
|
|
3
|
+
attr_reader :column, :cascade_update, :cascade_destroy, :scope
|
|
4
|
+
|
|
5
|
+
def initialize(column: :path, cascade_update: true, cascade_destroy: true, cascade: nil, scope: nil)
|
|
6
|
+
@column = column
|
|
7
|
+
@cascade_update = cascade.nil? ? cascade_update : cascade
|
|
8
|
+
@cascade_destroy = cascade.nil? ? cascade_destroy : cascade
|
|
9
|
+
@scope = normalize_scope(scope)
|
|
10
|
+
freeze
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def [](key)
|
|
14
|
+
case key
|
|
15
|
+
when :column then @column
|
|
16
|
+
when :cascade_update then @cascade_update
|
|
17
|
+
when :cascade_destroy then @cascade_destroy
|
|
18
|
+
when :scope then @scope
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def resolve_scope(record)
|
|
23
|
+
klass = record.class
|
|
24
|
+
case @scope
|
|
25
|
+
when nil
|
|
26
|
+
klass
|
|
27
|
+
when Proc
|
|
28
|
+
if @scope.arity.zero?
|
|
29
|
+
klass.instance_exec(&@scope)
|
|
30
|
+
else
|
|
31
|
+
klass.instance_exec(record, &@scope)
|
|
32
|
+
end
|
|
33
|
+
when Symbol, Array
|
|
34
|
+
conditions = Array(@scope).each_with_object({}) do |attr, hash|
|
|
35
|
+
hash[attr] = record.public_send(attr)
|
|
36
|
+
end
|
|
37
|
+
klass.where(conditions)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_h
|
|
42
|
+
hash = {
|
|
43
|
+
cascade_destroy: @cascade_destroy,
|
|
44
|
+
cascade_update: @cascade_update,
|
|
45
|
+
column: @column
|
|
46
|
+
}
|
|
47
|
+
hash[:scope] = @scope unless @scope.nil?
|
|
48
|
+
hash
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def normalize_scope(scope)
|
|
54
|
+
case scope
|
|
55
|
+
when nil, Proc
|
|
56
|
+
scope
|
|
57
|
+
when Symbol, String
|
|
58
|
+
scope.to_sym
|
|
59
|
+
when Array
|
|
60
|
+
scope.map(&:to_sym).freeze
|
|
61
|
+
else
|
|
62
|
+
raise ArgumentError, "Invalid ltree :scope option: #{scope.inspect}. Expected Symbol, Array<Symbol>, Proc, or nil."
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
data/lib/pg_ltree/version.rb
CHANGED
data/lib/pg_ltree.rb
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
require "active_support"
|
|
2
|
+
require "active_support/deprecation"
|
|
2
3
|
|
|
3
4
|
module PgLtree
|
|
4
5
|
autoload :Base, "pg_ltree/base"
|
|
6
|
+
autoload :Options, "pg_ltree/options"
|
|
7
|
+
|
|
8
|
+
# @return [ActiveSupport::Deprecation]
|
|
9
|
+
def self.deprecator
|
|
10
|
+
@deprecator ||= ActiveSupport::Deprecation.new("2.0", "PgLtree")
|
|
11
|
+
end
|
|
5
12
|
end
|
|
6
13
|
|
|
7
14
|
ActiveSupport.on_load(:active_record) do
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pg_ltree
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrei Panamarenka
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activerecord
|
|
@@ -19,7 +18,7 @@ dependencies:
|
|
|
19
18
|
version: '5.2'
|
|
20
19
|
- - "<"
|
|
21
20
|
- !ruby/object:Gem::Version
|
|
22
|
-
version: '
|
|
21
|
+
version: '9.0'
|
|
23
22
|
type: :runtime
|
|
24
23
|
prerelease: false
|
|
25
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -29,7 +28,7 @@ dependencies:
|
|
|
29
28
|
version: '5.2'
|
|
30
29
|
- - "<"
|
|
31
30
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '
|
|
31
|
+
version: '9.0'
|
|
33
32
|
- !ruby/object:Gem::Dependency
|
|
34
33
|
name: pg
|
|
35
34
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -112,56 +111,70 @@ dependencies:
|
|
|
112
111
|
requirements:
|
|
113
112
|
- - "~>"
|
|
114
113
|
- !ruby/object:Gem::Version
|
|
115
|
-
version: 0.9
|
|
114
|
+
version: '0.9'
|
|
116
115
|
type: :development
|
|
117
116
|
prerelease: false
|
|
118
117
|
version_requirements: !ruby/object:Gem::Requirement
|
|
119
118
|
requirements:
|
|
120
119
|
- - "~>"
|
|
121
120
|
- !ruby/object:Gem::Version
|
|
122
|
-
version: 0.9
|
|
121
|
+
version: '0.9'
|
|
123
122
|
- !ruby/object:Gem::Dependency
|
|
124
123
|
name: appraisal
|
|
125
124
|
requirement: !ruby/object:Gem::Requirement
|
|
126
125
|
requirements:
|
|
127
126
|
- - "~>"
|
|
128
127
|
- !ruby/object:Gem::Version
|
|
129
|
-
version: '2.
|
|
128
|
+
version: '2.5'
|
|
130
129
|
type: :development
|
|
131
130
|
prerelease: false
|
|
132
131
|
version_requirements: !ruby/object:Gem::Requirement
|
|
133
132
|
requirements:
|
|
134
133
|
- - "~>"
|
|
135
134
|
- !ruby/object:Gem::Version
|
|
136
|
-
version: '2.
|
|
135
|
+
version: '2.5'
|
|
137
136
|
- !ruby/object:Gem::Dependency
|
|
138
137
|
name: rspec
|
|
139
138
|
requirement: !ruby/object:Gem::Requirement
|
|
140
139
|
requirements:
|
|
141
140
|
- - "~>"
|
|
142
141
|
- !ruby/object:Gem::Version
|
|
143
|
-
version: '3.
|
|
142
|
+
version: '3.13'
|
|
144
143
|
type: :development
|
|
145
144
|
prerelease: false
|
|
146
145
|
version_requirements: !ruby/object:Gem::Requirement
|
|
147
146
|
requirements:
|
|
148
147
|
- - "~>"
|
|
149
148
|
- !ruby/object:Gem::Version
|
|
150
|
-
version: '3.
|
|
149
|
+
version: '3.13'
|
|
151
150
|
- !ruby/object:Gem::Dependency
|
|
152
151
|
name: database_cleaner
|
|
153
152
|
requirement: !ruby/object:Gem::Requirement
|
|
154
153
|
requirements:
|
|
155
154
|
- - "~>"
|
|
156
155
|
- !ruby/object:Gem::Version
|
|
157
|
-
version: '2.
|
|
156
|
+
version: '2.1'
|
|
158
157
|
type: :development
|
|
159
158
|
prerelease: false
|
|
160
159
|
version_requirements: !ruby/object:Gem::Requirement
|
|
161
160
|
requirements:
|
|
162
161
|
- - "~>"
|
|
163
162
|
- !ruby/object:Gem::Version
|
|
164
|
-
version: '2.
|
|
163
|
+
version: '2.1'
|
|
164
|
+
- !ruby/object:Gem::Dependency
|
|
165
|
+
name: simplecov
|
|
166
|
+
requirement: !ruby/object:Gem::Requirement
|
|
167
|
+
requirements:
|
|
168
|
+
- - "~>"
|
|
169
|
+
- !ruby/object:Gem::Version
|
|
170
|
+
version: '0.22'
|
|
171
|
+
type: :development
|
|
172
|
+
prerelease: false
|
|
173
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
174
|
+
requirements:
|
|
175
|
+
- - "~>"
|
|
176
|
+
- !ruby/object:Gem::Version
|
|
177
|
+
version: '0.22'
|
|
165
178
|
description: Organize ActiveRecord model into a tree structure using PostgreSQL LTree
|
|
166
179
|
email:
|
|
167
180
|
- andrei.panamarenka@gmail.com
|
|
@@ -169,18 +182,27 @@ executables: []
|
|
|
169
182
|
extensions: []
|
|
170
183
|
extra_rdoc_files: []
|
|
171
184
|
files:
|
|
185
|
+
- CHANGELOG.md
|
|
172
186
|
- MIT-LICENSE
|
|
187
|
+
- README.md
|
|
173
188
|
- Rakefile
|
|
174
189
|
- lib/pg_ltree.rb
|
|
175
190
|
- lib/pg_ltree/base.rb
|
|
176
191
|
- lib/pg_ltree/callbacks.rb
|
|
192
|
+
- lib/pg_ltree/configuration.rb
|
|
177
193
|
- lib/pg_ltree/model.rb
|
|
194
|
+
- lib/pg_ltree/model/cascade.rb
|
|
195
|
+
- lib/pg_ltree/model/path_info.rb
|
|
196
|
+
- lib/pg_ltree/model/predicates.rb
|
|
197
|
+
- lib/pg_ltree/model/querying.rb
|
|
198
|
+
- lib/pg_ltree/model/sql.rb
|
|
199
|
+
- lib/pg_ltree/model/traversal.rb
|
|
200
|
+
- lib/pg_ltree/options.rb
|
|
178
201
|
- lib/pg_ltree/version.rb
|
|
179
202
|
homepage: https://github.com/sjke/pg_ltree
|
|
180
203
|
licenses:
|
|
181
204
|
- MIT
|
|
182
205
|
metadata: {}
|
|
183
|
-
post_install_message:
|
|
184
206
|
rdoc_options: []
|
|
185
207
|
require_paths:
|
|
186
208
|
- lib
|
|
@@ -188,15 +210,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
188
210
|
requirements:
|
|
189
211
|
- - ">="
|
|
190
212
|
- !ruby/object:Gem::Version
|
|
191
|
-
version: 2.
|
|
213
|
+
version: 2.7.0
|
|
192
214
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
215
|
requirements:
|
|
194
216
|
- - ">="
|
|
195
217
|
- !ruby/object:Gem::Version
|
|
196
218
|
version: '0'
|
|
197
219
|
requirements: []
|
|
198
|
-
rubygems_version:
|
|
199
|
-
signing_key:
|
|
220
|
+
rubygems_version: 4.0.2
|
|
200
221
|
specification_version: 4
|
|
201
222
|
summary: Organize ActiveRecord model into a tree structure using PostgreSQL LTree
|
|
202
223
|
test_files: []
|