ancestry 3.2.1 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -32
- data/README.md +3 -0
- data/lib/ancestry/class_methods.rb +7 -23
- data/lib/ancestry/has_ancestry.rb +3 -8
- data/lib/ancestry/instance_methods.rb +11 -20
- data/lib/ancestry/materialized_path.rb +5 -15
- data/lib/ancestry/materialized_path_pg.rb +15 -15
- data/lib/ancestry/version.rb +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7d01ac0d4c8faa7ad66a0a8e5454625d723fc9226a7e0eb84b358fd25003148
|
4
|
+
data.tar.gz: e27cf882086e2c674972251bf35a52eca003d601dcf5d9ba390c1785f8900033
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2b1a7a69fa27d6f40f1c39a700c5926d3fa1b5da4637800b87d1ad274458cb0ca39a497d505f2b6224031914a484349cef8c433b191ba131e1396431fce767d
|
7
|
+
data.tar.gz: 9fd95019be895afcc8eeee14be8a7776a90a29ded86d25a3cb82a92097cfa7a141f8a765a062e4ac6f5665e6bf7656d8e9fbde030329b713f286f9d96fa179b7
|
data/CHANGELOG.md
CHANGED
@@ -3,15 +3,22 @@
|
|
3
3
|
Doing our best at supporting [SemVer](http://semver.org/) with
|
4
4
|
a nice looking [Changelog](http://keepachangelog.com).
|
5
5
|
|
6
|
-
## Version [HEAD] <
|
6
|
+
## Version [HEAD] <sub><sup>now</sub></sup>
|
7
7
|
|
8
|
-
|
8
|
+
## Version [4.0.0] <sub><sup>2021-04-12</sub></sup>
|
9
9
|
|
10
|
-
|
10
|
+
* dropped support for rails 4.2 and 5.0 (thx @d-m-u)
|
11
|
+
* better documentation counter cache option (thx @pustomytnyk)
|
12
|
+
* clean up code (thx @amatsuda @d-m-u)
|
13
|
+
* fixed rails 6.1 support (thx @cmr119 @d-staehler @danini-the-panini )
|
14
|
+
* phasing out `parent_id?`, `ancestors?` and using `has_parent?` instead
|
15
|
+
* fixed postgres order bug on rails 6.2 and higher (thx @smoyt)
|
16
|
+
|
17
|
+
## Version [3.2.1] <sub><sup>2020-09-23</sub></sup>
|
11
18
|
|
12
19
|
* fixed gemspec to include locales and pg (thx @HectorMF)
|
13
20
|
|
14
|
-
##
|
21
|
+
## Version [3.2.0] <sub><sup>2020-09-23</sub></sup>
|
15
22
|
|
16
23
|
* introduce i18n
|
17
24
|
* pg sql optimization for ancestry changes (thx @suonlight and @geis)
|
@@ -20,7 +27,7 @@ a nice looking [Changelog](http://keepachangelog.com).
|
|
20
27
|
* able to convert to ancestry from a parent_id column with a different name
|
21
28
|
* documentation fixes for better diagrams and grammar (thx @dtamais, @d-m-u, and @CamilleDrapier)
|
22
29
|
|
23
|
-
##
|
30
|
+
## Version [3.1.0] <sub><sup>2020-08-03</sub></sup>
|
24
31
|
|
25
32
|
* `:primary_key_format` method lets you change syntax. good for uuids.
|
26
33
|
* changed code from being `ancestry` string to `ancestry_ids` focused. May break monkey patches.
|
@@ -29,16 +36,16 @@ a nice looking [Changelog](http://keepachangelog.com).
|
|
29
36
|
* Better documentation for relationships (thnx @dtamai and @d-m-u)
|
30
37
|
* Fix creating children in `after_*` callbacks (thx @jstirk)
|
31
38
|
|
32
|
-
## Version [3.0.7] <
|
39
|
+
## Version [3.0.7] <sub><sup>2018-11-06</sub></sup>
|
33
40
|
|
34
41
|
* Fixed rails 5.1 change detection (thx @jrafanie)
|
35
42
|
* Introduce counter cache (thx @hw676018683)
|
36
43
|
|
37
|
-
## Version [3.0.6] <
|
44
|
+
## Version [3.0.6] <sub><sup>2018-11-06</sub></sup>
|
38
45
|
|
39
46
|
* Fixed rails 4.1 version check (thx @myxoh)
|
40
47
|
|
41
|
-
## Version [3.0.5] <
|
48
|
+
## Version [3.0.5] <sub><sup>2018-11-06</sub></sup>
|
42
49
|
|
43
50
|
## Changed
|
44
51
|
|
@@ -49,14 +56,14 @@ a nice looking [Changelog](http://keepachangelog.com).
|
|
49
56
|
|
50
57
|
* Reduced memory footprint of parsing ancestry column (thx @NickLaMuro)
|
51
58
|
|
52
|
-
## Version [3.0.4] <
|
59
|
+
## Version [3.0.4] <sub><sup>2018-10-27</sub></sup>
|
53
60
|
|
54
61
|
## Fixes
|
55
62
|
|
56
63
|
* Properly detects non-integer columns (thx @adam101)
|
57
64
|
* Arrange no longer drops nodes due to missing parents (thx @trafium)
|
58
65
|
|
59
|
-
## Version [3.0.3] <
|
66
|
+
## Version [3.0.3] <sub><sup>2018-10-23</sub></sup>
|
60
67
|
|
61
68
|
This branch (3.x) should still be compatible with rails 3 and 4.
|
62
69
|
Rails 5.1 and 5.2 support were introduced in this version, but ongoing support
|
@@ -72,7 +79,7 @@ has been moved to ancestry 4.0
|
|
72
79
|
* Dropped builds for ruby 1.9.3, 2.0, 2.1, and 2.2
|
73
80
|
* Dropped builds for Rails 3.x and 4.x (will use Active Record `or` syntax)
|
74
81
|
|
75
|
-
## Version [3.0.2] <
|
82
|
+
## Version [3.0.2] <sub><sup>2018-04-24</sub></sup>
|
76
83
|
|
77
84
|
## Fixes
|
78
85
|
|
@@ -82,7 +89,7 @@ has been moved to ancestry 4.0
|
|
82
89
|
* added missing `Ancestry::version`
|
83
90
|
* added Rails 5.2 support (thx @jjuliano)
|
84
91
|
|
85
|
-
## Version [3.0.1] <
|
92
|
+
## Version [3.0.1] <sub><sup>2017-07-05</sub></sup>
|
86
93
|
|
87
94
|
## Fixes
|
88
95
|
|
@@ -93,7 +100,7 @@ has been moved to ancestry 4.0
|
|
93
100
|
* fixed tests on mysql 5.7 and rails 3.2
|
94
101
|
* Dropped 3.1 scope changes
|
95
102
|
|
96
|
-
## Version [3.0.0] <
|
103
|
+
## Version [3.0.0] <sub><sup>2017-05-18</sub></sup>
|
97
104
|
|
98
105
|
## Changed
|
99
106
|
|
@@ -109,7 +116,7 @@ has been moved to ancestry 4.0
|
|
109
116
|
* Properly touches parents when different class for STI (thx @samtgarson)
|
110
117
|
* Fixed issues with parent_id (only present on master) (thx @domcleal)
|
111
118
|
|
112
|
-
## Version [2.2.2] <
|
119
|
+
## Version [2.2.2] <sub><sup>2016-11-01</sub></sup>
|
113
120
|
|
114
121
|
### Changed
|
115
122
|
|
@@ -117,7 +124,7 @@ has been moved to ancestry 4.0
|
|
117
124
|
* Fixed bug with explicit order clauses (introduced in 2.2.0)
|
118
125
|
* No longer load schema on `has_ancestry` load (thx @ledermann)
|
119
126
|
|
120
|
-
## Version [2.2.1] <
|
127
|
+
## Version [2.2.1] <sub><sup>2016-10-25</sub></sup>
|
121
128
|
|
122
129
|
Sorry for blip, local master got out of sync with upstream master.
|
123
130
|
Missed 2 commits (which are feature adds)
|
@@ -126,7 +133,7 @@ Missed 2 commits (which are feature adds)
|
|
126
133
|
* Use like (vs ilike) for rails 5.0 (performance enhancement)
|
127
134
|
* Use `COALESCE` for sorting on pg, mysql, and sqlite vs `CASE`
|
128
135
|
|
129
|
-
## Version [2.2.0] <
|
136
|
+
## Version [2.2.0] <sub><sup>2016-10-25</sub></sup>
|
130
137
|
|
131
138
|
### Added
|
132
139
|
* Predicates for scopes: e.g.: `ancestor_of?`, `parent_of?` (thx @neglectedvalue)
|
@@ -141,7 +148,7 @@ Missed 2 commits (which are feature adds)
|
|
141
148
|
* Upgrading tests for ruby versions (thx @brocktimus, @fryguy, @yui-knk)
|
142
149
|
* Fix non-default ancestry not getting used properly (thx @javiyu)
|
143
150
|
|
144
|
-
## Version [2.1.0]
|
151
|
+
## Version [2.1.0] <sub><sup>2014-04-16</sub></sup>
|
145
152
|
* Added arrange_serializable (thx @krishandley, @chicagogrrl)
|
146
153
|
* Add the :touch to update ancestors on save (thx @adammck)
|
147
154
|
* Change conditions into arel (thx @mlitwiniuk)
|
@@ -150,7 +157,7 @@ Missed 2 commits (which are feature adds)
|
|
150
157
|
* Performance tweak (thx @mjc)
|
151
158
|
* Improvements to organization (thx @xsuchy, @ryakh)
|
152
159
|
|
153
|
-
## Version [2.0.0] <
|
160
|
+
## Version [2.0.0] <sub><sup>2013-05-17</sub></sup>
|
154
161
|
* Removed rails 2 compatibility
|
155
162
|
* Added table name to condition constructing methods (thx @aflatter)
|
156
163
|
* Fix depth_cache not being updated when moving up to ancestors (thx @scottatron)
|
@@ -162,31 +169,31 @@ Missed 2 commits (which are feature adds)
|
|
162
169
|
* New adopt strategy (thx unknown)
|
163
170
|
* Many more improvements
|
164
171
|
|
165
|
-
## Version [1.3.0] <
|
172
|
+
## Version [1.3.0] <sub><sup>2012-05-04</sub></sup>
|
166
173
|
* Ancestry now ignores default scopes when moving or destroying nodes, ensuring tree consistency
|
167
174
|
* Changed ActiveRecord dependency to 2.3.14
|
168
175
|
|
169
|
-
## Version [1.2.5] <
|
176
|
+
## Version [1.2.5] <sub><sup>2012-03-15</sub></sup>
|
170
177
|
* Fixed warnings: "parenthesize argument(s) for future version"
|
171
178
|
* Fixed a bug in the restore_ancestry_integrity! method (thx Arthur Holstvoogd)
|
172
179
|
|
173
|
-
## Version [1.2.4] <
|
180
|
+
## Version [1.2.4] <sub><sup>2011-04-22</sub></sup>
|
174
181
|
* Prepended table names to column names in queries (thx @raelik)
|
175
182
|
* Better check to see if acts_as_tree can be overloaded (thx @jims)
|
176
183
|
* Performance inprovements (thx @kueda)
|
177
184
|
|
178
|
-
## Version [1.2.3] <
|
185
|
+
## Version [1.2.3] <sub><sup>2010-10-28</sub></sup>
|
179
186
|
* Fixed error with determining ActiveRecord version
|
180
187
|
* Added option to specify :primary_key_format (thx @rolftimmermans)
|
181
188
|
|
182
|
-
## Version [1.2.2] <
|
189
|
+
## Version [1.2.2] <sub><sup>2010-10-24</sub></sup>
|
183
190
|
* Fixed all deprecation warnings for rails 3.0.X
|
184
191
|
* Added `:report` option to `check_ancestry_integrity!`
|
185
192
|
* Changed ActiveRecord dependency to 2.2.2
|
186
193
|
* Tested and fixed for ruby 1.8.7 and 1.9.2
|
187
194
|
* Changed usage of `update_attributes` to `update_attribute` to allow ancestry column protection
|
188
195
|
|
189
|
-
## Version [1.2.0] <
|
196
|
+
## Version [1.2.0] <sub><sup>2009-11-07</sub></sup>
|
190
197
|
* Removed some duplication in has_ancestry
|
191
198
|
* Cleaned up plugin pattern according to http://yehudakatz.com/2009/11/12/better-ruby-idioms/
|
192
199
|
* Moved parts of ancestry into seperate files
|
@@ -197,23 +204,23 @@ Missed 2 commits (which are feature adds)
|
|
197
204
|
* Updated ordered_by_ancestry scope to support Microsoft SQL Server
|
198
205
|
* Added empty hash as parameter to exists? calls for older ActiveRecord versions
|
199
206
|
|
200
|
-
## Version [1.1.4] <
|
207
|
+
## Version [1.1.4] <sub><sup>2009-11-07</sub></sup>
|
201
208
|
* Thanks to a patch from tom taylor, Ancestry now works with different primary keys
|
202
209
|
|
203
|
-
## Version [1.1.3] <
|
210
|
+
## Version [1.1.3] <sub><sup>2009-11-01</sub></sup>
|
204
211
|
* Fixed a pretty bad bug where several operations took far too many queries
|
205
212
|
|
206
|
-
## Version [1.1.2] <
|
213
|
+
## Version [1.1.2] <sub><sup>2009-10-29</sub></sup>
|
207
214
|
* Added validation for depth cache column
|
208
215
|
* Added STI support (reported broken)
|
209
216
|
|
210
|
-
## Version [1.1.1] <
|
217
|
+
## Version [1.1.1] <sub><sup>2009-10-28</sub></sup>
|
211
218
|
* Fixed some parentheses warnings that where reported
|
212
219
|
* Fixed a reported issue with arrangement
|
213
220
|
* Fixed issues with ancestors and path order on postgres
|
214
221
|
* Added ordered_by_ancestry scope (needed to fix issues)
|
215
222
|
|
216
|
-
## Version [1.1.0] <
|
223
|
+
## Version [1.1.0] <sub><sup>2009-10-22</sub></sup>
|
217
224
|
* Depth caching (and cache rebuilding)
|
218
225
|
* Depth method for nodes
|
219
226
|
* Named scopes for selecting by depth
|
@@ -236,7 +243,7 @@ Missed 2 commits (which are feature adds)
|
|
236
243
|
* Removed rails specific init
|
237
244
|
* Removed uninstall script
|
238
245
|
|
239
|
-
## Version 1.0.0 <
|
246
|
+
## Version 1.0.0 <sub><sup>2009-10-16</sub></sup>
|
240
247
|
* Initial version
|
241
248
|
* Tree building
|
242
249
|
* Tree navigation
|
@@ -248,8 +255,10 @@ Missed 2 commits (which are feature adds)
|
|
248
255
|
* Validations
|
249
256
|
|
250
257
|
|
251
|
-
[HEAD]: https://github.com/stefankroes/ancestry/compare/
|
252
|
-
[
|
258
|
+
[HEAD]: https://github.com/stefankroes/ancestry/compare/v4.0.0...HEAD
|
259
|
+
[4.0.0]: https://github.com/stefankroes/ancestry/compare/v3.2.1...v4.0.0
|
260
|
+
[3.2.1]: https://github.com/stefankroes/ancestry/compare/v3.2.0...v3.2.1
|
261
|
+
[3.2.0]: https://github.com/stefankroes/ancestry/compare/v3.1.0...v3.2.0
|
253
262
|
[3.1.0]: https://github.com/stefankroes/ancestry/compare/v3.0.7...v3.1.0
|
254
263
|
[3.0.7]: https://github.com/stefankroes/ancestry/compare/v3.0.6...v3.0.7
|
255
264
|
[3.0.6]: https://github.com/stefankroes/ancestry/compare/v3.0.5...v3.0.6
|
data/README.md
CHANGED
@@ -132,6 +132,9 @@ The has_ancestry method supports the following options:
|
|
132
132
|
By default, primary keys only match integers ([0-9]+)
|
133
133
|
:touch Instruct Ancestry to touch the ancestors of a node when it changes, to
|
134
134
|
invalidate nested key-based caches. (default: false)
|
135
|
+
:counter_cache Boolean whether to create counter cache column accessor.
|
136
|
+
Default column name is `children_count`.
|
137
|
+
Pass symbol to use different column name (default: false)
|
135
138
|
|
136
139
|
# (Named) Scopes
|
137
140
|
|
@@ -16,7 +16,7 @@ module Ancestry
|
|
16
16
|
if [:before_depth, :to_depth, :at_depth, :from_depth, :after_depth].include? scope_name
|
17
17
|
scope.send scope_name, depth + relative_depth
|
18
18
|
else
|
19
|
-
raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_depth_option",
|
19
|
+
raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_depth_option", scope_name: scope_name))
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -217,31 +217,15 @@ module Ancestry
|
|
217
217
|
end
|
218
218
|
|
219
219
|
def unscoped_where
|
220
|
-
|
221
|
-
self.ancestry_base_class.unscoped do
|
222
|
-
yield self.ancestry_base_class
|
223
|
-
end
|
224
|
-
else
|
225
|
-
yield self.ancestry_base_class.unscope(:where)
|
226
|
-
end
|
220
|
+
yield self.ancestry_base_class.default_scoped.unscope(:where)
|
227
221
|
end
|
228
222
|
|
229
223
|
ANCESTRY_UNCAST_TYPES = [:string, :uuid, :text].freeze
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
@primary_key_is_an_integer = !ANCESTRY_UNCAST_TYPES.include?(columns_hash[primary_key.to_s].type)
|
236
|
-
end
|
237
|
-
end
|
238
|
-
else
|
239
|
-
def primary_key_is_an_integer?
|
240
|
-
if defined?(@primary_key_is_an_integer)
|
241
|
-
@primary_key_is_an_integer
|
242
|
-
else
|
243
|
-
@primary_key_is_an_integer = !ANCESTRY_UNCAST_TYPES.include?(type_for_attribute(primary_key).type)
|
244
|
-
end
|
224
|
+
def primary_key_is_an_integer?
|
225
|
+
if defined?(@primary_key_is_an_integer)
|
226
|
+
@primary_key_is_an_integer
|
227
|
+
else
|
228
|
+
@primary_key_is_an_integer = !ANCESTRY_UNCAST_TYPES.include?(type_for_attribute(primary_key).type)
|
245
229
|
end
|
246
230
|
end
|
247
231
|
end
|
@@ -5,7 +5,7 @@ module Ancestry
|
|
5
5
|
raise Ancestry::AncestryException.new(I18n.t("ancestry.option_must_be_hash")) unless options.is_a? Hash
|
6
6
|
options.each do |key, value|
|
7
7
|
unless [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column, :touch, :counter_cache, :primary_key_format, :update_strategy].include? key
|
8
|
-
raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_option",
|
8
|
+
raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_option", key: key.inspect, value: value.inspect))
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -87,12 +87,7 @@ module Ancestry
|
|
87
87
|
|
88
88
|
after_touch :touch_ancestors_callback
|
89
89
|
after_destroy :touch_ancestors_callback
|
90
|
-
|
91
|
-
if ActiveRecord::VERSION::STRING >= '5.1.0'
|
92
|
-
after_save :touch_ancestors_callback, if: :saved_changes?
|
93
|
-
else
|
94
|
-
after_save :touch_ancestors_callback, if: :changed?
|
95
|
-
end
|
90
|
+
after_save :touch_ancestors_callback, if: :saved_changes?
|
96
91
|
end
|
97
92
|
|
98
93
|
def acts_as_tree(*args)
|
@@ -115,5 +110,5 @@ module Ancestry
|
|
115
110
|
end
|
116
111
|
|
117
112
|
ActiveSupport.on_load :active_record do
|
118
|
-
|
113
|
+
extend Ancestry::HasAncestry
|
119
114
|
end
|
@@ -2,7 +2,7 @@ module Ancestry
|
|
2
2
|
module InstanceMethods
|
3
3
|
# Validate that the ancestors don't include itself
|
4
4
|
def ancestry_exclude_self
|
5
|
-
errors.add(:base, I18n.t("ancestry.exclude_self",
|
5
|
+
errors.add(:base, I18n.t("ancestry.exclude_self", class_name: self.class.name.humanize)) if ancestor_ids.include? self.id
|
6
6
|
end
|
7
7
|
|
8
8
|
# Update descendants with new ancestry (before save)
|
@@ -39,7 +39,7 @@ module Ancestry
|
|
39
39
|
when :adopt # make child elements of this node, child of its parent
|
40
40
|
descendants.each do |descendant|
|
41
41
|
descendant.without_ancestry_callbacks do
|
42
|
-
descendant.update_attribute :ancestor_ids, descendant.ancestor_ids.delete_if { |x| x == self.id }
|
42
|
+
descendant.update_attribute :ancestor_ids, (descendant.ancestor_ids.delete_if { |x| x == self.id })
|
43
43
|
end
|
44
44
|
end
|
45
45
|
when :restrict # throw an exception if it has children
|
@@ -78,12 +78,7 @@ module Ancestry
|
|
78
78
|
end
|
79
79
|
|
80
80
|
def update_parent_counter_cache
|
81
|
-
changed =
|
82
|
-
if ActiveRecord::VERSION::STRING >= '5.1.0'
|
83
|
-
saved_change_to_attribute?(self.ancestry_base_class.ancestry_column)
|
84
|
-
else
|
85
|
-
ancestry_changed?
|
86
|
-
end
|
81
|
+
changed = saved_change_to_attribute?(self.ancestry_base_class.ancestry_column)
|
87
82
|
|
88
83
|
return unless changed
|
89
84
|
|
@@ -100,20 +95,16 @@ module Ancestry
|
|
100
95
|
|
101
96
|
# Ancestors
|
102
97
|
|
103
|
-
def
|
98
|
+
def has_parent?
|
104
99
|
ancestor_ids.present?
|
105
100
|
end
|
106
|
-
alias :
|
101
|
+
alias :ancestors? :has_parent?
|
107
102
|
|
108
103
|
def ancestry_changed?
|
109
104
|
column = self.ancestry_base_class.ancestry_column.to_s
|
110
|
-
if ActiveRecord::VERSION::STRING >= '5.1.0'
|
111
105
|
# These methods return nil if there are no changes.
|
112
106
|
# This was fixed in a refactoring in rails 6.0: https://github.com/rails/rails/pull/35933
|
113
107
|
!!(will_save_change_to_attribute?(column) || saved_change_to_attribute?(column))
|
114
|
-
else
|
115
|
-
changed.include?(column)
|
116
|
-
end
|
117
108
|
end
|
118
109
|
|
119
110
|
def sane_ancestor_ids?
|
@@ -121,7 +112,7 @@ module Ancestry
|
|
121
112
|
end
|
122
113
|
|
123
114
|
def ancestors depth_options = {}
|
124
|
-
return self.ancestry_base_class.none unless
|
115
|
+
return self.ancestry_base_class.none unless has_parent?
|
125
116
|
self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.ancestors_of(self)
|
126
117
|
end
|
127
118
|
|
@@ -162,12 +153,12 @@ module Ancestry
|
|
162
153
|
end
|
163
154
|
|
164
155
|
def parent_id
|
165
|
-
ancestor_ids.last if
|
156
|
+
ancestor_ids.last if has_parent?
|
166
157
|
end
|
167
158
|
alias :parent_id? :ancestors?
|
168
159
|
|
169
160
|
def parent
|
170
|
-
unscoped_find(parent_id) if
|
161
|
+
unscoped_find(parent_id) if has_parent?
|
171
162
|
end
|
172
163
|
|
173
164
|
def parent_of?(node)
|
@@ -177,15 +168,15 @@ module Ancestry
|
|
177
168
|
# Root
|
178
169
|
|
179
170
|
def root_id
|
180
|
-
|
171
|
+
has_parent? ? ancestor_ids.first : id
|
181
172
|
end
|
182
173
|
|
183
174
|
def root
|
184
|
-
|
175
|
+
has_parent? ? unscoped_find(root_id) : self
|
185
176
|
end
|
186
177
|
|
187
178
|
def is_root?
|
188
|
-
!
|
179
|
+
!has_parent?
|
189
180
|
end
|
190
181
|
alias :root? :is_root?
|
191
182
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Ancestry
|
2
2
|
module MaterializedPath
|
3
|
-
BEFORE_LAST_SAVE_SUFFIX =
|
4
|
-
IN_DATABASE_SUFFIX =
|
3
|
+
BEFORE_LAST_SAVE_SUFFIX = '_before_last_save'.freeze
|
4
|
+
IN_DATABASE_SUFFIX = '_in_database'.freeze
|
5
5
|
ANCESTRY_DELIMITER='/'.freeze
|
6
6
|
|
7
7
|
def self.extended(base)
|
@@ -38,12 +38,7 @@ module Ancestry
|
|
38
38
|
def indirects_of(object)
|
39
39
|
t = arel_table
|
40
40
|
node = to_node(object)
|
41
|
-
#
|
42
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
43
|
-
where(t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true))
|
44
|
-
else
|
45
|
-
where(t[ancestry_column].matches("#{node.child_ancestry}/%"))
|
46
|
-
end
|
41
|
+
where(t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true))
|
47
42
|
end
|
48
43
|
|
49
44
|
def descendants_of(object)
|
@@ -54,12 +49,7 @@ module Ancestry
|
|
54
49
|
def descendant_conditions(object)
|
55
50
|
t = arel_table
|
56
51
|
node = to_node(object)
|
57
|
-
#
|
58
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
59
|
-
t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true).or(t[ancestry_column].eq(node.child_ancestry))
|
60
|
-
else
|
61
|
-
t[ancestry_column].matches("#{node.child_ancestry}/%").or(t[ancestry_column].eq(node.child_ancestry))
|
62
|
-
end
|
52
|
+
t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true).or(t[ancestry_column].eq(node.child_ancestry))
|
63
53
|
end
|
64
54
|
|
65
55
|
def subtree_of(object)
|
@@ -78,7 +68,7 @@ module Ancestry
|
|
78
68
|
if %w(mysql mysql2 sqlite sqlite3).include?(connection.adapter_name.downcase)
|
79
69
|
reorder(arel_table[ancestry_column], order)
|
80
70
|
elsif %w(postgresql).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::STRING >= "6.1"
|
81
|
-
reorder(Arel::Nodes::Ascending.new(arel_table[ancestry_column]).nulls_first)
|
71
|
+
reorder(Arel::Nodes::Ascending.new(arel_table[ancestry_column]).nulls_first, order)
|
82
72
|
else
|
83
73
|
reorder(
|
84
74
|
Arel::Nodes::Ascending.new(Arel::Nodes::NamedFunction.new('COALESCE', [arel_table[ancestry_column], Arel.sql("''")])),
|
@@ -1,22 +1,22 @@
|
|
1
1
|
module Ancestry
|
2
2
|
module MaterializedPathPg
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
# Update descendants with new ancestry (before save)
|
4
|
+
def update_descendants_with_new_ancestry
|
5
|
+
# If enabled and node is existing and ancestry was updated and the new ancestry is sane ...
|
6
|
+
if !ancestry_callbacks_disabled? && !new_record? && ancestry_changed? && sane_ancestry?
|
7
|
+
ancestry_column = ancestry_base_class.ancestry_column
|
8
|
+
old_ancestry = path_ids_in_database.join(Ancestry::MaterializedPath::ANCESTRY_DELIMITER)
|
9
|
+
new_ancestry = path_ids.join(Ancestry::MaterializedPath::ANCESTRY_DELIMITER)
|
10
|
+
update_clause = [
|
11
|
+
"#{ancestry_column} = regexp_replace(#{ancestry_column}, '^#{old_ancestry}', '#{new_ancestry}')"
|
12
|
+
]
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
if ancestry_base_class.respond_to?(:depth_cache_column) && respond_to?(ancestry_base_class.depth_cache_column)
|
15
|
+
depth_cache_column = ancestry_base_class.depth_cache_column.to_s
|
16
|
+
update_clause << "#{depth_cache_column} = length(regexp_replace(regexp_replace(ancestry, '^#{old_ancestry}', '#{new_ancestry}'), '\\d', '', 'g')) + 1"
|
17
|
+
end
|
18
18
|
|
19
|
-
|
19
|
+
unscoped_descendants.update_all update_clause.join(', ')
|
20
20
|
end
|
21
21
|
end
|
22
22
|
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:
|
4
|
+
version: 4.0.0
|
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:
|
12
|
+
date: 2021-04-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 5.2.4.5
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 5.2.4.5
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: appraisal
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,8 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
130
|
- !ruby/object:Gem::Version
|
131
131
|
version: '0'
|
132
132
|
requirements: []
|
133
|
-
|
134
|
-
rubygems_version: 2.7.6.2
|
133
|
+
rubygems_version: 3.1.2
|
135
134
|
signing_key:
|
136
135
|
specification_version: 4
|
137
136
|
summary: Organize ActiveRecord model into a tree structure
|