acts_as_recursive_tree 3.1.0 → 3.2.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
  SHA256:
3
- metadata.gz: b3681366c263bd90e2479c6a4761042825547241a15e828678c260789f05ad54
4
- data.tar.gz: c526e787809100bcba957ddeae424ffa068afe7f10e8e2ece1aec354cce6c70b
3
+ metadata.gz: 001a83205a1b8198bce2a13dda33718cd7733f67d99b8dd7f77562d8a9c16abf
4
+ data.tar.gz: 8ba160e08df9f2b5b0c508ed94c404e17dcde6206c14cafc6cc6d1c0794e13bd
5
5
  SHA512:
6
- metadata.gz: 2b4e5b9a5a1462b39cc8c2cc0184d8db0653b1d02be8728d2bf46fa309d847224765c3e6b8ac885f064a42f64b34375d9e032e888d5588c9940d55225ceced4a
7
- data.tar.gz: 4e8a2077169e98b889671412c87c80d21f03541bbcdc62cefe520e47c626459c29ff1ee6a07bb54a9ebd94ca8418c4369d27ff9950a3c559b0bd7ef41970afc6
6
+ metadata.gz: a3e9db36bef6ef087286641f6b58c30fc1bea95368a9bea2bfce0210fea9418cdfb9c897d9835a2c1f189a681472d885108602eb5b8186868621263242d0cdab
7
+ data.tar.gz: c7eb40f03f62e577da3a5baa6261f61408251f6ec0e0253fa18d1f11b7446487cc87c529a1960688ff8ef40468e34328d28cd155a11ff5de2da40d01f684e843
@@ -19,9 +19,21 @@ jobs:
19
19
  runs-on: ubuntu-latest
20
20
  strategy:
21
21
  matrix:
22
- ruby-version: ['2.5', '2.6', '2.7', '3.0']
22
+ ruby-version: ['2.5', '2.6', '2.7', '3.0', '3.1', '3.2']
23
23
  gemfile: [ar_52, ar_60, ar_61, ar_70]
24
24
  exclude:
25
+ - ruby-version: '3.2'
26
+ gemfile: ar_52
27
+ - ruby-version: '3.2'
28
+ gemfile: ar_60
29
+ - ruby-version: '3.2'
30
+ gemfile: ar_61
31
+ - ruby-version: '3.1'
32
+ gemfile: ar_52
33
+ - ruby-version: '3.1'
34
+ gemfile: ar_60
35
+ - ruby-version: '3.1'
36
+ gemfile: ar_61
25
37
  - ruby-version: '3.0'
26
38
  gemfile: ar_52
27
39
  - ruby-version: '2.5'
@@ -31,7 +43,7 @@ jobs:
31
43
  env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
32
44
  BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
33
45
  steps:
34
- - uses: actions/checkout@v2
46
+ - uses: actions/checkout@v3
35
47
  - name: Set up Ruby
36
48
  # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
37
49
  # change this to (see https://github.com/ruby/setup-ruby#versioning):
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ db.log
17
17
  test.sqlite3
