ancestry 3.0.6 → 3.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/ancestry/has_ancestry.rb +16 -2
- data/lib/ancestry/instance_methods.rb +54 -1
- data/lib/ancestry/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5962b504410b75525481574c43d8122fc4f4607e1c56f8827bbc565a7704adb6
|
4
|
+
data.tar.gz: 51c13aec4d355421c4e095804890c58eba3a8a774ce9286e3eaf28351ac22a11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6dc1d80c7a77fc1f97232634d11e188863d25e7e9da3f74654ada7f4d3fc2951c6962b4f2b516476772eecce98f66db7c4625298465d2f9de4a7d2f17ff2553
|
7
|
+
data.tar.gz: 7199de05c78a159e1e1924de1331617a9cc06d4f1403bd82f88a8da5e7f0c127343c1b905bfb714ba2e0e88c285fb9694f467eda9c56af4089db2077fe5c1da2
|
data/README.md
CHANGED
@@ -4,12 +4,11 @@ module Ancestry
|
|
4
4
|
# Check options
|
5
5
|
raise Ancestry::AncestryException.new("Options for has_ancestry must be in a hash.") unless options.is_a? Hash
|
6
6
|
options.each do |key, value|
|
7
|
-
unless [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column, :touch].include? key
|
7
|
+
unless [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column, :touch, :counter_cache].include? key
|
8
8
|
raise Ancestry::AncestryException.new("Unknown option for has_ancestry: #{key.inspect} => #{value.inspect}.")
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
12
|
# Create ancestry column accessor and set to option or default
|
14
13
|
cattr_accessor :ancestry_column
|
15
14
|
self.ancestry_column = options[:ancestry_column] || :ancestry
|
@@ -78,6 +77,21 @@ module Ancestry
|
|
78
77
|
validates_numericality_of depth_cache_column, :greater_than_or_equal_to => 0, :only_integer => true, :allow_nil => false
|
79
78
|
end
|
80
79
|
|
80
|
+
# Create counter cache column accessor and set to option or default
|
81
|
+
if options[:counter_cache]
|
82
|
+
cattr_accessor :counter_cache_column
|
83
|
+
|
84
|
+
if options[:counter_cache] == true
|
85
|
+
self.counter_cache_column = :children_count
|
86
|
+
else
|
87
|
+
self.counter_cache_column = options[:counter_cache]
|
88
|
+
end
|
89
|
+
|
90
|
+
after_create :increase_parent_counter_cache, if: :has_parent?
|
91
|
+
after_destroy :decrease_parent_counter_cache, if: :has_parent?
|
92
|
+
after_update :update_parent_counter_cache
|
93
|
+
end
|
94
|
+
|
81
95
|
# Create named scopes for depth
|
82
96
|
{:before_depth => '<', :to_depth => '<=', :at_depth => '=', :from_depth => '>=', :after_depth => '>'}.each do |scope_name, operator|
|
83
97
|
scope scope_name, lambda { |depth|
|
@@ -92,6 +92,44 @@ module Ancestry
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
+
# Counter Cache
|
96
|
+
def increase_parent_counter_cache
|
97
|
+
self.class.increment_counter _counter_cache_column, parent_id
|
98
|
+
end
|
99
|
+
|
100
|
+
def decrease_parent_counter_cache
|
101
|
+
# @_trigger_destroy_callback comes from activerecord, which makes sure only once decrement when concurrent deletion.
|
102
|
+
# but @_trigger_destroy_callback began after rails@5.1.0.alpha.
|
103
|
+
# https://github.com/rails/rails/blob/v5.2.0/activerecord/lib/active_record/persistence.rb#L340
|
104
|
+
# https://github.com/rails/rails/pull/14735
|
105
|
+
# https://github.com/rails/rails/pull/27248
|
106
|
+
return if defined?(@_trigger_destroy_callback) && !@_trigger_destroy_callback
|
107
|
+
return if ancestry_callbacks_disabled?
|
108
|
+
|
109
|
+
self.class.decrement_counter _counter_cache_column, parent_id
|
110
|
+
end
|
111
|
+
|
112
|
+
def update_parent_counter_cache
|
113
|
+
changed =
|
114
|
+
if ActiveRecord::VERSION::STRING >= '5.1.0'
|
115
|
+
saved_change_to_attribute?(self.ancestry_base_class.ancestry_column)
|
116
|
+
else
|
117
|
+
ancestry_changed?
|
118
|
+
end
|
119
|
+
|
120
|
+
return unless changed
|
121
|
+
|
122
|
+
if parent_id_was = parent_id_before_last_save
|
123
|
+
self.class.decrement_counter _counter_cache_column, parent_id_was
|
124
|
+
end
|
125
|
+
|
126
|
+
parent_id && self.class.increment_counter(_counter_cache_column, parent_id)
|
127
|
+
end
|
128
|
+
|
129
|
+
def _counter_cache_column
|
130
|
+
self.ancestry_base_class.counter_cache_column.to_s
|
131
|
+
end
|
132
|
+
|
95
133
|
# Ancestors
|
96
134
|
|
97
135
|
def ancestors?
|
@@ -101,7 +139,14 @@ module Ancestry
|
|
101
139
|
alias :has_parent? :ancestors?
|
102
140
|
|
103
141
|
def ancestry_changed?
|
104
|
-
|
142
|
+
column = self.ancestry_base_class.ancestry_column.to_s
|
143
|
+
if ActiveRecord::VERSION::STRING >= '5.1.0'
|
144
|
+
# These methods return nil if there are no changes.
|
145
|
+
# This was fixed in a refactoring in rails 6.0: https://github.com/rails/rails/pull/35933
|
146
|
+
!!(will_save_change_to_attribute?(column) || saved_change_to_attribute?(column))
|
147
|
+
else
|
148
|
+
changed.include?(column)
|
149
|
+
end
|
105
150
|
end
|
106
151
|
|
107
152
|
def ancestor_ids
|
@@ -113,6 +158,7 @@ module Ancestry
|
|
113
158
|
end
|
114
159
|
|
115
160
|
def ancestors depth_options = {}
|
161
|
+
return self.ancestry_base_class.none unless ancestors?
|
116
162
|
self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where ancestor_conditions
|
117
163
|
end
|
118
164
|
|
@@ -130,6 +176,13 @@ module Ancestry
|
|
130
176
|
parse_ancestry_column(send("#{self.ancestry_base_class.ancestry_column}#{BEFORE_LAST_SAVE_SUFFIX}"))
|
131
177
|
end
|
132
178
|
|
179
|
+
def parent_id_before_last_save
|
180
|
+
ancestry_was = send("#{self.ancestry_base_class.ancestry_column}#{BEFORE_LAST_SAVE_SUFFIX}")
|
181
|
+
return unless ancestry_was.present?
|
182
|
+
|
183
|
+
ancestry_was.split(ANCESTRY_DELIMITER).last.to_i
|
184
|
+
end
|
185
|
+
|
133
186
|
def path_ids
|
134
187
|
ancestor_ids + [id]
|
135
188
|
end
|
data/lib/ancestry/version.rb
CHANGED
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
|
+
version: 3.0.7
|
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: 2019-
|
12
|
+
date: 2019-06-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -157,7 +157,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
157
|
- !ruby/object:Gem::Version
|
158
158
|
version: '0'
|
159
159
|
requirements: []
|
160
|
-
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.7.6.2
|
161
162
|
signing_key:
|
162
163
|
specification_version: 4
|
163
164
|
summary: Organize ActiveRecord model into a tree structure
|