parentry 0.2.0 → 0.3.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/lib/parentry/class_methods.rb +1 -0
- data/lib/parentry/instance_methods.rb +30 -2
- data/lib/parentry/navigation.rb +1 -1
- data/lib/parentry/version.rb +1 -1
- data/lib/parentry.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6196cdde92cc6aa6dd52c59caaeb186f7ba46a09
|
4
|
+
data.tar.gz: cf93a7d1fd294bf3b26b4a2543ef542118877d94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4eda67096b326c045dc040b9e47731c150a208d14e9aac2ae607631d4c6c2939dcf8c8a96477c2d8b035ab71c3cedf4d11ee81c719cc833056bd905f246a50b
|
7
|
+
data.tar.gz: dae64307dcdf143e62d19a2d3f20cbcc99ade9d8afdd7b64177014a0b0035b265292d179249b97833586d26004ae4c3b6551ee839d3bc43b82076982a505a7c6
|
@@ -4,6 +4,7 @@ module Parentry
|
|
4
4
|
self.parentry_column = options.fetch(:parentry_column, 'parentry')
|
5
5
|
self.depth_offset = options.fetch(:depth_offset, 0)
|
6
6
|
self.cache_depth = options.fetch(:cache_depth, false)
|
7
|
+
self.touch_ancestors = options.fetch(:touch, false)
|
7
8
|
end
|
8
9
|
|
9
10
|
def arrange(options = {})
|
@@ -5,8 +5,8 @@ module Parentry
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def prevent_circular_parentry
|
8
|
-
computed = compute_parentry
|
9
|
-
errors.add(:parentry, 'contains a circular reference') unless computed.
|
8
|
+
computed = parse_parentry(compute_parentry)
|
9
|
+
errors.add(:parentry, 'contains a circular reference') unless computed.uniq == computed
|
10
10
|
end
|
11
11
|
|
12
12
|
def commit_parentry
|
@@ -40,5 +40,33 @@ module Parentry
|
|
40
40
|
def parentry
|
41
41
|
read_attribute(parentry_column)
|
42
42
|
end
|
43
|
+
|
44
|
+
def parse_parentry(input = parentry)
|
45
|
+
input.to_s.split('.').map(&:to_i)
|
46
|
+
end
|
47
|
+
|
48
|
+
def touch_ancestors_callback
|
49
|
+
return unless touch_ancestors
|
50
|
+
return if touch_callbacks_disabled?
|
51
|
+
|
52
|
+
parentry_scope.where(id: ancestor_ids_was + ancestor_ids).each do |ancestor|
|
53
|
+
ancestor.without_touch_callbacks { ancestor.touch }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def without_touch_callbacks
|
58
|
+
@disable_touch_callbacks = true
|
59
|
+
yield
|
60
|
+
@disable_touch_callbacks = false
|
61
|
+
end
|
62
|
+
|
63
|
+
def touch_callbacks_disabled?
|
64
|
+
@disable_touch_callbacks
|
65
|
+
end
|
66
|
+
|
67
|
+
def ancestor_ids_was
|
68
|
+
return [] unless changes[parentry_column]
|
69
|
+
parse_parentry(changes[parentry_column][0]).tap(&:pop)
|
70
|
+
end
|
43
71
|
end
|
44
72
|
end
|
data/lib/parentry/navigation.rb
CHANGED
data/lib/parentry/version.rb
CHANGED
data/lib/parentry.rb
CHANGED
@@ -7,7 +7,7 @@ module Parentry
|
|
7
7
|
|
8
8
|
def self.included(base)
|
9
9
|
base.class_eval do
|
10
|
-
mattr_accessor :parentry_column, :depth_offset, :cache_depth
|
10
|
+
mattr_accessor :parentry_column, :depth_offset, :cache_depth, :touch_ancestors
|
11
11
|
|
12
12
|
belongs_to :parent, class_name: base_class.name
|
13
13
|
has_many :children, class_name: base_class.name, foreign_key: :parent_id, dependent: :destroy
|
@@ -25,6 +25,10 @@ module Parentry
|
|
25
25
|
after_update :cascade_parentry, if: proc { changes[parentry_column].present? }
|
26
26
|
after_save :cache_parentry_depth, if: proc { cache_depth && depth != parentry_depth }
|
27
27
|
|
28
|
+
after_save :touch_ancestors_callback
|
29
|
+
after_touch :touch_ancestors_callback
|
30
|
+
after_destroy :touch_ancestors_callback
|
31
|
+
|
28
32
|
scope :order_by_parentry, -> { order("nlevel(#{parentry_column})") }
|
29
33
|
|
30
34
|
scope :before_depth, ->(depth) { where("nlevel(#{parentry_column}) - 1 < ?", depth + depth_offset) }
|