parentry 1.1.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,150 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ parentry (1.5.0)
5
+ activerecord (>= 5.1, < 7.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.0.3.4)
11
+ actionview (= 6.0.3.4)
12
+ activesupport (= 6.0.3.4)
13
+ rack (~> 2.0, >= 2.0.8)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.0.3.4)
18
+ activesupport (= 6.0.3.4)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activemodel (6.0.3.4)
24
+ activesupport (= 6.0.3.4)
25
+ activerecord (6.0.3.4)
26
+ activemodel (= 6.0.3.4)
27
+ activesupport (= 6.0.3.4)
28
+ activesupport (6.0.3.4)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 0.7, < 2)
31
+ minitest (~> 5.1)
32
+ tzinfo (~> 1.1)
33
+ zeitwerk (~> 2.2, >= 2.2.2)
34
+ appraisal (2.3.0)
35
+ bundler
36
+ rake
37
+ thor (>= 0.14.0)
38
+ ast (2.4.1)
39
+ builder (3.2.4)
40
+ coderay (1.1.3)
41
+ combustion (1.3.1)
42
+ activesupport (>= 3.0.0)
43
+ railties (>= 3.0.0)
44
+ thor (>= 0.14.6)
45
+ concurrent-ruby (1.1.7)
46
+ crass (1.0.6)
47
+ database_cleaner (1.8.5)
48
+ diff-lcs (1.4.4)
49
+ docile (1.3.4)
50
+ erubi (1.10.0)
51
+ i18n (1.8.5)
52
+ concurrent-ruby (~> 1.0)
53
+ loofah (2.8.0)
54
+ crass (~> 1.0.2)
55
+ nokogiri (>= 1.5.9)
56
+ method_source (1.0.0)
57
+ mini_portile2 (2.4.0)
58
+ minitest (5.14.2)
59
+ nokogiri (1.10.10)
60
+ mini_portile2 (~> 2.4.0)
61
+ parallel (1.20.1)
62
+ parser (3.0.0.0)
63
+ ast (~> 2.4.1)
64
+ pg (1.2.3)
65
+ pry (0.13.1)
66
+ coderay (~> 1.1)
67
+ method_source (~> 1.0)
68
+ rack (2.2.3)
69
+ rack-test (1.1.0)
70
+ rack (>= 1.0, < 3)
71
+ rails-dom-testing (2.0.3)
72
+ activesupport (>= 4.2.0)
73
+ nokogiri (>= 1.6)
74
+ rails-html-sanitizer (1.3.0)
75
+ loofah (~> 2.3)
76
+ railties (6.0.3.4)
77
+ actionpack (= 6.0.3.4)
78
+ activesupport (= 6.0.3.4)
79
+ method_source
80
+ rake (>= 0.8.7)
81
+ thor (>= 0.20.3, < 2.0)
82
+ rainbow (3.0.0)
83
+ rake (13.0.3)
84
+ regexp_parser (2.0.3)
85
+ rexml (3.2.4)
86
+ rspec-core (3.10.1)
87
+ rspec-support (~> 3.10.0)
88
+ rspec-expectations (3.10.1)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.10.0)
91
+ rspec-mocks (3.10.1)
92
+ diff-lcs (>= 1.2.0, < 2.0)
93
+ rspec-support (~> 3.10.0)
94
+ rspec-rails (4.0.2)
95
+ actionpack (>= 4.2)
96
+ activesupport (>= 4.2)
97
+ railties (>= 4.2)
98
+ rspec-core (~> 3.10)
99
+ rspec-expectations (~> 3.10)
100
+ rspec-mocks (~> 3.10)
101
+ rspec-support (~> 3.10)
102
+ rspec-support (3.10.1)
103
+ rubocop (1.7.0)
104
+ parallel (~> 1.10)
105
+ parser (>= 2.7.1.5)
106
+ rainbow (>= 2.2.2, < 4.0)
107
+ regexp_parser (>= 1.8, < 3.0)
108
+ rexml
109
+ rubocop-ast (>= 1.2.0, < 2.0)
110
+ ruby-progressbar (~> 1.7)
111
+ unicode-display_width (>= 1.4.0, < 2.0)
112
+ rubocop-ast (1.3.0)
113
+ parser (>= 2.7.1.5)
114
+ rubocop-rspec (2.1.0)
115
+ rubocop (~> 1.0)
116
+ rubocop-ast (>= 1.1.0)
117
+ ruby-progressbar (1.11.0)
118
+ simplecov (0.20.0)
119
+ docile (~> 1.1)
120
+ simplecov-html (~> 0.11)
121
+ simplecov_json_formatter (~> 0.1)
122
+ simplecov-html (0.12.3)
123
+ simplecov_json_formatter (0.1.2)
124
+ thor (1.0.1)
125
+ thread_safe (0.3.6)
126
+ tzinfo (1.2.9)
127
+ thread_safe (~> 0.1)
128
+ unicode-display_width (1.7.0)
129
+ zeitwerk (2.4.2)
130
+
131
+ PLATFORMS
132
+ x86_64-darwin-20
133
+ x86_64-linux
134
+
135
+ DEPENDENCIES
136
+ activerecord (~> 6.0, < 6.1)
137
+ appraisal
138
+ bundler (~> 2)
139
+ combustion (~> 1.1)
140
+ database_cleaner (~> 1.6)
141
+ parentry!
142
+ pg (~> 1.2)
143
+ pry (~> 0.13)
144
+ rake (~> 13)
145
+ rspec-rails (~> 4.0)
146
+ rubocop-rspec (~> 2.1)
147
+ simplecov (~> 0.20)
148
+
149
+ BUNDLED WITH
150
+ 2.2.32
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "activerecord", "~> 6.1", "< 6.2"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,149 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ parentry (1.5.0)
5
+ activerecord (>= 5.1, < 7.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.1.0)
11
+ actionview (= 6.1.0)
12
+ activesupport (= 6.1.0)
13
+ rack (~> 2.0, >= 2.0.9)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.1.0)
18
+ activesupport (= 6.1.0)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activemodel (6.1.0)
24
+ activesupport (= 6.1.0)
25
+ activerecord (6.1.0)
26
+ activemodel (= 6.1.0)
27
+ activesupport (= 6.1.0)
28
+ activesupport (6.1.0)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 1.6, < 2)
31
+ minitest (>= 5.1)
32
+ tzinfo (~> 2.0)
33
+ zeitwerk (~> 2.3)
34
+ appraisal (2.3.0)
35
+ bundler
36
+ rake
37
+ thor (>= 0.14.0)
38
+ ast (2.4.1)
39
+ builder (3.2.4)
40
+ coderay (1.1.3)
41
+ combustion (1.3.1)
42
+ activesupport (>= 3.0.0)
43
+ railties (>= 3.0.0)
44
+ thor (>= 0.14.6)
45
+ concurrent-ruby (1.1.7)
46
+ crass (1.0.6)
47
+ database_cleaner (1.8.5)
48
+ diff-lcs (1.4.4)
49
+ docile (1.3.4)
50
+ erubi (1.10.0)
51
+ i18n (1.8.5)
52
+ concurrent-ruby (~> 1.0)
53
+ loofah (2.8.0)
54
+ crass (~> 1.0.2)
55
+ nokogiri (>= 1.5.9)
56
+ method_source (1.0.0)
57
+ mini_portile2 (2.4.0)
58
+ minitest (5.14.2)
59
+ nokogiri (1.10.10)
60
+ mini_portile2 (~> 2.4.0)
61
+ parallel (1.20.1)
62
+ parser (3.0.0.0)
63
+ ast (~> 2.4.1)
64
+ pg (1.2.3)
65
+ pry (0.13.1)
66
+ coderay (~> 1.1)
67
+ method_source (~> 1.0)
68
+ rack (2.2.3)
69
+ rack-test (1.1.0)
70
+ rack (>= 1.0, < 3)
71
+ rails-dom-testing (2.0.3)
72
+ activesupport (>= 4.2.0)
73
+ nokogiri (>= 1.6)
74
+ rails-html-sanitizer (1.3.0)
75
+ loofah (~> 2.3)
76
+ railties (6.1.0)
77
+ actionpack (= 6.1.0)
78
+ activesupport (= 6.1.0)
79
+ method_source
80
+ rake (>= 0.8.7)
81
+ thor (~> 1.0)
82
+ rainbow (3.0.0)
83
+ rake (13.0.3)
84
+ regexp_parser (2.0.3)
85
+ rexml (3.2.4)
86
+ rspec-core (3.10.1)
87
+ rspec-support (~> 3.10.0)
88
+ rspec-expectations (3.10.1)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.10.0)
91
+ rspec-mocks (3.10.1)
92
+ diff-lcs (>= 1.2.0, < 2.0)
93
+ rspec-support (~> 3.10.0)
94
+ rspec-rails (4.0.2)
95
+ actionpack (>= 4.2)
96
+ activesupport (>= 4.2)
97
+ railties (>= 4.2)
98
+ rspec-core (~> 3.10)
99
+ rspec-expectations (~> 3.10)
100
+ rspec-mocks (~> 3.10)
101
+ rspec-support (~> 3.10)
102
+ rspec-support (3.10.1)
103
+ rubocop (1.7.0)
104
+ parallel (~> 1.10)
105
+ parser (>= 2.7.1.5)
106
+ rainbow (>= 2.2.2, < 4.0)
107
+ regexp_parser (>= 1.8, < 3.0)
108
+ rexml
109
+ rubocop-ast (>= 1.2.0, < 2.0)
110
+ ruby-progressbar (~> 1.7)
111
+ unicode-display_width (>= 1.4.0, < 2.0)
112
+ rubocop-ast (1.3.0)
113
+ parser (>= 2.7.1.5)
114
+ rubocop-rspec (2.1.0)
115
+ rubocop (~> 1.0)
116
+ rubocop-ast (>= 1.1.0)
117
+ ruby-progressbar (1.11.0)
118
+ simplecov (0.20.0)
119
+ docile (~> 1.1)
120
+ simplecov-html (~> 0.11)
121
+ simplecov_json_formatter (~> 0.1)
122
+ simplecov-html (0.12.3)
123
+ simplecov_json_formatter (0.1.2)
124
+ thor (1.0.1)
125
+ tzinfo (2.0.4)
126
+ concurrent-ruby (~> 1.0)
127
+ unicode-display_width (1.7.0)
128
+ zeitwerk (2.4.2)
129
+
130
+ PLATFORMS
131
+ x86_64-darwin-20
132
+ x86_64-linux
133
+
134
+ DEPENDENCIES
135
+ activerecord (~> 6.1, < 6.2)
136
+ appraisal
137
+ bundler (~> 2)
138
+ combustion (~> 1.1)
139
+ database_cleaner (~> 1.6)
140
+ parentry!
141
+ pg (~> 1.2)
142
+ pry (~> 0.13)
143
+ rake (~> 13)
144
+ rspec-rails (~> 4.0)
145
+ rubocop-rspec (~> 2.1)
146
+ simplecov (~> 0.20)
147
+
148
+ BUNDLED WITH
149
+ 2.2.32
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "activerecord", "~> 7.0", "< 7.1"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,150 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ parentry (1.5.0)
5
+ activerecord (>= 5.1, < 7.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (7.0.0)
11
+ actionview (= 7.0.0)
12
+ activesupport (= 7.0.0)
13
+ rack (~> 2.0, >= 2.2.0)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (7.0.0)
18
+ activesupport (= 7.0.0)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activemodel (7.0.0)
24
+ activesupport (= 7.0.0)
25
+ activerecord (7.0.0)
26
+ activemodel (= 7.0.0)
27
+ activesupport (= 7.0.0)
28
+ activesupport (7.0.0)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 1.6, < 2)
31
+ minitest (>= 5.1)
32
+ tzinfo (~> 2.0)
33
+ appraisal (2.3.0)
34
+ bundler
35
+ rake
36
+ thor (>= 0.14.0)
37
+ ast (2.4.2)
38
+ builder (3.2.4)
39
+ coderay (1.1.3)
40
+ combustion (1.3.5)
41
+ activesupport (>= 3.0.0)
42
+ railties (>= 3.0.0)
43
+ thor (>= 0.14.6)
44
+ concurrent-ruby (1.1.9)
45
+ crass (1.0.6)
46
+ database_cleaner (1.8.5)
47
+ diff-lcs (1.4.4)
48
+ docile (1.4.0)
49
+ erubi (1.10.0)
50
+ i18n (1.8.11)
51
+ concurrent-ruby (~> 1.0)
52
+ loofah (2.13.0)
53
+ crass (~> 1.0.2)
54
+ nokogiri (>= 1.5.9)
55
+ method_source (1.0.0)
56
+ minitest (5.15.0)
57
+ nokogiri (1.12.5-x86_64-darwin)
58
+ racc (~> 1.4)
59
+ nokogiri (1.12.5-x86_64-linux)
60
+ racc (~> 1.4)
61
+ parallel (1.21.0)
62
+ parser (3.0.3.1)
63
+ ast (~> 2.4.1)
64
+ pg (1.2.3)
65
+ pry (0.14.0)
66
+ coderay (~> 1.1)
67
+ method_source (~> 1.0)
68
+ racc (1.6.0)
69
+ rack (2.2.3)
70
+ rack-test (1.1.0)
71
+ rack (>= 1.0, < 3)
72
+ rails-dom-testing (2.0.3)
73
+ activesupport (>= 4.2.0)
74
+ nokogiri (>= 1.6)
75
+ rails-html-sanitizer (1.4.2)
76
+ loofah (~> 2.3)
77
+ railties (7.0.0)
78
+ actionpack (= 7.0.0)
79
+ activesupport (= 7.0.0)
80
+ method_source
81
+ rake (>= 12.2)
82
+ thor (~> 1.0)
83
+ zeitwerk (~> 2.5)
84
+ rainbow (3.0.0)
85
+ rake (13.0.6)
86
+ regexp_parser (2.1.1)
87
+ rexml (3.2.5)
88
+ rspec-core (3.10.1)
89
+ rspec-support (~> 3.10.0)
90
+ rspec-expectations (3.10.1)
91
+ diff-lcs (>= 1.2.0, < 2.0)
92
+ rspec-support (~> 3.10.0)
93
+ rspec-mocks (3.10.2)
94
+ diff-lcs (>= 1.2.0, < 2.0)
95
+ rspec-support (~> 3.10.0)
96
+ rspec-rails (4.0.2)
97
+ actionpack (>= 4.2)
98
+ activesupport (>= 4.2)
99
+ railties (>= 4.2)
100
+ rspec-core (~> 3.10)
101
+ rspec-expectations (~> 3.10)
102
+ rspec-mocks (~> 3.10)
103
+ rspec-support (~> 3.10)
104
+ rspec-support (3.10.3)
105
+ rubocop (1.22.3)
106
+ parallel (~> 1.10)
107
+ parser (>= 3.0.0.0)
108
+ rainbow (>= 2.2.2, < 4.0)
109
+ regexp_parser (>= 1.8, < 3.0)
110
+ rexml
111
+ rubocop-ast (>= 1.12.0, < 2.0)
112
+ ruby-progressbar (~> 1.7)
113
+ unicode-display_width (>= 1.4.0, < 3.0)
114
+ rubocop-ast (1.12.0)
115
+ parser (>= 3.0.1.1)
116
+ rubocop-rspec (2.5.0)
117
+ rubocop (~> 1.19)
118
+ ruby-progressbar (1.11.0)
119
+ simplecov (0.21.2)
120
+ docile (~> 1.1)
121
+ simplecov-html (~> 0.11)
122
+ simplecov_json_formatter (~> 0.1)
123
+ simplecov-html (0.12.3)
124
+ simplecov_json_formatter (0.1.3)
125
+ thor (1.1.0)
126
+ tzinfo (2.0.4)
127
+ concurrent-ruby (~> 1.0)
128
+ unicode-display_width (2.1.0)
129
+ zeitwerk (2.5.1)
130
+
131
+ PLATFORMS
132
+ x86_64-darwin-20
133
+ x86_64-linux
134
+
135
+ DEPENDENCIES
136
+ activerecord (~> 7.0, < 7.1)
137
+ appraisal
138
+ bundler (~> 2)
139
+ combustion (~> 1.1)
140
+ database_cleaner (~> 1.6)
141
+ parentry!
142
+ pg (~> 1.2)
143
+ pry (~> 0.13)
144
+ rake (~> 13)
145
+ rspec-rails (~> 4.0)
146
+ rubocop-rspec (~> 2.1)
147
+ simplecov (~> 0.20)
148
+
149
+ BUNDLED WITH
150
+ 2.2.32
@@ -11,12 +11,13 @@ module Parentry
11
11
  include Strategy::Array if parentry_strategy == 'array'
