hierarchable 0.3.1 → 0.3.3
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/Gemfile.lock +23 -23
- data/README.md +10 -11
- data/lib/hierarchable/hierarchable.rb +2 -2
- data/lib/hierarchable/version.rb +1 -1
- 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: 562da829a749b954d19b9e9895b6d5a02a675ac0147c39b2ad644da52c1dfcb0
|
|
4
|
+
data.tar.gz: 539c4342aa90f98cca4464d6cead1bc1955e3efb6db36e0b0607816afb2557b9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 23ae268818d9af68e38575f72107078a1074ea5ed936b0bb40297f69321378fc9c602ff19ffcdcc475580fb8b8a5a84737d692d071660d70bfecc50e44948730
|
|
7
|
+
data.tar.gz: 76cdde5df266bde48687038bac416de3109c34af973e619137f4a840d65f87e6f644cdaf496f3b55111f3cce988714bccba16032cada943982667d2072eb37e8
|
data/Gemfile.lock
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
hierarchable (0.3.
|
|
4
|
+
hierarchable (0.3.3)
|
|
5
5
|
activerecord (> 4.2.0)
|
|
6
6
|
activesupport (> 4.2.0)
|
|
7
7
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
activemodel (7.0.4)
|
|
12
|
-
activesupport (= 7.0.4)
|
|
13
|
-
activerecord (7.0.4)
|
|
14
|
-
activemodel (= 7.0.4)
|
|
15
|
-
activesupport (= 7.0.4)
|
|
16
|
-
activesupport (7.0.4)
|
|
11
|
+
activemodel (7.0.4.1)
|
|
12
|
+
activesupport (= 7.0.4.1)
|
|
13
|
+
activerecord (7.0.4.1)
|
|
14
|
+
activemodel (= 7.0.4.1)
|
|
15
|
+
activesupport (= 7.0.4.1)
|
|
16
|
+
activesupport (7.0.4.1)
|
|
17
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
18
18
|
i18n (>= 1.6, < 2)
|
|
19
19
|
minitest (>= 5.1)
|
|
@@ -26,50 +26,50 @@ GEM
|
|
|
26
26
|
i18n (1.12.0)
|
|
27
27
|
concurrent-ruby (~> 1.0)
|
|
28
28
|
json (2.6.3)
|
|
29
|
-
minitest (5.
|
|
29
|
+
minitest (5.17.0)
|
|
30
30
|
parallel (1.22.1)
|
|
31
|
-
parser (3.
|
|
31
|
+
parser (3.2.0.0)
|
|
32
32
|
ast (~> 2.4.1)
|
|
33
|
-
rack (3.0.
|
|
33
|
+
rack (3.0.4.1)
|
|
34
34
|
rainbow (3.1.1)
|
|
35
35
|
rake (13.0.6)
|
|
36
|
-
regexp_parser (2.6.
|
|
36
|
+
regexp_parser (2.6.2)
|
|
37
37
|
rexml (3.2.5)
|
|
38
|
-
rubocop (1.
|
|
38
|
+
rubocop (1.43.0)
|
|
39
39
|
json (~> 2.3)
|
|
40
40
|
parallel (~> 1.10)
|
|
41
|
-
parser (>= 3.
|
|
41
|
+
parser (>= 3.2.0.0)
|
|
42
42
|
rainbow (>= 2.2.2, < 4.0)
|
|
43
43
|
regexp_parser (>= 1.8, < 3.0)
|
|
44
44
|
rexml (>= 3.2.5, < 4.0)
|
|
45
|
-
rubocop-ast (>= 1.
|
|
45
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
|
46
46
|
ruby-progressbar (~> 1.7)
|
|
47
|
-
unicode-display_width (>=
|
|
48
|
-
rubocop-ast (1.24.
|
|
47
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
|
48
|
+
rubocop-ast (1.24.1)
|
|
49
49
|
parser (>= 3.1.1.0)
|
|
50
|
-
rubocop-minitest (0.
|
|
50
|
+
rubocop-minitest (0.26.1)
|
|
51
51
|
rubocop (>= 0.90, < 2.0)
|
|
52
|
-
rubocop-performance (1.15.
|
|
52
|
+
rubocop-performance (1.15.2)
|
|
53
53
|
rubocop (>= 1.7.0, < 2.0)
|
|
54
54
|
rubocop-ast (>= 0.4.0)
|
|
55
|
-
rubocop-rails (2.17.
|
|
55
|
+
rubocop-rails (2.17.4)
|
|
56
56
|
activesupport (>= 4.2.0)
|
|
57
57
|
rack (>= 1.1)
|
|
58
58
|
rubocop (>= 1.33.0, < 2.0)
|
|
59
59
|
rubocop-rake (0.6.0)
|
|
60
60
|
rubocop (~> 1.0)
|
|
61
61
|
ruby-progressbar (1.11.0)
|
|
62
|
-
ruby_audit (2.
|
|
62
|
+
ruby_audit (2.2.0)
|
|
63
63
|
bundler-audit (~> 0.9.0)
|
|
64
|
-
sqlite3 (1.
|
|
65
|
-
sqlite3 (1.
|
|
64
|
+
sqlite3 (1.6.0-x86_64-darwin)
|
|
65
|
+
sqlite3 (1.6.0-x86_64-linux)
|
|
66
66
|
temping (4.0.0)
|
|
67
67
|
activerecord (>= 5.2, < 7.1)
|
|
68
68
|
activesupport (>= 5.2, < 7.1)
|
|
69
69
|
thor (1.2.1)
|
|
70
70
|
tzinfo (2.0.5)
|
|
71
71
|
concurrent-ruby (~> 1.0)
|
|
72
|
-
unicode-display_width (2.
|
|
72
|
+
unicode-display_width (2.4.2)
|
|
73
73
|
|
|
74
74
|
PLATFORMS
|
|
75
75
|
x86_64-darwin-22
|
data/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# Hierarchable
|
|
2
2
|
|
|
3
3
|
[](https://dl.circleci.com/status-badge/redirect/gh/prschmid/hierarchable/tree/main)
|
|
4
|
+
[](https://badge.fury.io/rb/hierarchable)
|
|
4
5
|
|
|
5
6
|
A simple way to define cross model hierarchical (parent, child, sibling) relationships between ActiveRecord models.
|
|
6
7
|
|
|
@@ -24,14 +25,14 @@ Or install it yourself as:
|
|
|
24
25
|
|
|
25
26
|
Once the gem is installed, you will need to make sure that your models have the correct columns.
|
|
26
27
|
|
|
27
|
-
* hierarchy_root
|
|
28
|
-
* hierarchy_parent
|
|
29
|
-
* hierarchy_ancestors_path
|
|
28
|
+
* `hierarchy_root`: The root node in the hierarchy hierarchy (polymorphic)
|
|
29
|
+
* `hierarchy_parent`: The parent of the current object (polymorphic)
|
|
30
|
+
* `hierarchy_ancestors_path`: The string representation of all ancestors of
|
|
30
31
|
the current object (string).
|
|
31
32
|
|
|
32
33
|
Assuming that you are using UUIDs for your IDs, this can be done by adding the following to the model(s) for which you wish to have hierarchy information:
|
|
33
34
|
|
|
34
|
-
```
|
|
35
|
+
```ruby
|
|
35
36
|
t.references :hierarchy_root,
|
|
36
37
|
polymorphic: true,
|
|
37
38
|
null: true,
|
|
@@ -59,13 +60,13 @@ We will describe the usage using a simplistic Project and Task analogy where we
|
|
|
59
60
|
class Project
|
|
60
61
|
include Hierarchable
|
|
61
62
|
hierarchable
|
|
62
|
-
# If desired, could explicitly
|
|
63
|
-
# the same
|
|
63
|
+
# If desired, one could explicitly set the parent source to `nil` for clarity,
|
|
64
|
+
# but this is the same as omitting it.
|
|
64
65
|
# hierarchable parent_source: nil
|
|
65
66
|
end
|
|
66
67
|
```
|
|
67
68
|
|
|
68
|
-
This will set up the `Project` as the root of the hierarchy. When a `Project` model is saved, it will not have any values for the hierarchy_root, hierarchy_parent, or hierarchy_ancestors_path. This is because for the root item
|
|
69
|
+
This will set up the `Project` as the root of the hierarchy. When a `Project` model is saved, it will not have any values for the hierarchy_root, hierarchy_parent, or hierarchy_ancestors_path (i.e. they are `nil`). This is because for the root item we are not guaranteed to have an ID for the object until after it is saved. Thus, there is no consistent way for us to set these values across different use cases. This doesn't affect any of the usage of the library, it's just something to keep in mind.
|
|
69
70
|
|
|
70
71
|
```ruby
|
|
71
72
|
project = Project.create!
|
|
@@ -148,7 +149,7 @@ project.hierarchy_descendants
|
|
|
148
149
|
|
|
149
150
|
The major distinction for what is returned is whether you are querying "up the hierarchy" or "down the hierarchy". As there is only 1 path up the hierchy to get to the root, the return values of `hierarchy_ancestors` is a list and `hierarchy_parent` is a single object. However, traversing down the list is a little more tricky as there are various models and potential paths to get all the way do to the leaves. As such, for all methods at the same level or going down the tree (`hierarchy_siblings`, `hierarchy_children`, and `hierarchy_descendants`), the return value is a hash that has the model class as the key, and either a `ActiveRecord::Relation` or a list as the value. For example, for a Project model that has tasks and milestones as descendants, the return value might be something like
|
|
150
151
|
|
|
151
|
-
```
|
|
152
|
+
```ruby
|
|
152
153
|
{
|
|
153
154
|
'Task': [all descendant tasks starting at the project]
|
|
154
155
|
'Milestone': [all descendant milestones starting at the project]
|
|
@@ -166,7 +167,7 @@ All of the methods (except `hierarchy_parent`) take a `models` paramter that can
|
|
|
166
167
|
|
|
167
168
|
There are times when we only need to get the siblings/children/descendants of one type and having a hash returned is a little cumbersome. To deal with this case, you can pass `compact: true` as a parameter and it will return just single result not as a hash. For example:
|
|
168
169
|
|
|
169
|
-
```
|
|
170
|
+
```ruby
|
|
170
171
|
# Returns as a hash of the form `{Task: [..all descendants..]}`
|
|
171
172
|
project.hierarch_descendants(models: ['Task'])
|
|
172
173
|
|
|
@@ -254,8 +255,6 @@ class SomeObject
|
|
|
254
255
|
end
|
|
255
256
|
```
|
|
256
257
|
|
|
257
|
-
Note: For the use case that this library was designed (e.g. creating breadcrumbs) this was a limitation that was perfectly acceptible. In the future we may plan to letusers create an optional "ancestry" table to make this more efficient. Once this table exists, inserts and updates will be slower as an extra object will need to be managed, but queries descenants will be improved.
|
|
258
|
-
|
|
259
258
|
### Configuring the separators
|
|
260
259
|
|
|
261
260
|
By default the separators to use for the path and records are `/` and `|` respectively. This means that a hierarchy path will look something like
|
|
@@ -689,9 +689,9 @@ module Hierarchable
|
|
|
689
689
|
return false if source.blank?
|
|
690
690
|
|
|
691
691
|
changed_method = "#{source}_id_changed?"
|
|
692
|
-
public_send(changed_method) if respond_to?(changed_method)
|
|
692
|
+
return public_send(changed_method) if respond_to?(changed_method)
|
|
693
693
|
|
|
694
|
-
send(source)
|
|
694
|
+
send(source)&.id == hierarchy_parent_id
|
|
695
695
|
end
|
|
696
696
|
|
|
697
697
|
# Update the hierarchy_ancestors_path if the hierarchy has changed.
|
data/lib/hierarchable/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hierarchable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Patrick R. Schmid
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2023-01-
|
|
11
|
+
date: 2023-01-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|