18
18
  /gemfiles/*.lock
19
19
  /gemfiles/.bundle
20
+ /.ruby-version
data/Appraisals CHANGED
@@ -16,6 +16,6 @@ appraise 'ar-61' do
16
16
  end
17
17
 
18
18
  appraise 'ar-70' do
19
- gem 'activerecord', '~> 7.0.0'
20
- gem 'activesupport', '~> 7.0.0'
19
+ gem 'activerecord', '~> 7.0.1'
20
+ gem 'activesupport', '~> 7.0.1'
21
21
  end
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ### Version 3.2.0
2
+ - Added #preload_tree method to preload the parent/child relations of a single node
3
+
1
4
  ### Version 3.1.0
2
5
  - Rails 7 support
3
6
 
data/README.md CHANGED
@@ -19,6 +19,17 @@ ActsAsRecursiveTree currently supports following ActiveRecord versions and is te
19
19
  * ActiveRecord 6.1.x
20
20
  * ActiveRecord 7.0.x
21
21
 
22
+ ## Supported Rubies
23
+ ActsAsRecursiveTree is tested with following rubies:
24
+ * MRuby 2.5
25
+ * MRuby 2.6
26
+ * MRuby 2.7
27
+ * MRuby 3.0
28
+ * MRuby 3.1
29
+ * MRuby 3.2
30
+
31
+ Other Ruby implementations are not tested, but should also work.
32
+
22
33
  ## Installation
23
34
 
24
35
  Add this line to your application's Gemfile:
@@ -123,7 +134,7 @@ __Additional methods:__
123
134
  __Utility methods:__
124
135
  * `root?` - returns true if this node is a root node
125
136
  * `leaf?` - returns true if this node is a leave node
126
-
137
+ * `preload_tree` - fetches all descendants of this node and assignes the proper parent/children associations. You are then able to traverse the tree through the children/parent association without querying the database again.
127
138
 
128
139
  ## Customizing the recursion
129
140
 
@@ -201,17 +212,6 @@ sub_node_instance.descendants # => returns Node and SubNode instances
201
212
  ```
202
213
 
203
214
 
204
- ## Known Issues
205
-
206
- When using PostgreSQL as underlying database system chances are good that you encounter following error message:
207
-
208
- `
209
- ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR: bind message supplies 1 parameters, but prepared statement "" requires 2
210
- `
211
-
212
- This is a known ActiveRecord issue which should be fixed in Rails 5.2. Alternative
213
-
214
-
215
215
  ## Contributing
216
216
 
217
217
  1. Fork it ( https://github.com/1and1/acts_as_recursive_tree/fork )
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 7.0.0"
6
- gem "activesupport", "~> 7.0.0"
5
+ gem "activerecord", "~> 7.0.1"
6
+ gem "activesupport", "~> 7.0.1"
7
7
 
8
8
  gemspec path: "../"
@@ -8,7 +8,8 @@ module ActsAsRecursiveTree
8
8
  # * <tt>foreign_key</tt> - specifies the column name to use for tracking
9
9
  # of the tree (default: +parent_id+)
10
10
  def recursive_tree(parent_key: :parent_id, parent_type_column: nil)
11
- class_attribute :_recursive_tree_config
11
+ class_attribute(:_recursive_tree_config, instance_writer: false)
12
+
12
13
  self._recursive_tree_config = Config.new(
13
14
  model_class: self,
14
15
  parent_key: parent_key.to_sym,
@@ -91,6 +91,14 @@ module ActsAsRecursiveTree
91
91
  children.none?
92
92
  end
93
93
 
94
+ #
95
+ # Fetches all descendants of this node and assigns the parent/children associations
96
+ #
97
+ def preload_tree
98
+ ActsAsRecursiveTree::Preloaders::Descendants.new(self).preload!
99
+ true
100
+ end
101
+
94
102
  def base_class
95
103
  self.class.base_class
96
104
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActsAsRecursiveTree
4
+ module Preloaders
5
+ #
6
+ # Preloads all descendants records for a given node and sets the parent and child associations on each record
7
+ # based on the preloaded data. After this, calling #parent or #children will not trigger a database query.
8
+ #
9
+ class Descendants
10
+ def initialize(node)
11
+ @node = node
12
+ @parent_key = node._recursive_tree_config.parent_key
13
+ end
14
+
15
+ def preload!
16
+ apply_records(@node)
17
+ end
18
+
19
+ private
20
+
21
+ def records
22
+ @records ||= @node.descendants.to_a
23
+ end
24
+
25
+ def apply_records(parent_node)
26
+ children = records.select { |child| child.send(@parent_key) == parent_node.id }
27
+
28
+ parent_node.association(:children).target = children
29
+
30
+ children.each do |child|
31
+ child.association(:parent).target = parent_node
32
+ apply_records(child)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsRecursiveTree
4
- VERSION = '3.1.0'
4
+ VERSION = '3.2.0'
5
5
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe ActsAsRecursiveTree::Preloaders::Descendants do
6
+ include TreeMethods
7
+
8
+ let(:preloader) { described_class.new(root.reload) }
9
+ let(:root) { create_tree(2) }
10
+ let(:children) { root.children }
11
+
12
+ describe '#preload! will set the associations target attribute' do
13
+ before do
14
+ preloader.preload!
15
+ end
16
+
17
+ it 'sets the children assoction' do
18
+ children.each do |child|
19
+ expect(child.association(:children).target).not_to be_nil
20
+ end
21
+ end
22
+
23
+ it 'sets the parent assoction' do
24
+ children.each do |child|
25
+ expect(child.association(:parent).target).not_to be_nil
26
+ end
27
+ end
28
+ end
29
+ end
@@ -3,16 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Node do
6
- def create_tree(max_level, current_level = 0, node = nil)
7
- node = Node.create!(name: 'root') if node.nil?
8
-
9
- 1.upto(max_level - current_level) do |index|
10
- child = node.children.create!(name: "child #{index} - level #{current_level}")
11
- create_tree(max_level, current_level + 1, child)
12
- end
13
-
14
- node
15
- end
6
+ include TreeMethods
16
7
 
17
8
  before do
18
9
  @root = create_tree(3)
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,10 @@ require_relative 'db/database'
11
11
 
12
12
  require 'database_cleaner'
13
13
 
14
+ # Requires supporting ruby files with custom matchers and macros, etc,
15
+ # in spec/support/ and its subdirectories.
16
+ Dir[File.join(__dir__, 'support/**/*.rb')].sort.each { |f| require f }
17
+
14
18
  # This file was generated by the `rspec --init` command. Conventionally, all
