mongoid_orderable 6.0.6 → 6.0.8
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 +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +66 -67
- data/Rakefile +22 -3
- data/lib/mongoid/orderable/handlers/base.rb +3 -0
- data/lib/mongoid/orderable/mixins/helpers.rb +7 -1
- data/lib/mongoid/orderable/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 426b10ca2d6e73ff7502ef65ec4c4370d58fc82e695f3b6d12a9468185b54ab7
|
|
4
|
+
data.tar.gz: 0def8544900272000caf8a1e7c99aee9805a0963a885b4940e5c94a1164d2460
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 541b75725c985f24d4ac168306a2f4f8fd2e295a3e471860df2295500414cfbb1441da94f794e5544fc11e4342cdc3ed6afa1cc7080e4575cad89f7da0742695
|
|
7
|
+
data.tar.gz: 22ad56900543a8b8b83381e265359e99bd75c276c66340ad53640eaaad63df080f4a83ca797b3cc562476a5d181887cbfc2daa77935b4f7a95ac180d5a2f86ff
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
### 6.0.8 (2026/11/24)
|
|
2
|
+
|
|
3
|
+
* [#94](https://github.com/mongoid/mongoid_orderable/pull/94): Lint Markdown, YAML, and spelling with rubocop-md, rubocop-prose, rubocop-spellcheck, and rubocop-yaml in a separate Lint workflow, and clean up documentation - [@dblock](https://github.com/dblock).
|
|
4
|
+
* [#95](https://github.com/mongoid/mongoid_orderable/pull/95): Run CI pushes only on the default branch, cancel superseded pull request runs, skip duplicate lint in the test matrix, and bound job times - [@dblock](https://github.com/dblock).
|
|
5
|
+
* [#96](https://github.com/mongoid/mongoid_orderable/pull/96): Release with ruby-gem-release from a manually dispatched GitHub Actions workflow using RubyGems Trusted Publishing, with dry runs - [@dblock](https://github.com/dblock).
|
|
6
|
+
|
|
7
|
+
### 6.0.7 (2026/10/18)
|
|
8
|
+
|
|
9
|
+
* [#92](https://github.com/mongoid/mongoid_orderable/pull/92): Fix direct position assignment for embedded documents while retaining the movement API, based on regression tests by [@johnnyshields](https://github.com/johnnyshields) - [@dblock](https://github.com/dblock).
|
|
10
|
+
|
|
1
11
|
### 6.0.6 (2026/10/14)
|
|
2
12
|
|
|
3
13
|
* [#90](https://github.com/mongoid/mongoid_orderable/pull/90): Explicitly load Logger for modern ActiveSupport compatibility.
|
data/README.md
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
[](https://badge.fury.io/rb/mongoid_orderable)
|
|
2
|
-
[](https://github.com/mongoid/mongoid_orderable/actions/workflows/test.yml?query=branch%3Amaster)
|
|
3
|
-
[](https://coveralls.io/github/mongoid/mongoid_orderable?branch=master)
|
|
4
|
-
|
|
5
1
|
# Mongoid Orderable
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
[](https://badge.fury.io/rb/mongoid_orderable) [](https://github.com/mongoid/mongoid_orderable/actions/workflows/test.yml?query=branch%3Amaster) [](https://coveralls.io/github/mongoid/mongoid_orderable?branch=master)
|
|
8
4
|
|
|
9
|
-
Mongoid::Orderable is
|
|
5
|
+
Mongoid::Orderable is an ordered list implementation for your Mongoid 7+ projects.
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
## Core Features
|
|
12
8
|
|
|
13
9
|
* Sets a position index field on your documents which allows you to sort them in order.
|
|
14
10
|
* Uses MongoDB's `$inc` operator to batch-update position.
|
|
@@ -28,18 +24,25 @@ For older versions, please use Mongoid::Orderable 5.x and earlier.
|
|
|
28
24
|
|
|
29
25
|
Transaction support requires MongoDB 4.2+ (4.4+ recommended.)
|
|
30
26
|
|
|
31
|
-
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
Add the gem to your Gemfile and run `bundle install`.
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
gem 'mongoid_orderable'
|
|
33
|
+
```
|
|
32
34
|
|
|
33
35
|
## Usage
|
|
34
36
|
|
|
37
|
+
See [UPGRADING](UPGRADING.md) for migration notes, including embedded position assignment changes in 6.0.7.
|
|
38
|
+
|
|
35
39
|
### Getting Started
|
|
36
40
|
|
|
37
41
|
```ruby
|
|
38
42
|
gem 'mongoid_orderable'
|
|
39
43
|
```
|
|
40
44
|
|
|
41
|
-
Include `Mongoid::Orderable` into your model and call `orderable` method.
|
|
42
|
-
Embedded objects are automatically scoped to their parent.
|
|
45
|
+
Include `Mongoid::Orderable` into your model and call `orderable` method. Embedded objects are automatically scoped to their parent.
|
|
43
46
|
|
|
44
47
|
```ruby
|
|
45
48
|
class MyModel
|
|
@@ -47,8 +50,8 @@ class MyModel
|
|
|
47
50
|
include Mongoid::Orderable
|
|
48
51
|
|
|
49
52
|
belongs_to :group
|
|
50
|
-
belongs_to :drawer, class_name:
|
|
51
|
-
|
|
53
|
+
belongs_to :drawer, class_name: 'Office::Drawer',
|
|
54
|
+
foreign_key: 'legacy_drawer_key_id'
|
|
52
55
|
|
|
53
56
|
orderable
|
|
54
57
|
|
|
@@ -62,15 +65,14 @@ class MyModel
|
|
|
62
65
|
orderable scope: ->(doc) { where(group_id: doc.group_id) }
|
|
63
66
|
|
|
64
67
|
# this one if you want specify indexes manually
|
|
65
|
-
orderable index: false
|
|
68
|
+
orderable index: false
|
|
66
69
|
|
|
67
70
|
# count position from zero as the top-most value (1 is the default value)
|
|
68
71
|
orderable base: 0
|
|
69
72
|
end
|
|
70
73
|
```
|
|
71
74
|
|
|
72
|
-
You can also set default config values in an initializer, which will be
|
|
73
|
-
applied when calling the `orderable` macro in a model.
|
|
75
|
+
You can also set default config values in an initializer, which will be applied when calling the `orderable` macro in a model.
|
|
74
76
|
|
|
75
77
|
```ruby
|
|
76
78
|
# configs/initializers/mongoid_orderable.rb
|
|
@@ -121,43 +123,41 @@ class Book
|
|
|
121
123
|
end
|
|
122
124
|
```
|
|
123
125
|
|
|
124
|
-
The above defines two different orderable_fields on Book - *position* and *serial_no*.
|
|
125
|
-
The following helpers are generated in this case:
|
|
126
|
+
The above defines two different orderable_fields on Book - *position* and *serial_no*. The following helpers are generated in this case:
|
|
126
127
|
|
|
127
128
|
```ruby
|
|
128
|
-
book.move_#{field}_to
|
|
129
|
-
book.move_#{field}_to=
|
|
130
|
-
book.move_#{field}_to!
|
|
129
|
+
book.move_ #{field}_to
|
|
130
|
+
book.move_ #{field}_to=
|
|
131
|
+
book.move_ #{field}_to!
|
|
131
132
|
|
|
132
|
-
book.move_#{field}_to_top
|
|
133
|
-
book.move_#{field}_to_bottom
|
|
134
|
-
book.move_#{field}_higher
|
|
135
|
-
book.move_#{field}_lower
|
|
133
|
+
book.move_ #{field}_to_top
|
|
134
|
+
book.move_ #{field}_to_bottom
|
|
135
|
+
book.move_ #{field}_higher
|
|
136
|
+
book.move_ #{field}_lower
|
|
136
137
|
|
|
137
|
-
book.next_#{field}_items
|
|
138
|
-
book.previous_#{field}_items
|
|
138
|
+
book.next_ #{field}_items
|
|
139
|
+
book.previous_ #{field}_items
|
|
139
140
|
|
|
140
|
-
book.next_#{field}_item
|
|
141
|
-
book.previous_#{field}_item
|
|
141
|
+
book.next_ #{field}_item
|
|
142
|
+
book.previous_ #{field}_item
|
|
142
143
|
```
|
|
143
144
|
|
|
144
145
|
where `#{field}` is either `position` or `serial_no`.
|
|
145
146
|
|
|
146
|
-
When a model defines multiple orderable fields, the original helpers are also available and work
|
|
147
|
-
on the first orderable field.
|
|
147
|
+
When a model defines multiple orderable fields, the original helpers are also available and work on the first orderable field.
|
|
148
148
|
|
|
149
149
|
```ruby
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
150
|
+
@book1 = Book.create!
|
|
151
|
+
@book2 = Book.create!
|
|
152
|
+
@book2 # => <Book _id: 53a16a2ba1bde4f746000001, serial_no: 1, position: 1>
|
|
153
|
+
@book2.move_to! :top # this will change the :position of the book to 0 (not serial_no)
|
|
154
|
+
@book2 # => <Book _id: 53a16a2ba1bde4f746000001, serial_no: 1, position: 0>
|
|
155
155
|
```
|
|
156
156
|
|
|
157
157
|
To specify any other orderable field as default pass the **default: true** option with orderable.
|
|
158
158
|
|
|
159
159
|
```ruby
|
|
160
|
-
|
|
160
|
+
orderable field: sno, as: :serial_no, default: true
|
|
161
161
|
```
|
|
162
162
|
|
|
163
163
|
### Embedded Documents
|
|
@@ -173,8 +173,7 @@ class Question
|
|
|
173
173
|
end
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
-
If you bulk import embedded documents without specifying their position,
|
|
177
|
-
no field `position` will be written.
|
|
176
|
+
If you bulk import embedded documents without specifying their position, no field `position` will be written.
|
|
178
177
|
|
|
179
178
|
```ruby
|
|
180
179
|
class Survey
|
|
@@ -184,15 +183,27 @@ class Survey
|
|
|
184
183
|
end
|
|
185
184
|
```
|
|
186
185
|
|
|
187
|
-
To ensure the position is written correctly, you will need to set
|
|
188
|
-
|
|
186
|
+
To ensure the position is written correctly, you will need to set `cascade_callbacks: true` on the relation.
|
|
187
|
+
|
|
188
|
+
Assigning an embedded document's position reorders its siblings when either the child or its parent is saved:
|
|
189
|
+
|
|
190
|
+
```ruby
|
|
191
|
+
survey = Survey.create!
|
|
192
|
+
3.times { survey.questions.create! }
|
|
193
|
+
question = survey.questions.last
|
|
194
|
+
|
|
195
|
+
question.position = 1
|
|
196
|
+
survey.save! # question.save! also works
|
|
197
|
+
survey.reload.questions.map(&:position) # => [2, 3, 1]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Positions below the top or above the bottom are clamped to the list bounds. Assigning `nil` moves an existing child to the bottom. New children with a position are inserted at that position, shifting later siblings. Existing `move_to`, `move_to!`, and their helper methods remain supported; an explicit movement request takes precedence over a direct position assignment.
|
|
201
|
+
|
|
202
|
+
This changes the previous behavior where assigning an existing embedded child's position could leave duplicate positions instead of shifting its siblings. See [UPGRADING](UPGRADING.md).
|
|
189
203
|
|
|
190
204
|
### Disable Ordering
|
|
191
205
|
|
|
192
|
-
You can disable position tracking for specific documents using the `:if` and `:unless` options.
|
|
193
|
-
This is in advanced scenarios where you want to control position manually for certain documents.
|
|
194
|
-
In general, the disable condition should match a specific scope.
|
|
195
|
-
**Warning:** If used improperly, this will cause your documents to become out-of-order.
|
|
206
|
+
You can disable position tracking for specific documents using the `:if` and `:unless` options. This is in advanced scenarios where you want to control position manually for certain documents. In general, the disable condition should match a specific scope. **Warning:** If used improperly, this will cause your documents to become out-of-order.
|
|
196
207
|
|
|
197
208
|
```ruby
|
|
198
209
|
class Book
|
|
@@ -200,19 +211,14 @@ class Book
|
|
|
200
211
|
include Mongoid::Orderable
|
|
201
212
|
|
|
202
213
|
field :track_position, type: Boolean
|
|
203
|
-
|
|
214
|
+
|
|
204
215
|
orderable if: :track_position, unless: -> { created_at < Date.parse('2020-01-01') }
|
|
205
216
|
end
|
|
206
217
|
```
|
|
207
218
|
|
|
208
219
|
## Transaction Support
|
|
209
220
|
|
|
210
|
-
By default, Mongoid Orderable does not guarantee ordering consistency
|
|
211
|
-
when doing multiple concurrent updates on documents. This means that
|
|
212
|
-
instead of having positions `1, 2, 3, 4, 5`, after running your system
|
|
213
|
-
in production at scale your position data will become corrupted, e.g.
|
|
214
|
-
`1, 1, 4, 4, 6`. To remedy this, this Mongoid Orderable can use
|
|
215
|
-
[MongoDB transactions](https://docs.mongodb.com/manual/core/transactions/)
|
|
221
|
+
By default, Mongoid Orderable does not guarantee ordering consistency when doing multiple concurrent updates on documents. This means that instead of having positions `1, 2, 3, 4, 5`, after running your system in production at scale your position data will become corrupted, e.g. `1, 1, 4, 4, 6`. To remedy this, this Mongoid Orderable can use [MongoDB transactions](https://docs.mongodb.com/manual/core/transactions/)
|
|
216
222
|
|
|
217
223
|
### Prerequisites
|
|
218
224
|
|
|
@@ -234,34 +240,27 @@ class MyModel
|
|
|
234
240
|
end
|
|
235
241
|
```
|
|
236
242
|
|
|
237
|
-
When two transactions are attempted at the same time, database-level
|
|
238
|
-
`WriteConflict` failures may result and retries will be attempted.
|
|
239
|
-
After `transaction_max_retries` has been exceeded, a
|
|
240
|
-
`Mongoid::Orderable::Errors::TransactionFailed` error will be raised.
|
|
243
|
+
When two transactions are attempted at the same time, database-level `WriteConflict` failures may result and retries will be attempted. After `transaction_max_retries` has been exceeded, a `Mongoid::Orderable::Errors::TransactionFailed` error will be raised.
|
|
241
244
|
|
|
242
245
|
### Locks
|
|
243
246
|
|
|
244
|
-
When using transactions, Mongoid Orderable creates a collection
|
|
245
|
-
`mongoid_orderable_locks` which is used to store temporary lock objects.
|
|
246
|
-
Lock collections use a TTL index which auto-deletes objects older than 1 day.
|
|
247
|
+
When using transactions, Mongoid Orderable creates a collection `mongoid_orderable_locks` which is used to store temporary lock objects. Lock collections use a TTL index which auto-deletes objects older than 1 day.
|
|
247
248
|
|
|
248
249
|
You can change the lock collection name globally or per model:
|
|
249
250
|
|
|
250
251
|
```ruby
|
|
251
252
|
Mongoid::Orderable.configure do |config|
|
|
252
|
-
config.lock_collection =
|
|
253
|
+
config.lock_collection = 'my_locks' # default: "mongoid_orderable_locks"
|
|
253
254
|
end
|
|
254
255
|
|
|
255
256
|
class MyModel
|
|
256
|
-
orderable :position, lock_collection:
|
|
257
|
+
orderable :position, lock_collection: 'my_model_locks'
|
|
257
258
|
end
|
|
258
259
|
```
|
|
259
260
|
|
|
260
261
|
### MongoDB 4.2 Support
|
|
261
262
|
|
|
262
|
-
In MongoDB 4.2, collections cannot be created within transactions.
|
|
263
|
-
Therefore, you will need to manually run the following command once
|
|
264
|
-
to initialize the lock collection:
|
|
263
|
+
In MongoDB 4.2, collections cannot be created within transactions. Therefore, you will need to manually run the following command once to initialize the lock collection:
|
|
265
264
|
|
|
266
265
|
```ruby
|
|
267
266
|
Mongoid::Orderable::Models::Lock.create!
|
|
@@ -269,12 +268,12 @@ Mongoid::Orderable::Models::Lock.create!
|
|
|
269
268
|
|
|
270
269
|
This step is not necessary when using MongoDB 4.4+.
|
|
271
270
|
|
|
272
|
-
|
|
271
|
+
## Contributing
|
|
273
272
|
|
|
274
|
-
|
|
273
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md), [CHANGELOG.md](CHANGELOG.md), [UPGRADING.md](UPGRADING.md), and [RELEASING.md](RELEASING.md).
|
|
275
274
|
|
|
276
|
-
|
|
275
|
+
## Copyright & License
|
|
277
276
|
|
|
278
277
|
Copyright (c) 2011 Arkadiy Zabazhanov, Johnny Shields, and contributors.
|
|
279
278
|
|
|
280
|
-
MIT license, see [LICENSE](LICENSE.txt) for details.
|
|
279
|
+
MIT license, see [LICENSE.txt](LICENSE.txt) for details.
|
data/Rakefile
CHANGED
|
@@ -12,13 +12,32 @@ end
|
|
|
12
12
|
|
|
13
13
|
Bundler::GemHelper.install_tasks
|
|
14
14
|
|
|
15
|
+
if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
|
|
16
|
+
Gem.loaded_specs.key?('ruby-gem-release')
|
|
17
|
+
require_relative 'script/release'
|
|
18
|
+
MongoidOrderableRelease.workflow.install_tasks
|
|
19
|
+
|
|
20
|
+
%w[release release:rubygem_push release:source_control_push].each do |name|
|
|
21
|
+
Rake::Task[name].clear
|
|
22
|
+
desc 'Disabled; releases run through GitHub Actions, see RELEASING.md'
|
|
23
|
+
task(name) { abort "#{name} is disabled; releases run through GitHub Actions, see RELEASING.md" }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
15
27
|
require 'rspec/core'
|
|
16
28
|
require 'rspec/core/rake_task'
|
|
17
29
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
18
30
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
|
19
31
|
end
|
|
20
32
|
|
|
21
|
-
|
|
22
|
-
|
|
33
|
+
lint = RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
|
|
34
|
+
Gem.loaded_specs.key?('rubocop')
|
|
35
|
+
if lint
|
|
36
|
+
require 'rubocop/rake_task'
|
|
37
|
+
RuboCop::RakeTask.new(:rubocop)
|
|
38
|
+
|
|
39
|
+
desc 'Lint Ruby, Markdown, YAML, and spelling'
|
|
40
|
+
task lint: :rubocop
|
|
41
|
+
end
|
|
23
42
|
|
|
24
|
-
task default: %i[rubocop spec]
|
|
43
|
+
task default: lint ? %i[rubocop spec] : %i[spec]
|
|
@@ -60,6 +60,7 @@ module Handlers
|
|
|
60
60
|
set_lock(field) if use_transactions
|
|
61
61
|
|
|
62
62
|
f = orderable_field(field)
|
|
63
|
+
target_position ||= orderable_position(field) || :bottom if embedded? && doc.send("#{f}_changed?")
|
|
63
64
|
scope = orderable_scope(field)
|
|
64
65
|
scope_changed = orderable_scope_changed?(field)
|
|
65
66
|
|
|
@@ -74,6 +75,8 @@ module Handlers
|
|
|
74
75
|
nil
|
|
75
76
|
elsif persisted? && !embedded?
|
|
76
77
|
scope.where(_id: _id).pluck(f).first
|
|
78
|
+
elsif persisted? && embedded?
|
|
79
|
+
doc.send("#{f}_was")
|
|
77
80
|
else
|
|
78
81
|
orderable_position(field)
|
|
79
82
|
end
|
|
@@ -29,7 +29,13 @@ module Mixins
|
|
|
29
29
|
def orderable_bottom(field = nil, in_list = true)
|
|
30
30
|
field ||= default_orderable_field
|
|
31
31
|
f = orderable_field(field)
|
|
32
|
-
|
|
32
|
+
scope = orderable_scope(field)
|
|
33
|
+
scope = scope.ne(_id: _id) if embedded?
|
|
34
|
+
max = scope.ne(f => nil).max(f)
|
|
35
|
+
if embedded? && in_list && persisted?
|
|
36
|
+
previous_position = send("#{f}_was")
|
|
37
|
+
max = [max, previous_position].compact.max
|
|
38
|
+
end
|
|
33
39
|
return orderable_top(field) unless max
|
|
34
40
|
in_list ? max : max.next
|
|
35
41
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mongoid_orderable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 6.0.
|
|
4
|
+
version: 6.0.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- pyromaniac
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: mongoid
|
|
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
82
|
version: '0'
|
|
83
83
|
requirements: []
|
|
84
|
-
rubygems_version: 3.5.
|
|
84
|
+
rubygems_version: 3.5.22
|
|
85
85
|
signing_key:
|
|
86
86
|
specification_version: 4
|
|
87
87
|
summary: Mongoid orderable list implementation
|