oort 0.1.0 → 0.1.1
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/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/README.md +74 -1
- data/lib/oort/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: 59358b08ac7c205469cd32c017b73ab36846c520e6d066e67c577bfd4daaacad
|
4
|
+
data.tar.gz: 1c8ab9a97f89c45b7f1630198b2d810ceb6a877d7adbfd0641c92241e5aca425
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 819c6dce941d799a7497e88fe05bc104391e5bc0ce13e3f4ed0a96e388a1ccb3940f1e46de139a02fc742dc2fc26fa2b9643d5ce30995ae242fb2bf5132c4e44
|
7
|
+
data.tar.gz: 7763c120b0311b667159bf1adc48c464a8b43d3a770ac78af8bc9fcba7e616bbce11eef5564859c149b3ea09656e75e29bc2183f35d085fc6942d78a83b5fc58
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
oort (0.1.
|
4
|
+
oort (0.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -24,6 +24,7 @@ GEM
|
|
24
24
|
tzinfo (~> 2.0)
|
25
25
|
ast (2.4.2)
|
26
26
|
base64 (0.2.0)
|
27
|
+
benchmark (0.2.1)
|
27
28
|
bigdecimal (3.1.6)
|
28
29
|
concurrent-ruby (1.2.3)
|
29
30
|
connection_pool (2.4.1)
|
@@ -71,6 +72,7 @@ PLATFORMS
|
|
71
72
|
|
72
73
|
DEPENDENCIES
|
73
74
|
activerecord (~> 7.0, >= 7.0.1)
|
75
|
+
benchmark
|
74
76
|
minitest
|
75
77
|
oort!
|
76
78
|
pg
|
data/README.md
CHANGED
@@ -1,6 +1,79 @@
|
|
1
1
|
# Oort
|
2
2
|
|
3
|
-
|
3
|
+
#### Rails sorting and ordering without deadlocks.
|
4
|
+
|
5
|
+
*Rails and PostgreSQL only (for now).*
|
6
|
+
|
7
|
+
Typically, ordering involves adding a position column to records and rearranging the entire collection when altering the sort order. However, this approach is prone to deadlocks and places a heavy load on the database, especially when modifying multiple records simultaneously.
|
8
|
+
|
9
|
+
Oort provides an alternative solution by allowing the order to be stored in an array column on the parent object. Any changes to the sort order become a simple modification to a single column.
|
10
|
+
|
11
|
+
|
12
|
+
## Instructions
|
13
|
+
|
14
|
+
Let's begin with a basic schema involving a User and a Post. You can substitute these entities as needed; just ensure that handles_ordering_of has a corresponding has_many association. (Replace instances of post with your own association in the following examples.)
|
15
|
+
|
16
|
+
### Migration
|
17
|
+
|
18
|
+
Firstly, you will also need the following migration to users (postgresql only for now):
|
19
|
+
|
20
|
+
```
|
21
|
+
def change
|
22
|
+
add_column(:users, :posts_ordering, :integer, array: true, default: [], using: 'ARRAY[benefit_type]::INTEGER[]')
|
23
|
+
|
24
|
+
add_check_constraint :users, '(array_position(posts_ordering, null) is null)', name: 'posts_ordering'
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
This will store the ids of posts in an array on the user, and will only accept integers to prevent any nasty surprises.
|
29
|
+
|
30
|
+
### Model
|
31
|
+
|
32
|
+
Include the `Oort::Ordered` module in the parent object:
|
33
|
+
|
34
|
+
```
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
include Oort::Ordered
|
37
|
+
handles_ordering_of :posts
|
38
|
+
|
39
|
+
has_many :posts
|
40
|
+
end
|
41
|
+
|
42
|
+
class Post < ActiveRecord::Base
|
43
|
+
belongs_to :user
|
44
|
+
end
|
45
|
+
|
46
|
+
```
|
47
|
+
|
48
|
+
This inclusion adds `update_posts_ordering` to the user model and `insert_at` to the posts model. It also introduces removal methods: `remove_from_posts_ordering` for the user model and `remove_from_reorderable` for the posts model.
|
49
|
+
|
50
|
+
|
51
|
+
### Callbacks
|
52
|
+
|
53
|
+
The following callbacks are also added to post:
|
54
|
+
|
55
|
+
```
|
56
|
+
after_create_commit :insert_at
|
57
|
+
after_destroy :remove_from_reorderable
|
58
|
+
```
|
59
|
+
|
60
|
+
These callbacks automatically insert a new post at the first position and remove a destroyed post from the user's list.
|
61
|
+
|
62
|
+
### Usage
|
63
|
+
To change to order of a post, simply call `post.insert_at(12)`
|
64
|
+
To remove a post, simply call `post.remove_from_reorderable`
|
65
|
+
|
66
|
+
### Scope
|
67
|
+
The `ordered_with` scope is also added to the post model. This allows a `user` object to have the following query:
|
68
|
+
|
69
|
+
```
|
70
|
+
user.posts.ordered_with(user.posts_ordering)
|
71
|
+
|
72
|
+
# or
|
73
|
+
|
74
|
+
Post.where(user_id: user.id).ordered_with(user.posts_ordering)
|
75
|
+
```
|
76
|
+
|
4
77
|
|
5
78
|
## Installation
|
6
79
|
|
data/lib/oort/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tobyond
|
@@ -10,7 +10,7 @@ bindir: exe
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2024-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Rails sorting and ordering without deadlocks
|
14
14
|
email:
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
@@ -41,7 +41,7 @@ files:
|
|
41
41
|
- lib/oort/scopes.rb
|
42
42
|
- lib/oort/version.rb
|
43
43
|
- sig/oort.rbs
|
44
|
-
homepage:
|
44
|
+
homepage: https://github.com/tobyond/oort
|
45
45
|
licenses:
|
46
46
|
- MIT
|
47
47
|
metadata:
|