mongoid_orderable 4.1.1 → 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +77 -17
  3. data/LICENSE.txt +20 -0
  4. data/README.md +256 -127
  5. data/Rakefile +24 -6
  6. data/lib/config/locales/en.yml +12 -9
  7. data/lib/mongoid/orderable.rb +29 -22
  8. data/lib/mongoid/orderable/configs/field_config.rb +79 -0
  9. data/lib/mongoid/orderable/configs/global_config.rb +26 -0
  10. data/lib/mongoid/orderable/engine.rb +206 -0
  11. data/lib/mongoid/orderable/errors/invalid_target_position.rb +19 -18
  12. data/lib/mongoid/orderable/errors/transaction_failed.rb +20 -0
  13. data/lib/mongoid/orderable/generators/base.rb +21 -0
  14. data/lib/mongoid/orderable/generators/helpers.rb +29 -0
  15. data/lib/mongoid/orderable/generators/listable.rb +41 -0
  16. data/lib/mongoid/orderable/generators/lock_collection.rb +37 -0
  17. data/lib/mongoid/orderable/generators/movable.rb +62 -0
  18. data/lib/mongoid/orderable/generators/position.rb +26 -0
  19. data/lib/mongoid/orderable/generators/scope.rb +26 -0
  20. data/lib/mongoid/orderable/installer.rb +63 -0
  21. data/lib/mongoid/orderable/mixins/callbacks.rb +29 -0
  22. data/lib/mongoid/orderable/mixins/helpers.rb +39 -0
  23. data/lib/mongoid/orderable/mixins/listable.rb +49 -0
  24. data/lib/mongoid/orderable/mixins/movable.rb +60 -0
  25. data/lib/mongoid/orderable/version.rb +7 -0
  26. data/lib/mongoid_orderable.rb +29 -56
  27. data/spec/mongoid/orderable_spec.rb +1486 -1408
  28. data/spec/spec_helper.rb +21 -37
  29. metadata +62 -42
  30. data/.gitignore +0 -4
  31. data/.rspec +0 -1
  32. data/.rvmrc +0 -1
  33. data/.travis.yml +0 -20
  34. data/Gemfile +0 -15
  35. data/lib/mongoid/orderable/callbacks.rb +0 -75
  36. data/lib/mongoid/orderable/configuration.rb +0 -60
  37. data/lib/mongoid/orderable/errors.rb +0 -2
  38. data/lib/mongoid/orderable/errors/mongoid_orderable_error.rb +0 -14
  39. data/lib/mongoid/orderable/generator.rb +0 -34
  40. data/lib/mongoid/orderable/generator/helpers.rb +0 -29
  41. data/lib/mongoid/orderable/generator/listable.rb +0 -41
  42. data/lib/mongoid/orderable/generator/movable.rb +0 -62
  43. data/lib/mongoid/orderable/generator/position.rb +0 -26
  44. data/lib/mongoid/orderable/generator/scope.rb +0 -17
  45. data/lib/mongoid/orderable/helpers.rb +0 -50
  46. data/lib/mongoid/orderable/listable.rb +0 -49
  47. data/lib/mongoid/orderable/movable.rb +0 -58
  48. data/lib/mongoid/orderable/orderable_class.rb +0 -51
  49. data/lib/mongoid_orderable/mongoid/contexts/enumerable.rb +0 -15
  50. data/lib/mongoid_orderable/mongoid/contexts/mongo.rb +0 -18
  51. data/lib/mongoid_orderable/mongoid/contextual/memory.rb +0 -15
  52. data/lib/mongoid_orderable/mongoid/criteria.rb +0 -4
  53. data/lib/mongoid_orderable/version.rb +0 -3
  54. data/mongoid_orderable.gemspec +0 -25
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cea70aa8c9e3b86f49203e6f1bb17b45a994e45a
4
- data.tar.gz: 99ad3433535e36e7e4f9f579f1ab4c7640f5552c
2
+ SHA256:
3
+ metadata.gz: 517614edc30dd44ac15d5eafcfcc9103bced1dd7ddd4cb0f3624edfe6adb6af2
4
+ data.tar.gz: 914eff039882840a25b888fe534a5adf3198c762a2944d141d425daff558baf5
5
5
  SHA512:
