oort 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -2
- data/README.md +30 -5
- data/Rakefile +2 -0
- data/lib/oort/callbacks.rb +13 -7
- data/lib/oort/inserts.rb +14 -5
- data/lib/oort/ordered.rb +6 -8
- data/lib/oort/scopes.rb +9 -19
- data/lib/oort/version.rb +1 -1
- data/oort-0.1.2.gem +0 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93d4111c81efb6f7e4f31bca10cfbef5c96a89ff1513db010fc6e31c45905552
|
4
|
+
data.tar.gz: d2fd5e70bb3ae83b72524fbb768551d8319db308aa9e0c124735804f29477421
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b81fea732d5131d90cc061fe21536edbdc2a1d35022b90feccd00c90fefb818e0ec4d29487202378c03a4a919bc1aef1e67165774b2e459d38bdf261ed15716
|
7
|
+
data.tar.gz: da75ab4163fe2b5ddb37099659ae0c38a248bdd44e2c8bcf627ad5e17ce6d634813c02072bafe876797b0b2a9973797a88f92ba8cf9d230a910462af9327abfe
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
oort (0.
|
4
|
+
oort (0.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -69,6 +69,7 @@ GEM
|
|
69
69
|
|
70
70
|
PLATFORMS
|
71
71
|
x86_64-darwin-20
|
72
|
+
x86_64-darwin-22
|
72
73
|
|
73
74
|
DEPENDENCIES
|
74
75
|
activerecord (~> 7.0, >= 7.0.1)
|
@@ -81,4 +82,4 @@ DEPENDENCIES
|
|
81
82
|
zeitwerk (~> 2.5)
|
82
83
|
|
83
84
|
BUNDLED WITH
|
84
|
-
2.
|
85
|
+
2.5.3
|
data/README.md
CHANGED
@@ -17,7 +17,7 @@ Let's begin with a basic schema involving a User and a Post. You can substitute
|
|
17
17
|
|
18
18
|
Firstly, you will also need the following migration to users (postgresql only for now):
|
19
19
|
|
20
|
-
```
|
20
|
+
```RUBY
|
21
21
|
def change
|
22
22
|
add_column(:users, :posts_ordering, :integer, array: true, default: [], using: 'ARRAY[benefit_type]::INTEGER[]')
|
23
23
|
|
@@ -31,7 +31,7 @@ This will store the ids of posts in an array on the user, and will only accept i
|
|
31
31
|
|
32
32
|
Include the `Oort::Ordered` module in the parent object:
|
33
33
|
|
34
|
-
```
|
34
|
+
```RUBY
|
35
35
|
class User < ActiveRecord::Base
|
36
36
|
include Oort::Ordered
|
37
37
|
handles_ordering_of :posts
|
@@ -52,7 +52,7 @@ This inclusion adds `update_posts_ordering` to the user model and `insert_at` to
|
|
52
52
|
|
53
53
|
The following callbacks are also added to post:
|
54
54
|
|
55
|
-
```
|
55
|
+
```RUBY
|
56
56
|
after_create_commit :insert_at
|
57
57
|
after_destroy :remove_from_reorderable
|
58
58
|
```
|
@@ -63,10 +63,20 @@ These callbacks automatically insert a new post at the first position and remove
|
|
63
63
|
To change to order of a post, simply call `post.insert_at(12)`
|
64
64
|
To remove a post, simply call `post.remove_from_reorderable`
|
65
65
|
|
66
|
-
###
|
67
|
-
|
66
|
+
### Scopes
|
67
|
+
You can add the `ordered_with` scope.
|
68
|
+
|
69
|
+
```RUBY
|
70
|
+
class Post < ActiveRecord::Base
|
71
|
+
include Oort::Scopes
|
68
72
|
|
73
|
+
belongs_to :user
|
74
|
+
end
|
69
75
|
```
|
76
|
+
|
77
|
+
This allows a `user` object to have the following query:
|
78
|
+
|
79
|
+
```RUBY
|
70
80
|
user.posts.ordered_with(user.posts_ordering)
|
71
81
|
|
72
82
|
# or
|
@@ -74,6 +84,21 @@ user.posts.ordered_with(user.posts_ordering)
|
|
74
84
|
Post.where(user_id: user.id).ordered_with(user.posts_ordering)
|
75
85
|
```
|
76
86
|
|
87
|
+
### Customization
|
88
|
+
|
89
|
+
`handles_ordering_of` can allow for new records to be the top of the order or the bottom of the order. By default it will push any new records to the top of the order, but you can specify the bottom of the order like so:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
|
93
|
+
class User < ActiveRecord::Base
|
94
|
+
include Oort::Ordered
|
95
|
+
handles_ordering_of :posts, default: :bottom
|
96
|
+
|
97
|
+
has_many :posts
|
98
|
+
end
|
99
|
+
|
100
|
+
```
|
101
|
+
|
77
102
|
|
78
103
|
## Installation
|
79
104
|
|
data/Rakefile
CHANGED
data/lib/oort/callbacks.rb
CHANGED
@@ -2,22 +2,24 @@
|
|
2
2
|
|
3
3
|
module Oort
|
4
4
|
class Callbacks
|
5
|
-
def self.call(association_class:, remove_from_method_name:, insert_method_name:, instance_name:)
|
5
|
+
def self.call(association_class:, remove_from_method_name:, insert_method_name:, instance_name:, default:)
|
6
6
|
new(
|
7
7
|
association_class: association_class,
|
8
8
|
remove_from_method_name: remove_from_method_name,
|
9
9
|
insert_method_name: insert_method_name,
|
10
|
-
instance_name: instance_name
|
10
|
+
instance_name: instance_name,
|
11
|
+
default: default
|
11
12
|
).call
|
12
13
|
end
|
13
14
|
|
14
|
-
attr_reader :association_class, :remove_from_method_name, :insert_method_name, :instance_name
|
15
|
+
attr_reader :association_class, :remove_from_method_name, :insert_method_name, :instance_name, :default
|
15
16
|
|
16
|
-
def initialize(association_class:, remove_from_method_name:, insert_method_name:, instance_name:)
|
17
|
+
def initialize(association_class:, remove_from_method_name:, insert_method_name:, instance_name:, default:)
|
17
18
|
@association_class = association_class
|
18
19
|
@remove_from_method_name = remove_from_method_name
|
19
20
|
@insert_method_name = insert_method_name
|
20
21
|
@instance_name = instance_name
|
22
|
+
@default = default
|
21
23
|
end
|
22
24
|
|
23
25
|
def call
|
@@ -29,7 +31,7 @@ module Oort
|
|
29
31
|
|
30
32
|
def add_callbacks
|
31
33
|
association_class.class_eval do
|
32
|
-
after_create_commit :
|
34
|
+
after_create_commit :initial_insert_at
|
33
35
|
after_destroy :remove_from_reorderable
|
34
36
|
end
|
35
37
|
end
|
@@ -45,9 +47,13 @@ module Oort
|
|
45
47
|
# public_send(:user).public_send(:remove_from_posts_ordering, id)
|
46
48
|
# end
|
47
49
|
<<-RUBY, __FILE__, __LINE__ + 1
|
48
|
-
def insert_at(position = 0)
|
50
|
+
def insert_at(position = 0, initial: nil)
|
49
51
|
public_send(#{instance_name.inspect})
|
50
|
-
.public_send(#{insert_method_name.inspect}, insert: id, at: position)
|
52
|
+
.public_send(#{insert_method_name.inspect}, insert: id, at: position, initial: initial)
|
53
|
+
end
|
54
|
+
|
55
|
+
def initial_insert_at
|
56
|
+
insert_at(initial: #{default.inspect})
|
51
57
|
end
|
52
58
|
|
53
59
|
def remove_from_reorderable
|
data/lib/oort/inserts.rb
CHANGED
@@ -26,13 +26,22 @@ module Oort
|
|
26
26
|
# end
|
27
27
|
# end
|
28
28
|
<<-RUBY, __FILE__, __LINE__ + 1
|
29
|
-
def #{insert_method_name}(insert:, at: 0)
|
29
|
+
def #{insert_method_name}(insert:, at: 0, initial: nil)
|
30
30
|
with_lock do
|
31
31
|
current_values = public_send(#{stored_in.inspect})
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
|
33
|
+
if initial == :top
|
34
|
+
current_values.unshift(insert)
|
35
|
+
save
|
36
|
+
elsif initial == :bottom
|
37
|
+
current_values << insert
|
38
|
+
save
|
39
|
+
else
|
40
|
+
current_index = current_values.find_index(insert)
|
41
|
+
insertable = current_index.blank? ? insert : current_values.delete_at(current_index)
|
42
|
+
current_values.insert(at, insertable)
|
43
|
+
update(#{stored_in.inspect} => current_values)
|
44
|
+
end
|
36
45
|
end
|
37
46
|
end
|
38
47
|
RUBY
|
data/lib/oort/ordered.rb
CHANGED
@@ -7,27 +7,25 @@ module Oort
|
|
7
7
|
module Ordered
|
8
8
|
def self.included(base)
|
9
9
|
base.extend ClassMethods
|
10
|
-
# base.include InstanceMethods
|
11
|
-
|
12
|
-
base.class_eval do
|
13
|
-
end
|
14
10
|
end
|
15
11
|
|
16
12
|
module ClassMethods
|
17
|
-
def handles_ordering_of(association)
|
13
|
+
def handles_ordering_of(association, default: :top)
|
18
14
|
args = {
|
19
15
|
stored_in: :"#{association}_ordering",
|
20
16
|
insert_method_name: :"update_#{association}_ordering",
|
21
17
|
remove_from_method_name: :"remove_from_#{association}_ordering",
|
22
18
|
association_class: association.to_s.classify.constantize,
|
23
19
|
instance_name: :"#{name.downcase}",
|
24
|
-
class_name: name.classify.constantize
|
20
|
+
class_name: name.classify.constantize,
|
21
|
+
default:
|
25
22
|
}
|
26
23
|
|
27
24
|
Inserts.call(**args.slice(:stored_in, :insert_method_name, :class_name))
|
28
25
|
Removes.call(**args.slice(:stored_in, :remove_from_method_name, :class_name))
|
29
|
-
|
30
|
-
|
26
|
+
Callbacks.call(
|
27
|
+
**args.slice(:association_class, :remove_from_method_name, :insert_method_name, :instance_name, :default)
|
28
|
+
)
|
31
29
|
end
|
32
30
|
end
|
33
31
|
end
|
data/lib/oort/scopes.rb
CHANGED
@@ -1,30 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Oort
|
4
|
-
|
5
|
-
def self.
|
6
|
-
new(association_class: association_class).call
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize(association_class:)
|
10
|
-
@association_class = association_class
|
11
|
-
end
|
12
|
-
|
13
|
-
def call
|
4
|
+
module Scopes
|
5
|
+
def self.included(base)
|
14
6
|
# user = User.find(909)
|
15
7
|
# user.posts.ordered_with(user.posts_ordered)
|
16
8
|
# by default this will use posts.id::INTEGER
|
17
9
|
# but you can pass in something else if you have joins and items
|
18
10
|
# stored in another table
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
}
|
27
|
-
end
|
11
|
+
base.scope :ordered_with, lambda { |ids, type = "#{table_name}.id::INTEGER"|
|
12
|
+
if ids.blank?
|
13
|
+
order(:id)
|
14
|
+
else
|
15
|
+
order(Arel.sql("array_position(ARRAY[#{ids.join(", ")}], #{type})"))
|
16
|
+
end
|
17
|
+
}
|
28
18
|
end
|
29
19
|
end
|
30
20
|
end
|
data/lib/oort/version.rb
CHANGED
data/oort-0.1.2.gem
ADDED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tobyond
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01
|
11
|
+
date: 2024-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Rails sorting and ordering without deadlocks
|
14
14
|
email:
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/oort/removes.rb
|
41
41
|
- lib/oort/scopes.rb
|
42
42
|
- lib/oort/version.rb
|
43
|
+
- oort-0.1.2.gem
|
43
44
|
- sig/oort.rbs
|
44
45
|
homepage: https://github.com/tobyond/oort
|
45
46
|
licenses:
|
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
62
|
- !ruby/object:Gem::Version
|
62
63
|
version: '0'
|
63
64
|
requirements: []
|
64
|
-
rubygems_version: 3.
|
65
|
+
rubygems_version: 3.5.3
|
65
66
|
signing_key:
|
66
67
|
specification_version: 4
|
67
68
|
summary: Oort to sort, order
|