parentry 0.2.0 → 0.3.0

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: f3f1eabb0151c4d46831ffbe3d442403509888d0
4
- data.tar.gz: 9c049edc49491eb47f152c175cadf24e118d7b11
3
+ metadata.gz: 6196cdde92cc6aa6dd52c59caaeb186f7ba46a09
4
+ data.tar.gz: cf93a7d1fd294bf3b26b4a2543ef542118877d94
5
5
  SHA512:
6
- metadata.gz: 448cc42620b9053eaec75d101bd2eaf3a4595429e013b39cc4e712bba955b656a6eb8be92f2255c9ec1a10b5b46f10adb3071a12fa629ec315ae082e450c6a50
7
- data.tar.gz: 525a7bc48fd91ab28519d78add1fbb94f70baf782d071678c16648e22b7abda76117513bda2599b633976f50d4312b3efc6ccea68a241efe9bf6d5a3fff4e146
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.split('.').uniq == computed.split('.')
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
@@ -13,7 +13,7 @@ module Parentry
13
13
  end
14
14
 
15
15
  def path_ids
16
- parentry.split('.').map(&:to_i)
16
+ parse_parentry
17
17
  end
18
18
 
19
19
  def path(scopes = {})
@@ -1,3 +1,3 @@
1
1
  module Parentry
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
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) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parentry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hamed Asghari