6
- metadata.gz: 52b38d32f9272161134dd2c4b04d70549e6b8fe7034b0838c12d243e420702889ae36f2b43463c22b0afafc5447817414f0d402d9441b9a6847434c328f505c4
7
- data.tar.gz: 1608265030534f0bb65c9911014cb1e3d6a54691a037644cdc31295aec872c58b4edfc84414cf847e2c35497b2629d87606f6fe48b993c3b2ca34a237d97890b
6
+ metadata.gz: 67ef0079e9c442579b08d6043fd80215e7678f705fea24b4581131b39880d1b0b33cc9fee4d8eb9ec1905e0b8b8ad2b4c79706ff170e0d49ec5c91130b91737a
7
+ data.tar.gz: c7c9befe3900fa468e6149fa0562de121525428b294981c6990d50e862f42912fa8a2b59a09e54e63328975b303af51de7c556a4b9a9632f0bbb9e4ed7d876b4
@@ -1,17 +1,77 @@
1
- # master
2
-
3
- # 4.1.1
4
-
5
- * Fix: index should respect scope (@joeyAghion)
6
-
7
- # 4.1.0
8
-
9
- * Resolving scope foreign key form passed relation name (@dsci)
10
- * Fix: relation metadata quering (@pjkelly)
11
- * `previous_items` and `next_items` methods (@mrjlynch)
12
-
13
- # 4.0.0
14
-
15
- * Semantic versioning
16
- * Added Mongoid 4 support (@dblock)
17
- * Fixes by @johnnyshields and @zhengjia
1
+ ### 6.0.2 (Next)
2
+
3
+ * Your contribution here.
4
+
5
+ ### 6.0.1 (2021/01/26)
6
+
7
+ * [#69](https://github.com/mongoid/mongoid_orderable/pull/69): Fix: Transactions should force read from primary - [@johnnyshields](https://github.com/johnnyshields).
8
+
9
+ ### 6.0.0 (2021/01/23)
10
+
11
+ * [#65](https://github.com/mongoid/mongoid_orderable/pull/65): Feature: Add transaction support with lock table - [@johnnyshields](https://github.com/johnnyshields).
12
+ * [#65](https://github.com/mongoid/mongoid_orderable/pull/65): Feature: Avoid calling position function if neither position nor scope is changing - [@johnnyshields](https://github.com/johnnyshields).
13
+ * [#62](https://github.com/mongoid/mongoid_orderable/pull/62): Feature: Allow scope parameter to be an Array - [@johnnyshields](https://github.com/johnnyshields).
14
+ * [#62](https://github.com/mongoid/mongoid_orderable/pull/62): Feature: Add global configurability - [@johnnyshields](https://github.com/johnnyshields).
15
+ * [#65](https://github.com/mongoid/mongoid_orderable/pull/65): Fix: Do not run embedded destroy callbacks when the root object has been destroyed - [@johnnyshields](https://github.com/johnnyshields).
16
+ * [#62](https://github.com/mongoid/mongoid_orderable/pull/62): Refactor: rename "column" to "field" - [@johnnyshields](https://github.com/johnnyshields).
17
+ * [#60](https://github.com/mongoid/mongoid_orderable/pull/60): Refactor: Drop support for Mongoid 6.x and lower - [@johnnyshields](https://github.com/johnnyshields).
18
+ * [#60](https://github.com/mongoid/mongoid_orderable/pull/60): Refactor: Remove Mongoid Compatibility (no longer needed) - [@johnnyshields](https://github.com/johnnyshields).
19
+
20
+ ### 5.2.0 (2018/05/01)
21
+
22
+ * [#57](https://github.com/mongoid/mongoid_orderable/pull/57): Compatibility with Mongoid 7 - [@tomasc](https://github.com/tomasc).
23
+ * [#52](https://github.com/mongoid/mongoid_orderable/pull/52): Test against Mongoid 6 - [@dblock](https://github.com/dblock).
24
+
25
+ ### 5.1.0 (2017/06/04)
26
+
27
+ * [#50](https://github.com/mongoid/mongoid_orderable/pull/50): Added Danger, PR linter - [@dblock](https://github.com/dblock).
28
+ * [#51](https://github.com/mongoid/mongoid_orderable/pull/51): Added RuboCop - [@dblock](https://github.com/dblock).
29
+ * [#49](https://github.com/mongoid/mongoid_orderable/pull/49): Fix orderable on embedded documents inside an inherited model - [@rafaelgaspar](https://github.com/rafaelgaspar).
30
+
31
+ ### 5.0.0 (2015/10/21)
32
+
33
+ * [#41](https://github.com/mongoid/mongoid_orderable/pull/41): Mongoid 5 support - [@dblock](https://github.com/dblock).
34
+ * [#41](https://github.com/mongoid/mongoid_orderable/pull/41): Dropped support for ruby 1.8 - [@dblock](https://github.com/dblock).
35
+
36
+ ### 4.1.1 (2014/12/19)
37
+
38
+ * [#34](https://github.com/mongoid/mongoid_orderable/pull/34): Fix: index should respect scope - [@joeyAghion](https://github.com/joeyAghion).
39
+
40
+ ### 4.1.0 (2014/3/19)
41
+
42
+ * [#30](https://github.com/mongoid/mongoid_orderable/pull/30): Fix: reset a single field when `orderable_scope` changed instead of removing it from list - [@Bharat311](https://github.com/Bharat311).
43
+ * [#29](https://github.com/mongoid/mongoid_orderable/pull/29): Fix: incorrect position when orderable base is set - [@Bharat311](https://github.com/Bharat311).
44
+ * [#28](https://github.com/mongoid/mongoid_orderable/pull/28): Inherited `orderable_configurations` - [@johnny-miyake](https://github.com/johnny-miyake).
45
+ * [#27](https://github.com/mongoid/mongoid_orderable/pull/27): Allowed numeric string to specify a target position - [@johnny-miyake](https://github.com/johnny-miyake).
46
+ * [#26](https://github.com/mongoid/mongoid_orderable/pull/26): Added support for multiple orderable fields - [@Bharat311](https://github.com/Bharat311).
47
+ * [#22](https://github.com/mongoid/mongoid_orderable/pull/22): Added lazy support for scopes with different foreign key names - [@dsci](https://github.com/dsci).
48
+ * [#21](https://github.com/mongoid/mongoid_orderable/pull/21): Accounting for change in Mongoid's metadata API - [@pjkelly](https://github.com/pjkelly).
49
+ * [#20](https://github.com/mongoid/mongoid_orderable/pull/20): Added next and previous item methods - [@mrjlynch](https://github.com/mrjlynch).
50
+ * [#19](https://github.com/mongoid/mongoid_orderable/pull/19): Added lower and higher item methods - [@mrjlynch](https://github.com/mrjlynch).
51
+
52
+ ### 4.0.0 (2013/10/22)
53
+
54
+ * [#18](https://github.com/mongoid/mongoid_orderable/pull/18): Added Mongoid 4 support - [@dblock](https://github.com/dblock).
55
+ * [#16](https://github.com/mongoid/mongoid_orderable/pull/16): Fix for Mongoid identity map combined with scoped orderable - [@johnnyshields](https://github.com/johnnyshields).
56
+ * [#15](https://github.com/mongoid/mongoid_orderable/pull/15): Support pass-thru of Mongoid field alias (`:as` parameter) - [@johnnyshields](https://github.com/johnnyshields).
57
+ * [#13](https://github.com/mongoid/mongoid_orderable/pull/13): Support inheritance - [@zhengjia](https://github.com/zhengjia).
58
+
59
+ ### 1.2.0 (2013/1/10)
60
+
61
+ * [#12](https://github.com/mongoid/mongoid_orderable/pull/12): Added support for "base" in config options, in order to do a zero-based position - [@johnnyshields](https://github.com/johnnyshields).
62
+
63
+ ### 1.1.0 (2012/5/23)
64
+
65
+ * Added Mongoid 3 support - [@pyromaniac](https://github.com/pyromaniac).
66
+
67
+ ### 1.0.0 (2012/2/14)
68
+
69
+ * [#5](https://github.com/mongoid/mongoid_orderable/pull/5): Allow for use with default scopes - [@mattiassvedhem](https://github.com/mattiassvedhem).
70
+
71
+ ### 0.9.1 (2012/2/3)
72
+
73
+ ### 0.9.0 (2011/11/23)
74
+
75
+ ### 0.0.1 (2011/11/1)
76
+
77
+ * Initial public release - [@pyromaniac](https://github.com/pyromaniac).
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011-2017 Arkadiy Zabazhanov & Contributors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,127 +1,256 @@
1
- [![Build Status](https://secure.travis-ci.org/pyromaniac/mongoid_orderable.png)](http://travis-ci.org/pyromaniac/mongoid_orderable)
2
-
3
- # What?
4
-
5
- Mongoid::Orderable is a ordered list implementation for your mongoid models.
6
-
7
- # Why?
8
-
9
- * It uses native mongo batch increment feature
10
- * It supports mutators api
11
- * It correctly assigns the position while moving document between scopes
12
- * It supports mongoid 2, 3 and 4
13
- * It supports specifying multiple orderable columns
14
-
15
- # How?
16
-
17
- ```
18
- gem 'mongoid_orderable'
19
- ```
20
-
21
- Gem has the same api as others. Just include Mongoid::Orderable into your model and call `orderable` method.
22
- Embedded objects are automatically scoped by their parent.
23
-
24
- ```
25
- class Item
26
- include Mongoid::Document
27
- include Mongoid::Orderable
28
-
29
- # belongs_to :group
30
- # belongs_to :drawer, class_name: "Office::Drawer",
31
- # foreign_key: "legacy_drawer_key_id"
32
-
33
- # orderable
34
- # orderable scope: :group, column: :pos
35
- # orderable scope: :drawer, column: :pos # resolves scope foreign key from relation
36
- # orderable scope: 'drawer', column: :pos # but if you pass a string - it will use it as is, as the column name for scope
37
- # orderable scope: lambda { |document| where(group_id: document.group_id) }
38
- # orderable index: false # this one if you want specify indexes manually
39
- # orderable base: 0 # count position from zero as the top-most value (1 is the default value)
40
- end
41
- ```
42
-
43
- # Usage
44
-
45
- ```
46
- item.move_to 2 # just change position
47
- item.move_to! 2 # and save
48
- item.move_to = 2 # assignable method
49
-
50
- # symbol position
51
- item.move_to :top
52
- item.move_to :bottom
53
- item.move_to :higher
54
- item.move_to :lower
55
-
56
- # generated methods
57
- item.move_to_top
58
- item.move_to_bottom
59
- item.move_higher
60
- item.move_lower
61
-
62
- item.next_items # return a collection of items higher on the list
63
- item.previous_items # return a collection of items lower on the list
64
-
65
- item.next_item # returns the next item in the list
66
- item.previous_item # returns the previous item in the list
67
- ```
68
-
69
- # Multiple Columns
70
-
71
- You can also define multiple orderable columns for any class including the Mongoid::Orderable module.
72
-
73
- ```
74
- class Book
75
- include Mongoid::Document
76
- include Mongoid::Orderable
77
-
78
- orderable base: 0
79
- orderable column: sno, as: :serial_no
80
- end
81
- ```
82
-
83
- The above defines two different orderable_columns on Book - *position* and *serial_no*.
84
- The following helpers are generated in this case:
85
-
86
- ```
87
- item.move_#{column_name}_to
88
- item.move_#{column_name}_to=
89
- item.move_#{column_name}_to!
90
-
91
- item.move_#{column_name}_to_top
92
- item.move_#{column_name}_to_bottom
93
- item.move_#{column_name}_higher
94
- item.move_#{column_name}_lower
95
-
96
- item.next_#{column_name}_items
97
- item.previous_#{column_name}_items
98
-
99
- item.next_#{column_name}_item
100
- item.previous_#{column_name}_item
101
- ```
102
-
103
- *where column_name is either **position** or **serial_no**.*
104
-
105
- When a model defines multiple orderable columns, the original helpers are also available and work on the first orderable column.
106
-
107
- ```
108
- @book1 = Book.create!
109
- @book2 = Book.create!
110
- @book2 => #<Book _id: 53a16a2ba1bde4f746000001, serial_no: 1, position: 1>
111
- @book2.move_to! :top # this will change the :position of the book to 0 (not serial_no)
112
- @book2 => #<Book _id: 53a16a2ba1bde4f746000001, serial_no: 1, position: 0>
113
- ```
114
-
115
- To specify any other orderable column as default pass the **default: true** option with orderable.
116
-
117
- ```
118
- orderable column: sno, as: :serial_no, default: true
119
- ```
120
-
121
- # Contributing
122
-
123
- Fork && Patch && Spec && Push && Pull request.
124
-
125
- # License
126
-
127
- Mongoid::Orderable is released under the MIT license.
1
+ [![Gem Version](https://badge.fury.io/rb/mongoid_orderable.svg)](https://badge.fury.io/rb/mongoid_orderable)
2
+ [![Build Status](https://travis-ci.org/mongoid/mongoid_orderable.svg?branch=master)](https://travis-ci.org/mongoid/mongoid_orderable)
3
+
4
+ # Mongoid Orderable
5
+
6
+ Mongoid::Orderable is a ordered list implementation for your Mongoid 7+ projects.
7
+
8
+ ### Core Features
9
+
10
+ * Sets a position index field on your documents which allows you to sort them in order.
11
+ * Uses MongoDB's `$inc` operator to batch-update position.
12
+ * Supports scope for position index, including changing scopes.
13
+ * Supports multiple position indexes on the same document.
14
+ * (Optional) Uses [MongoDB transactions](https://docs.mongodb.com/manual/core/transactions/) to ensure order integrity during concurrent updates.
15
+
16
+ ### Version Support
17
+
18
+ As of version 6.0.0, Mongoid::Orderable supports the following dependency versions:
19
+
20
+ * Ruby 2.6+
21
+ * Mongoid 7.0+
22
+ * Rails 5.2+
23
+
24
+ For older versions, please use Mongoid::Orderable 5.x and earlier.
25
+
26
+ Transaction support requires MongoDB 4.2+ (4.4+ recommended.)
27
+
28
+ ## Usage
29
+
30
+ ### Getting Started
31
+
32
+ ```ruby
33
+ gem 'mongoid_orderable'
34
+ ```
35
+
36
+ Include `Mongoid::Orderable` into your model and call `orderable` method.
37
+ Embedded objects are automatically scoped to their parent.
38
+
39
+ ```ruby
40
+ class MyModel
41
+ include Mongoid::Document
42
+ include Mongoid::Orderable
43
+
44
+ belongs_to :group
45
+ belongs_to :drawer, class_name: "Office::Drawer",
46
+ foreign_key: "legacy_drawer_key_id"
47
+
48
+ orderable
49
+
50
+ # if you set :scope as a symbol, it will resolve the foreign key from relation
51
+ orderable scope: :drawer, field: :pos
52
+
53
+ # if you set :scope as a string, it will use it as the field name for scope
54
+ orderable scope: 'drawer', field: :pos
55
+
56
+ # scope can also be a proc
57
+ orderable scope: ->(doc) { where(group_id: doc.group_id) }
58
+
59
+ # this one if you want specify indexes manually
60
+ orderable index: false
61
+
62
+ # count position from zero as the top-most value (1 is the default value)
63
+ orderable base: 0
64
+ end
65
+ ```
66
+
67
+ You can also set default config values in an initializer, which will be
68
+ applied when calling the `orderable` macro in a model.
69
+
70
+ ```ruby
71
+ # configs/initializers/mongoid_orderable.rb
72
+ Mongoid::Orderable.configure do |config|
73
+ config.field = :pos
74
+ config.base = 0
75
+ config.index = false
76
+ end
77
+ ```
78
+
79
+ ### Moving Position
80
+
81
+ ```ruby
82
+ item.move_to 2 # just change position
83
+ item.move_to! 2 # and save
84
+ item.move_to = 2 # assignable method
85
+
86
+ # symbol position
87
+ item.move_to :top
88
+ item.move_to :bottom
89
+ item.move_to :higher
90
+ item.move_to :lower
91
+
92
+ # generated methods
93
+ item.move_to_top
94
+ item.move_to_bottom
95
+ item.move_higher
96
+ item.move_lower
97
+
98
+ item.next_items # return a collection of items higher on the list
99
+ item.previous_items # return a collection of items lower on the list
100
+
101
+ item.next_item # returns the next item in the list
102
+ item.previous_item # returns the previous item in the list
103
+ ```
104
+
105
+ ### Multiple Fields
106
+
107
+ You can also define multiple orderable fields for any class including the Mongoid::Orderable module.
108
+
109
+ ```ruby
110
+ class Book
111
+ include Mongoid::Document
112
+ include Mongoid::Orderable
113
+
114
+ orderable base: 0
115
+ orderable field: sno, as: :serial_no
116
+ end
117
+ ```
118
+
119
+ The above defines two different orderable_fields on Book - *position* and *serial_no*.
120
+ The following helpers are generated in this case:
121
+
122
+ ```ruby
123
+ book.move_#{field}_to
124
+ book.move_#{field}_to=
125
+ book.move_#{field}_to!
126
+
127
+ book.move_#{field}_to_top
128
+ book.move_#{field}_to_bottom
129
+ book.move_#{field}_higher
130
+ book.move_#{field}_lower
131
+
132
+ book.next_#{field}_items
133
+ book.previous_#{field}_items
134
+
135
+ book.next_#{field}_item
136
+ book.previous_#{field}_item
137
+ ```
138
+
139
+ where `#{field}` is either `position` or `serial_no`.
140
+
141
+ When a model defines multiple orderable fields, the original helpers are also available and work on the first orderable field.
142
+
143
+ ```ruby
144
+ @book1 = Book.create!
145
+ @book2 = Book.create!
146
+ @book2 # => <Book _id: 53a16a2ba1bde4f746000001, serial_no: 1, position: 1>
147
+ @book2.move_to! :top # this will change the :position of the book to 0 (not serial_no)
148
+ @book2 # => <Book _id: 53a16a2ba1bde4f746000001, serial_no: 1, position: 0>
149
+ ```
150
+
151
+ To specify any other orderable field as default pass the **default: true** option with orderable.
152
+
153
+ ```ruby
154
+ orderable field: sno, as: :serial_no, default: true
155
+ ```
156
+
157
+ ### Embedded Documents
158
+
159
+ ```ruby
160
+ class Question
161
+ include Mongoid::Document
162
+ include Mongoid::Orderable
163
+
164
+ embedded_in :survey
165
+
166
+ orderable
167
+ end
168
+ ```
169
+
170
+ If you bulk import embedded documents without specifying their position,
171
+ no field `position` will be written.
172
+
173
+ ```ruby
174
+ class Survey
175
+ include Mongoid::Document
176
+
177
+ embeds_many :questions, cascade_callbacks: true
178
+ end
179
+ ```
180
+
181
+ To ensure the position is written correctly, you will need to set
182
+ `cascade_callbacks: true` on the relation.
183
+
184
+ ## Transaction Support
185
+
186
+ By default, Mongoid Orderable does not guarantee ordering consistency
187
+ when doing multiple concurrent updates on documents. This means that
188
+ instead of having positions `1, 2, 3, 4, 5`, after running your system
189
+ in production at scale your position data will become corrupted, e.g.
190
+ `1, 1, 4, 4, 6`. To remedy this, this Mongoid Orderable can use
191
+ [MongoDB transactions](https://docs.mongodb.com/manual/core/transactions/)
192
+
193
+ ### Prerequisites
194
+
195
+ * MongoDB version 4.2+ (4.4+ recommended.)
196
+ * Requires [MongoDB Replica Set](https://docs.mongodb.com/manual/tutorial/deploy-replica-set/) topology
197
+
198
+ ### Configuration
199
+
200
+ You may enable transactions on both the global and model configs:
201
+
202
+ ```ruby
203
+ Mongoid::Orderable.configure do |config|
204
+ config.use_transactions = true # default: false
205
+ config.transaction_max_retries = 10 # default: 10
206
+ end
207
+
208
+ class MyModel
209
+ orderable :position, use_transactions: false
210
+ end
211
+ ```
212
+
213
+ When two transactions are attempted at the same time, database-level
214
+ `WriteConflict` failures may result and retries will be attempted.
215
+ After `transaction_max_retries` has been exceeded, a
216
+ `Mongoid::Orderable::Errors::TransactionFailed` error will be raised.
217
+
218
+ ### Locks
219
+
220
+ When using transactions, Mongoid Orderable creates a collection
221
+ `mongoid_orderable_locks` which is used to store temporary lock objects.
222
+ This collection accumulate documents overtime; it is safe to delete it periodically.
223
+
224
+ You can change the lock collection name globally or per model:
225
+
226
+ ```ruby
227
+ Mongoid::Orderable.configure do |config|
228
+ config.lock_collection = "my_locks" # default: "mongoid_orderable_locks"
229
+ end
230
+
231
+ class MyModel
232
+ orderable :position, lock_collection: "my_model_locks"
233
+ end
234
+ ```
235
+
236
+ ### MongoDB 4.2 Support
237
+
238
+ In MongoDB 4.2, collections cannot be created within transactions.
239
+ Therefore, you will need to manually run the following command once
240
+ to initialize the lock collection:
241
+
242
+ ```ruby
243
+ Mongoid::Orderable::Models::Lock.create!
244
+ ```
245
+
246
+ This step is not necessary when using MongoDB 4.4+.
247
+
248
+ ### Contributing
249
+
250
+ Please fork the project on Github and raise a pull request including passing specs.
251
+
252
+ ### Copyright & License
253
+
254
+ Copyright (c) 2011 Arkadiy Zabazhanov, Johnny Shields, and contributors.
255
+
256
+ MIT license, see [LICENSE](LICENSE.txt) for details.