15
19
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
16
20
  # The generated `.rspec` file contains `--require spec_helper` which will cause
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Helper methods for simple tree creation
4
+ module TreeMethods
5
+ def create_tree(max_level, current_level = 0, node = nil)
6
+ node = Node.create!(name: 'root') if node.nil?
7
+
8
+ 1.upto(max_level - current_level) do |index|
9
+ child = node.children.create!(name: "child #{index} - level #{current_level}")
10
+ create_tree(max_level, current_level + 1, child)
11
+ end
12
+
13
+ node
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_recursive_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wolfgang Wedelich-John
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-12-20 00:00:00.000000000 Z
12
+ date: 2023-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -222,9 +222,11 @@ files:
222
222
  - lib/acts_as_recursive_tree/options/depth_condition.rb
223
223
  - lib/acts_as_recursive_tree/options/query_options.rb
224
224
  - lib/acts_as_recursive_tree/options/values.rb
225
+ - lib/acts_as_recursive_tree/preloaders/descendants.rb
225
226
  - lib/acts_as_recursive_tree/railtie.rb
226
227
  - lib/acts_as_recursive_tree/scopes.rb
227
228
  - lib/acts_as_recursive_tree/version.rb
229
+ - spec/acts_as_recursive_tree/preloaders/descendants_spec.rb
228
230
  - spec/builders_spec.rb
229
231
  - spec/db/database.rb
230
232
  - spec/db/database.yml
@@ -234,6 +236,7 @@ files:
234
236
  - spec/model/node_spec.rb
235
237
  - spec/model/relation_spec.rb
236
238
  - spec/spec_helper.rb
239
+ - spec/support/tree_methods.rb
237
240
  - spec/values_spec.rb
238
241
  homepage: https://github.com/1and1/acts_as_recursive_tree
239
242
  licenses:
@@ -261,6 +264,7 @@ signing_key:
261
264
  specification_version: 4
262
265
  summary: Drop in replacement for acts_as_tree but using recursive queries
263
266
  test_files:
267
+ - spec/acts_as_recursive_tree/preloaders/descendants_spec.rb
264
268
  - spec/builders_spec.rb
265
269
  - spec/db/database.rb
266
270
  - spec/db/database.yml
@@ -270,4 +274,5 @@ test_files:
270
274
  - spec/model/node_spec.rb
271
275
  - spec/model/relation_spec.rb
272
276
  - spec/spec_helper.rb
277
+ - spec/support/tree_methods.rb
273
278
  - spec/values_spec.rb