12
12
  end
13
13
 
14
+ # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
14
15
  def arrange(options = {})
15
16
  scope =
16
17
  if (order = options.delete(:order))
17
- self.base_class.order_by_parentry.order(order)
18
+ base_class.order_by_parentry.order(order)
18
19
  else
19
- self.base_class.order_by_parentry
20
+ base_class.order_by_parentry
20
21
  end
21
22
 
22
23
  scope.where(options).each_with_object(Hash.new { |h, k| h[k] = {} }) do |node, memo|
@@ -27,5 +28,6 @@ module Parentry
27
28
  insert_node[node] = {}
28
29
  end
29
30
  end
31
+ # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
30
32
  end
31
33
  end
@@ -46,6 +46,7 @@ module Parentry
46
46
 
47
47
  def ancestor_ids_was
48
48
  return [] unless saved_changes[parentry_column]
49
+
49
50
  parse_parentry(saved_changes[parentry_column][0]).tap(&:pop)
50
51
  end
51
52
  end
@@ -33,9 +33,9 @@ module Parentry
33
33
  end
34
34
 
35
35
  def leaf?
36
- children.size == 0
36
+ children.size.zero?
37
37
  end
38
- alias_method :childless?, :leaf?
38
+ alias childless? leaf?
39
39
 
40
40
  def children?
41
41
  !leaf?
@@ -50,7 +50,7 @@ module Parentry
50
50
  end
51
51
 
52
52
  def siblings?
53
- siblings.size > 0
53
+ siblings.size.positive?
54
54
  end
55
55
 
56
56
  def only_child?
@@ -80,6 +80,7 @@ module Parentry
80
80
  def depth
81
81
  return depth_offset if root?
82
82
  return parent.depth + 1 if changes[:parent_id].present?
83
+
83
84
  path_ids.size - 1 + depth_offset
84
85
  end
85
86
 
@@ -3,7 +3,7 @@ module Parentry
3
3
  module Array
4
4
  module ClassMethods
5
5
  def parentry_depth_function
6
- "array_length(#{parentry_column}, 1)"
6
+ Arel.sql("array_length(#{parentry_column}, 1)")
7
7
  end
8
8
  end
9
9
  end
@@ -8,13 +8,14 @@ module Parentry
8
8
 
9
9
  def compute_parentry
10
10
  return [] unless persisted?
11
+
11
12
  parent.present? ? parent.parentry + [id] : [id]
12
13
  end
13
14
 
14
15
  def cascade_parentry
15
16
  old_path, new_path = saved_changes[parentry_column]
16
17
  parentry_scope.where(
17
- ["#{parentry_column} @> ARRAY[:tree] AND id != :id", tree: old_path, id: id]
18
+ ["#{parentry_column} @> ARRAY[:tree] AND id != :id", { tree: old_path, id: id }]
18
19
  ).update_all(
19
20
  [
20
21
  "#{parentry_column} = array_cat(ARRAY[?], #{parentry_column}[?:array_length(#{parentry_column}, 1)])",
@@ -3,7 +3,7 @@ module Parentry
3
3
  module Ltree
4
4
  module ClassMethods
5
5
  def parentry_depth_function
6
- "nlevel(#{parentry_column})"
6
+ Arel.sql("nlevel(#{parentry_column})")
7
7
  end
8
8
  end
9
9
  end
@@ -7,17 +7,17 @@ module Parentry
7
7
  end
8
8
 
9
9
  def compute_parentry
10
- parent.present? ? "#{parent.parentry}.#{id}" : "#{id}"
10
+ parent.present? ? "#{parent.parentry}.#{id}" : id.to_s
11
11
  end
12
12
 
13
13
  def cascade_parentry
14
14
  old_path, new_path = saved_changes[parentry_column]
15
15
  parentry_scope.where(
16
- ["#{parentry_column} <@ :tree AND id != :id", tree: old_path, id: id]
16
+ ["#{parentry_column} <@ :tree AND id != :id", { tree: old_path, id: id }]
17
17
  ).update_all(
18
18
  [
19
19
  "#{parentry_column} = :new_path || subpath(#{parentry_column}, nlevel(:old_path))",
20
- new_path: new_path, old_path: old_path
20
+ { new_path: new_path, old_path: old_path }
21
21
  ]
22
22
  )
23
23
  end
@@ -1,3 +1,3 @@
1
1
  module Parentry
2
- VERSION = '1.1.0'
2
+ VERSION = '1.5.0'.freeze
3
3
  end
data/lib/parentry.rb CHANGED
@@ -1,55 +1,56 @@
1
1
  require 'parentry/version'
2
2
  require 'parentry/strategy'
3
3
 
4
+ require 'active_support/concern'
5
+
4
6
  module Parentry
5
7
  autoload :InstanceMethods, 'parentry/instance_methods'
6
8
  autoload :Navigation, 'parentry/navigation'
7
9
  autoload :ClassMethods, 'parentry/class_methods'
8
10
 
9
- def self.included(base)
10
- base.class_eval do
11
- mattr_accessor :parentry_strategy, :parentry_column, :depth_offset, :cache_depth, :touch_ancestors
11
+ extend ActiveSupport::Concern
12
+
13
+ # rubocop:disable Metrics/BlockLength
14
+ included do
15
+ include Navigation
16
+ include InstanceMethods
12
17
 
13
- belongs_to :parent, class_name: base_class.name, optional: true
14
- has_many :children, class_name: base_class.name, foreign_key: :parent_id, dependent: :destroy
18
+ mattr_accessor :parentry_strategy, :parentry_column, :depth_offset, :cache_depth, :touch_ancestors
15
19
 
16
- validate do
17
- unless parent.blank? || parent.persisted?
18
- errors.add(:parent, 'must be persisted')
19
- end
20
- end
20
+ belongs_to :parent, class_name: base_class.name, optional: true
21
+ has_many :children, class_name: base_class.name, foreign_key: :parent_id, dependent: :destroy
21
22
 
22
- validate :prevent_circular_parentry
23
+ validate do
24
+ errors.add(:parent, 'must be persisted') unless parent.blank? || parent.persisted?
25
+ end
23
26
 
24
- after_create :commit_parentry
25
- before_update :assign_parentry, if: proc { changes[:parent_id].present? }
26
- after_update :cascade_parentry, if: proc { saved_changes[parentry_column].present? }
27
+ validate :prevent_circular_parentry
27
28
 
28
- before_validation :cache_parentry_depth, if: proc { cache_depth }
29
- before_save :cache_parentry_depth, if: proc { cache_depth }
29
+ after_create :commit_parentry
30
+ before_update :assign_parentry, if: proc { changes[:parent_id].present? }
31
+ after_update :cascade_parentry, if: proc { saved_changes[parentry_column].present? }
30
32
 
31
- after_save :touch_ancestors_callback
32
- after_touch :touch_ancestors_callback
33
- after_destroy :touch_ancestors_callback
33
+ before_validation :cache_parentry_depth, if: proc { cache_depth }
34
+ before_save :cache_parentry_depth, if: proc { cache_depth }
34
35
 
35
- scope :order_by_parentry, -> { order(parentry_depth_function) }
36
+ after_save :touch_ancestors_callback
37
+ after_touch :touch_ancestors_callback
38
+ after_destroy :touch_ancestors_callback
36
39
 
37
- scope :before_depth, ->(depth) { where("#{parentry_depth_function} - 1 < ?", depth + depth_offset) }
38
- scope :to_depth, ->(depth) { where("#{parentry_depth_function} - 1 <= ?", depth + depth_offset) }
39
- scope :at_depth, ->(depth) { where("#{parentry_depth_function} - 1 = ?", depth + depth_offset) }
40
- scope :from_depth, ->(depth) { where("#{parentry_depth_function} - 1 >= ?", depth + depth_offset) }
41
- scope :after_depth, ->(depth) { where("#{parentry_depth_function} - 1 > ?", depth + depth_offset) }
40
+ scope :order_by_parentry, -> { order(parentry_depth_function) }
42
41
 
43
- scope :roots, -> { where("#{parentry_depth_function} = 1") }
44
- scope :ancestors_of, ->(node) { where(node.ancestor_conditions).where.not(id: node.id) }
45
- scope :children_of, ->(node) { where(parent_id: node.id) }
46
- scope :descendants_of, ->(node) { subtree_of(node).where.not(id: node.id) }
47
- scope :subtree_of, ->(node) { where(node.subtree_conditions) }
48
- scope :siblings_of, ->(node) { where(parent_id: node.parent_id).where.not(id: node.id) }
49
- end
42
+ scope :before_depth, ->(depth) { where("#{parentry_depth_function} - 1 < ?", depth + depth_offset) }
43
+ scope :to_depth, ->(depth) { where("#{parentry_depth_function} - 1 <= ?", depth + depth_offset) }
44
+ scope :at_depth, ->(depth) { where("#{parentry_depth_function} - 1 = ?", depth + depth_offset) }
45
+ scope :from_depth, ->(depth) { where("#{parentry_depth_function} - 1 >= ?", depth + depth_offset) }
46
+ scope :after_depth, ->(depth) { where("#{parentry_depth_function} - 1 > ?", depth + depth_offset) }
50
47
 
51
- base.send :include, Navigation
52
- base.send :include, InstanceMethods
53
- base.extend ClassMethods
48
+ scope :roots, -> { where("#{parentry_depth_function} = 1") }
49
+ scope :ancestors_of, ->(node) { where(node.ancestor_conditions).where.not(id: node.id) }
50
+ scope :children_of, ->(node) { where(parent_id: node.id) }
51
+ scope :descendants_of, ->(node) { subtree_of(node).where.not(id: node.id) }
52
+ scope :subtree_of, ->(node) { where(node.subtree_conditions) }
53
+ scope :siblings_of, ->(node) { where(parent_id: node.parent_id).where.not(id: node.id) }
54
54
  end
55
+ # rubocop:enable Metrics/BlockLength
55
56
  end