ancestry 3.0.4 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5bb7b7759117d88e5bec4658c6c4d691549f606
4
- data.tar.gz: 0314da6d13466c30a6d21252083f37bbf5304bc5
3
+ metadata.gz: 441841ddcb84e00cdc685fe6bfc4ba8db2a184ae
4
+ data.tar.gz: d860eef74743307b1fae929ec652f8ec77474a10
5
5
  SHA512:
6
- metadata.gz: 2bcd4bc5dcf11e3aab9242990a98aefb18bdbb6c7de506d501689de9514ed664467d008037f5699dde52ebe4c5e27e655682d243c6b6fbd394e48eb40766906f
7
- data.tar.gz: 7eddc0d2e5e9a46e7e8404b7447681f0205c7fa7db4075a410c73301cd31ce99988e0087b217a3cd0d7491411c4457e83628d41baf39489038af5ae87a00ec54
6
+ metadata.gz: a75b3aceafbe2a286feff48f0bb8ec1fe4618ad45b80d24ee7c400fdedfcde0144dad477aeae119d7709861068237cd4c12a5d41429b3c7ff6f667b8da47c71f
7
+ data.tar.gz: a1abe3c0d18f3bddb7bf531a7a1a46d5f53a954da1eba0a46741f00b8f0aa4baae7802a172c676ed65afc1b843aea8fe4a7cdc6669241777f31debc8e4025d70
data/README.md CHANGED
@@ -118,6 +118,8 @@ To navigate an Ancestry model, use the following instance methods:
118
118
  |`is_only_child?` <br/> `only_child?` |true if the record is the only child of its parent|
119
119
  |`descendants` |direct and indirect children of the record|
120
120
  |`descendant_ids` |direct and indirect children's ids of the record|
121
+ |`indirects` |indirect children of the record|
122
+ |`indirect_ids` |indirect children's ids of the record|
121
123
  |`subtree` |the model on descendants and itself|
122
124
  |`subtree_ids` |a list of all ids in the record's subtree|
123
125
  |`depth` |the depth of the node, root nodes are at depth 0|
@@ -133,6 +135,8 @@ There are also instance methods to determine the relationship between 2 nodes:
133
135
  |`root_of?(node)` | node's root is this record|
134
136
  |`ancestor_of?(node)`| node's ancestors include this record|
135
137
  |`child_of?(node)` | node is record's parent|
138
+ |`descendant_of?(node)` | node is one of this record's ancestors|
139
+ |`indirect_of?(node)` | node is one of this record's ancestors but not a parent|
136
140
 
137
141
  # Options for `has_ancestry`
138
142
 
@@ -174,6 +178,7 @@ For convenience, a couple of named scopes are included at the class level:
174
178
  ancestors_of(node) Ancestors of node, node can be either a record or an id
175
179
  children_of(node) Children of node, node can be either a record or an id
176
180
  descendants_of(node) Descendants of node, node can be either a record or an id
181
+ indirects_of(node) Indirect children of node, node can be either a record or an id
177
182
  subtree_of(node) Subtree of node, node can be either a record or an id
178
183
  siblings_of(node) Siblings of node, node can be either a record or an id
179
184
 
@@ -41,6 +41,7 @@ module Ancestry
41
41
  scope :roots, lambda { where(root_conditions) }
42
42
  scope :ancestors_of, lambda { |object| where(ancestor_conditions(object)) }
43
43
  scope :children_of, lambda { |object| where(child_conditions(object)) }
44
+ scope :indirects_of, lambda { |object| where(indirect_conditions(object)) }
44
45
  scope :descendants_of, lambda { |object| where(descendant_conditions(object)) }
45
46
  scope :subtree_of, lambda { |object| where(subtree_conditions(object)) }
46
47
  scope :siblings_of, lambda { |object| where(sibling_conditions(object)) }
@@ -275,6 +275,24 @@ module Ancestry
275
275
  ancestor_ids.include?(node.id)
276
276
  end
277
277
 
278
+ # Indirects
279
+
280
+ def indirect_conditions
281
+ self.ancestry_base_class.indirect_conditions(self)
282
+ end
283
+
284
+ def indirects depth_options = {}
285
+ self.ancestry_base_class.ordered_by_ancestry.scope_depth(depth_options, depth).where indirect_conditions
286
+ end
287
+
288
+ def indirect_ids depth_options = {}
289
+ indirects(depth_options).pluck(self.ancestry_base_class.primary_key)
290
+ end
291
+
292
+ def indirect_of?(node)
293
+ ancestor_ids[0..-2].include?(node.id)
294
+ end
295
+
278
296
  # Subtree
279
297
 
280
298
  def subtree_conditions
@@ -302,11 +320,12 @@ module Ancestry
302
320
  end
303
321
 
304
322
  private
323
+ ANCESTRY_DELIMITER = '/'.freeze
305
324
 
306
325
  def parse_ancestry_column obj
307
- obj_ids = obj.to_s.split('/')
308
- obj_ids.map!(&:to_i) if self.class.primary_key_is_an_integer?
309
- obj_ids
326
+ return [] unless obj
327
+ obj_ids = obj.split(ANCESTRY_DELIMITER)
328
+ self.class.primary_key_is_an_integer? ? obj_ids.map!(&:to_i) : obj_ids
310
329
  end
311
330
 
312
331
  def unscoped_descendants
@@ -27,6 +27,18 @@ module Ancestry
27
27
  t[ancestry_column].eq(node.child_ancestry)
28
28
  end
29
29
 
30
+ # indirect = anyone who is a descendant, but not a child
31
+ def indirect_conditions(object)
32
+ t = arel_table
33
+ node = to_node(object)
34
+ # rails has case sensitive matching.
35
+ if ActiveRecord::VERSION::MAJOR >= 5
36
+ t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true)
37
+ else
38
+ t[ancestry_column].matches("#{node.child_ancestry}/%")
39
+ end
40
+ end
41
+
30
42
  def descendant_conditions(object)
31
43
  t = arel_table
32
44
  node = to_node(object)
@@ -1,3 +1,3 @@
1
1
  module Ancestry
2
- VERSION = "3.0.4"
2
+ VERSION = "3.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ancestry
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.4
4
+ version: 3.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Kroes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-03 00:00:00.000000000 Z
12
+ date: 2018-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord