hierarchable 0.2.0 → 0.2.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +14 -4
- data/lib/hierarchable/hierarchable.rb +12 -8
- 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: f108839bd5cf2491856dfdcc1af95e0d50204f32e467c22bb3f4925d0028f5be
|
|
4
|
+
data.tar.gz: 2379a67ad164232c8efb8c33f8ca6117c6e51387feb39585b4e114ce16542736
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 88822385da9430f6183eafbc4873a7b3009461d733768453212c3abaf615bca2e318da1c36ac3fb9a4d46bdb072fcd07655069a9148a314212915e205f34baa0
|
|
7
|
+
data.tar.gz: bc9b0a309a1e31d7da843ff2ed5f1e75384f9812f63bde5d759018f72de51ebab45e8ea32d45f79cd7f12451d56b439bb69b32b2af0cbc06ee62eeab17ca7d44
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -183,15 +183,25 @@ project.hierarchy_descendants(models: :this)
|
|
|
183
183
|
task.hierarchy_siblings
|
|
184
184
|
```
|
|
185
185
|
|
|
186
|
-
In order to figure out the potential descendants of an object we need to inspect the object and query all relations to to see if any of those have this object as an ancestor. In
|
|
186
|
+
In order to figure out the potential descendants of an object we need to inspect the object and query all relations to to see if any of those have this object as an ancestor. In many cases these relations can be inferred correctly by getting all of the `has_many` relationships that a model has defined. To be safe and not return potential duplicate associations, the only associations that are automatically detected are the ones that are the pluralized form of the model name.
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
```ruby
|
|
189
|
+
class Project
|
|
190
|
+
has_many :tasks
|
|
191
|
+
has_many :completed_tasks, -> { completed }, class_name: 'Task'
|
|
192
|
+
has_many :timestamps, class_name: 'MetricLibrary::Timestamp`
|
|
193
|
+
end
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
In the `Project` model defined above, only the `:tasks` association will be used for finding descendants.
|
|
197
|
+
|
|
198
|
+
However there are times when we need to manually add a child relation to be inspected. This can be done in one of two ways. The most common case is if we want to specify additional associations. This will take all of the associations that can be auto-detected and also add in the one provided.
|
|
189
199
|
|
|
190
200
|
```ruby
|
|
191
201
|
class SomeObject
|
|
192
202
|
include Hierarched
|
|
193
203
|
hierarched parent_source: :parent,
|
|
194
|
-
|
|
204
|
+
additional_descendant_associations: [:some_association]
|
|
195
205
|
end
|
|
196
206
|
```
|
|
197
207
|
|
|
@@ -201,7 +211,7 @@ There may also be a case when we want exact control over what associations that
|
|
|
201
211
|
class SomeObject
|
|
202
212
|
include Hierarched
|
|
203
213
|
hierarched parent_source: :parent,
|
|
204
|
-
|
|
214
|
+
descendant_associations: [:some_association]
|
|
205
215
|
end
|
|
206
216
|
```
|
|
207
217
|
|
|
@@ -77,7 +77,7 @@ module Hierarchable
|
|
|
77
77
|
self.hierarchable_config = {
|
|
78
78
|
parent_source: opts.fetch(:parent_source, nil),
|
|
79
79
|
additional_descendant_associations: opts.fetch(
|
|
80
|
-
:
|
|
80
|
+
:additional_descendant_associations, []
|
|
81
81
|
),
|
|
82
82
|
descendant_associations: opts.fetch(:descendant_associations, nil),
|
|
83
83
|
path_separator: opts.fetch(
|
|
@@ -382,10 +382,10 @@ module Hierarchable
|
|
|
382
382
|
hierarchy_root_id: id
|
|
383
383
|
)
|
|
384
384
|
else
|
|
385
|
-
path = public_send(:
|
|
385
|
+
path = public_send(:hierarchy_full_path)
|
|
386
386
|
model.where(
|
|
387
387
|
'hierarchy_ancestors_path LIKE ?',
|
|
388
|
-
"#{model.sanitize_sql_like(path)}
|
|
388
|
+
"#{model.sanitize_sql_like(path)}%"
|
|
389
389
|
)
|
|
390
390
|
end
|
|
391
391
|
if model == self.class
|
|
@@ -421,10 +421,14 @@ module Hierarchable
|
|
|
421
421
|
# Return all of the `has_many` association names this class class has as a
|
|
422
422
|
# list of symbols.
|
|
423
423
|
#
|
|
424
|
-
#
|
|
425
|
-
#
|
|
426
|
-
# the
|
|
427
|
-
#
|
|
424
|
+
# In order to be safe and not return potential duplicate associations,
|
|
425
|
+
# the only associations that are automatically
|
|
426
|
+
# detected are the ones that are the pluralized form of the model name.
|
|
427
|
+
# For example, if a model as the association `has_many :tasks`, there
|
|
428
|
+
# will need to be a Task model for this association to be kept.
|
|
429
|
+
#
|
|
430
|
+
# If there are some associations that need to be manually
|
|
431
|
+
# added, one simply needs to specify them when setting up the model.
|
|
428
432
|
#
|
|
429
433
|
# The most common case is if we want to specify additional associations.
|
|
430
434
|
# This will take all of the associations that can be auto-detected and
|
|
@@ -458,7 +462,7 @@ module Hierarchable
|
|
|
458
462
|
.reject(&:through_reflection?)
|
|
459
463
|
.map(&:name)
|
|
460
464
|
associations += hierarchable_config[:additional_descendant_associations]
|
|
461
|
-
associations
|
|
465
|
+
associations.uniq
|
|
462
466
|
end
|
|
463
467
|
|
|
464
468
|
# Return the string representation of the current object in the format when
|
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.2.
|
|
4
|
+
version: 0.2.1
|
|
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: 2022-12-
|
|
11
|
+
date: 2022-12-23 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|