mongoid-tree 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/Gemfile +13 -5
- data/README.md +8 -16
- data/Rakefile +2 -13
- data/lib/mongoid/tree/counter_caching.rb +26 -0
- data/lib/mongoid/tree.rb +2 -1
- data/spec/mongoid/tree/counter_caching_spec.rb +39 -0
- data/spec/mongoid/tree/traversal_spec.rb +1 -1
- data/spec/mongoid/tree_spec.rb +13 -5
- data/spec/spec_helper.rb +1 -0
- data/spec/support/logger.rb +9 -0
- data/spec/support/models/node.rb +5 -1
- metadata +32 -18
- data/.rspec +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2d75be10c74b80e4233e3865d42bdca651a741ec3c535b41eee3da042297a195
|
4
|
+
data.tar.gz: 45b2f88c1d46bcd9819b66701d4d16eaef3874a6e92d09fbb4e5cc6bcd06e767
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 697aab3571146827d8a2ec7987890c185e9685b821ff8fcd3c46487d3512c962322990a36c9977b5bde7cc5ae332d153e42a2b920c1904c09efbf47118c4b2d9
|
7
|
+
data.tar.gz: 63426c0b41fbbc0a8559dfeda7f3e5a158b230084fff852dcd984a3919cf22d230d27df0c3b2cb4f2c934e15356859430701fbad00bcfb0d0d7e255e0e494df5
|
data/Gemfile
CHANGED
@@ -2,10 +2,18 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
|
6
|
-
gem '
|
7
|
-
gem '
|
5
|
+
case version = ENV['MONGOID_VERSION'] || '~> 7.0'
|
6
|
+
when 'HEAD' then gem 'mongoid', github: 'mongodb/mongoid'
|
7
|
+
when /8/ then gem 'mongoid', '~> 8.0'
|
8
|
+
when /7/ then gem 'mongoid', '~> 7.0'
|
9
|
+
when /6/ then gem 'mongoid', '~> 6.0'
|
10
|
+
when /5/ then gem 'mongoid', '~> 5.0'
|
11
|
+
when /4/ then gem 'mongoid', '~> 4.0'
|
12
|
+
else gem 'mongoid', version
|
13
|
+
end
|
8
14
|
|
9
|
-
|
10
|
-
gem '
|
15
|
+
unless ENV['CI']
|
16
|
+
gem 'guard-rspec', '>= 0.6.0'
|
17
|
+
gem 'ruby_gntp', '>= 0.3.4'
|
18
|
+
gem 'rb-fsevent' if RUBY_PLATFORM =~ /darwin/
|
11
19
|
end
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# mongoid-tree [](https://github.com/benedikt/mongoid-tree)
|
2
2
|
|
3
3
|
A tree structure for Mongoid documents using the materialized path pattern
|
4
4
|
|
5
5
|
## Requirements
|
6
6
|
|
7
|
-
* mongoid (>= 4.0, <
|
7
|
+
* mongoid (>= 4.0, < 9.0)
|
8
8
|
|
9
9
|
For a mongoid 3.x compatible version, please use mongoid-tree 1.0.x,
|
10
10
|
for a mongoid 2.x compatible version, please use mongoid-tree 0.7.x.
|
@@ -14,13 +14,13 @@ for a mongoid 2.x compatible version, please use mongoid-tree 0.7.x.
|
|
14
14
|
|
15
15
|
To install mongoid_tree, simply add it to your Gemfile:
|
16
16
|
|
17
|
-
gem 'mongoid-tree', :
|
17
|
+
gem 'mongoid-tree', require: 'mongoid/tree'
|
18
18
|
|
19
19
|
In order to get the latest development version of mongoid-tree:
|
20
20
|
|
21
|
-
gem 'mongoid-tree', :
|
21
|
+
gem 'mongoid-tree', git: 'git://github.com/benedikt/mongoid-tree', branch: :main
|
22
22
|
|
23
|
-
You might want to add
|
23
|
+
You might want to add `require: nil` option and explicitly `require 'mongoid/tree'` where needed and finally run
|
24
24
|
|
25
25
|
bundle install
|
26
26
|
|
@@ -30,7 +30,7 @@ To fix issues with the ordering of ancestors, mongoid-tree 2.0 introduces a new
|
|
30
30
|
|
31
31
|
## Usage
|
32
32
|
|
33
|
-
Read the API documentation at
|
33
|
+
Read the API documentation at https://www.rubydoc.info/github/benedikt/mongoid-tree and take a look at the `Mongoid::Tree` module
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
class Node
|
@@ -189,7 +189,7 @@ end
|
|
189
189
|
|
190
190
|
## Build Status
|
191
191
|
|
192
|
-
mongoid-tree is on [
|
192
|
+
mongoid-tree is on [GitHub Actions](https://github.com/benedikt/mongoid-tree/actions) running the specs on Ruby 2.7-3.2 and Mongoid 4.x-8.x.
|
193
193
|
|
194
194
|
## Known issues
|
195
195
|
|
@@ -206,14 +206,6 @@ See [https://github.com/benedikt/mongoid-tree](https://github.com/benedikt/mongo
|
|
206
206
|
See a list of all contributors at [https://github.com/benedikt/mongoid-tree/contributors](https://github.com/benedikt/mongoid-tree/contributors). Thanks a lot everyone!
|
207
207
|
|
208
208
|
|
209
|
-
## Support
|
210
|
-
|
211
|
-
If you like mongoid-tree and want to support the development, I would appreciate a small donation:
|
212
|
-
|
213
|
-
[](http://www.pledgie.com/campaigns/12137)
|
214
|
-
|
215
|
-
[](https://flattr.com/submit/auto?user_id=benediktdeicke&url=https://github.com/benedikt/mongoid-tree&title=mongoid-tree&language=&tags=github&category=software)
|
216
|
-
|
217
209
|
## Copyright
|
218
210
|
|
219
|
-
Copyright (c) 2010-
|
211
|
+
Copyright (c) 2010-2023 Benedikt Deicke. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,25 +1,14 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
1
2
|
require 'rspec/core/rake_task'
|
2
3
|
require 'yard'
|
3
4
|
|
4
|
-
spec = Gem::Specification.load("mongoid-tree.gemspec")
|
5
|
-
|
6
5
|
RSpec::Core::RakeTask.new(:spec)
|
7
6
|
|
8
7
|
task :default => :spec
|
9
8
|
|
10
9
|
YARD::Rake::YardocTask.new(:doc)
|
11
10
|
|
12
|
-
desc "Build the .gem file"
|
13
|
-
task :build do
|
14
|
-
system "gem build #{spec.name}.gemspec"
|
15
|
-
end
|
16
|
-
|
17
|
-
desc "Push the .gem file to rubygems.org"
|
18
|
-
task :release => :build do
|
19
|
-
system "gem push #{spec.name}-#{spec.version}.gem"
|
20
|
-
end
|
21
|
-
|
22
11
|
desc "Open an irb session"
|
23
12
|
task :console do
|
24
13
|
sh "irb -rubygems -I lib -r ./spec/spec_helper.rb"
|
25
|
-
end
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Tree
|
3
|
+
##
|
4
|
+
# = Mongoid::Tree::CounterCaching
|
5
|
+
#
|
6
|
+
# Mongoid::Tree doesn't use a counter cache for the children by default.
|
7
|
+
# To enable counter caching for each node's children, include
|
8
|
+
# both Mongoid::Tree and Mongoid::Tree::CounterCaching into your document.
|
9
|
+
module CounterCaching
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
field :children_count, :type => Integer, :default => 0
|
14
|
+
|
15
|
+
metadata = relations['parent']
|
16
|
+
metadata.options[:counter_cache] = true
|
17
|
+
|
18
|
+
if respond_to?(:add_counter_cache_callbacks)
|
19
|
+
add_counter_cache_callbacks(metadata)
|
20
|
+
else
|
21
|
+
metadata.send(:define_counter_cache_callbacks!)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/mongoid/tree.rb
CHANGED
@@ -83,6 +83,7 @@ module Mongoid
|
|
83
83
|
|
84
84
|
autoload :Ordering, 'mongoid/tree/ordering'
|
85
85
|
autoload :Traversal, 'mongoid/tree/traversal'
|
86
|
+
autoload :CounterCaching, 'mongoid/tree/counter_caching'
|
86
87
|
|
87
88
|
included do
|
88
89
|
has_many :children, :class_name => self.name, :foreign_key => :parent_id, :inverse_of => :parent, :validate => false
|
@@ -431,7 +432,7 @@ module Mongoid
|
|
431
432
|
# @private
|
432
433
|
# @return [undefined]
|
433
434
|
def rearrange
|
434
|
-
if
|
435
|
+
if parent.present?
|
435
436
|
self.parent_ids = parent.parent_ids + [self.parent_id]
|
436
437
|
else
|
437
438
|
self.parent_ids = []
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Tree::CounterCaching do
|
4
|
+
|
5
|
+
subject { CounterCachedNode }
|
6
|
+
|
7
|
+
before do
|
8
|
+
setup_tree <<-ENDTREE
|
9
|
+
node1:
|
10
|
+
- node2:
|
11
|
+
- node3
|
12
|
+
- node4:
|
13
|
+
- node5
|
14
|
+
- node6
|
15
|
+
- node7
|
16
|
+
ENDTREE
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when a child gets created' do
|
20
|
+
it 'should calculate the counter cache' do
|
21
|
+
expect(node(:node1).children_count).to eq(3)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when a child gets destroyed' do
|
26
|
+
it 'should update the counter cache' do
|
27
|
+
node(:node4).destroy
|
28
|
+
expect(node(:node1).children_count).to eq(2)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when a child gets moved' do
|
33
|
+
it 'should update the counter cache' do
|
34
|
+
node(:node6).update(parent: node(:node1))
|
35
|
+
expect(node(:node4).children_count).to eq(1)
|
36
|
+
expect(node(:node1).children_count).to eq(4)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -15,7 +15,7 @@ describe Mongoid::Tree::Traversal do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should complain about unsupported traversal methods" do
|
18
|
-
expect { subject.traverse('non_existing') {} }.to raise_error
|
18
|
+
expect { subject.traverse('non_existing') {} }.to raise_error NoMethodError
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should default to depth_first traversal" do
|
data/spec/mongoid/tree_spec.rb
CHANGED
@@ -7,7 +7,11 @@ describe Mongoid::Tree do
|
|
7
7
|
it "should reference many children as inverse of parent with index" do
|
8
8
|
a = Node.reflect_on_association(:children)
|
9
9
|
expect(a).to be
|
10
|
-
|
10
|
+
if Mongoid::Compatibility::Version.mongoid7?
|
11
|
+
expect(a).to be_kind_of(Mongoid::Association::Referenced::HasMany)
|
12
|
+
else
|
13
|
+
expect(a.macro).to eq(:has_many)
|
14
|
+
end
|
11
15
|
expect(a.class_name).to eq('Node')
|
12
16
|
expect(a.foreign_key).to eq('parent_id')
|
13
17
|
expect(Node.index_specification(:parent_id => 1)).to be
|
@@ -16,7 +20,11 @@ describe Mongoid::Tree do
|
|
16
20
|
it "should be referenced in one parent as inverse of children" do
|
17
21
|
a = Node.reflect_on_association(:parent)
|
18
22
|
expect(a).to be
|
19
|
-
|
23
|
+
if Mongoid::Compatibility::Version.mongoid7?
|
24
|
+
expect(a).to be_kind_of(Mongoid::Association::Referenced::BelongsTo)
|
25
|
+
else
|
26
|
+
expect(a.macro).to eq(:belongs_to)
|
27
|
+
end
|
20
28
|
expect(a.class_name).to eq('Node')
|
21
29
|
expect(a.inverse_of).to eq(:children)
|
22
30
|
end
|
@@ -320,9 +328,9 @@ describe Mongoid::Tree do
|
|
320
328
|
- subsubchild
|
321
329
|
ENDTREE
|
322
330
|
|
323
|
-
filtered_ancestors = node(:subsubchild).ancestors.
|
324
|
-
|
325
|
-
|
331
|
+
filtered_ancestors = node(:subsubchild).ancestors.any_of(
|
332
|
+
{ :name => 'child' },
|
333
|
+
{ :name => 'subchild' }
|
326
334
|
)
|
327
335
|
|
328
336
|
expect(filtered_ancestors.to_a).to eq([node(:child), node(:subchild)])
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/models/node.rb
CHANGED
metadata
CHANGED
@@ -1,35 +1,49 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benedikt Deicke
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "<"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '7.0'
|
20
17
|
- - ">="
|
21
18
|
- !ruby/object:Gem::Version
|
22
19
|
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '9'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
27
30
|
- - "<"
|
28
31
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
32
|
+
version: '9'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: mongoid-compatibility
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
30
37
|
- - ">="
|
31
38
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: rake
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,16 +76,16 @@ dependencies:
|
|
62
76
|
name: yard
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|
64
78
|
requirements:
|
65
|
-
- - "
|
79
|
+
- - ">="
|
66
80
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
81
|
+
version: 0.9.20
|
68
82
|
type: :development
|
69
83
|
prerelease: false
|
70
84
|
version_requirements: !ruby/object:Gem::Requirement
|
71
85
|
requirements:
|
72
|
-
- - "
|
86
|
+
- - ">="
|
73
87
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
88
|
+
version: 0.9.20
|
75
89
|
description: A tree structure for Mongoid documents using the materialized path pattern
|
76
90
|
email:
|
77
91
|
- benedikt@synatic.net
|
@@ -79,25 +93,27 @@ executables: []
|
|
79
93
|
extensions: []
|
80
94
|
extra_rdoc_files: []
|
81
95
|
files:
|
82
|
-
- ".rspec"
|
83
96
|
- Gemfile
|
84
97
|
- LICENSE
|
85
98
|
- README.md
|
86
99
|
- Rakefile
|
87
100
|
- lib/mongoid/tree.rb
|
101
|
+
- lib/mongoid/tree/counter_caching.rb
|
88
102
|
- lib/mongoid/tree/ordering.rb
|
89
103
|
- lib/mongoid/tree/traversal.rb
|
104
|
+
- spec/mongoid/tree/counter_caching_spec.rb
|
90
105
|
- spec/mongoid/tree/ordering_spec.rb
|
91
106
|
- spec/mongoid/tree/traversal_spec.rb
|
92
107
|
- spec/mongoid/tree_spec.rb
|
93
108
|
- spec/spec_helper.rb
|
109
|
+
- spec/support/logger.rb
|
94
110
|
- spec/support/macros/tree_macros.rb
|
95
111
|
- spec/support/models/node.rb
|
96
112
|
homepage: https://github.com/benedikt/mongoid-tree
|
97
113
|
licenses:
|
98
114
|
- MIT
|
99
115
|
metadata: {}
|
100
|
-
post_install_message:
|
116
|
+
post_install_message:
|
101
117
|
rdoc_options: []
|
102
118
|
require_paths:
|
103
119
|
- lib
|
@@ -112,10 +128,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
128
|
- !ruby/object:Gem::Version
|
113
129
|
version: '0'
|
114
130
|
requirements: []
|
115
|
-
|
116
|
-
|
117
|
-
signing_key:
|
131
|
+
rubygems_version: 3.4.1
|
132
|
+
signing_key:
|
118
133
|
specification_version: 4
|
119
134
|
summary: A tree structure for Mongoid documents
|
120
135
|
test_files: []
|
121
|
-
has_rdoc:
|
data/.rspec
DELETED