active_record-order_by_ids 0.1.0 → 0.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 +4 -4
- data/.gitignore +1 -0
- data/README.md +20 -4
- data/lib/active_record/order_by_ids.rb +10 -4
- data/lib/active_record/order_by_ids/version.rb +1 -1
- metadata +2 -3
- data/Gemfile.lock +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4b4601b1f55aa5a1b0978ca907b20498150c0c8e21007184cf2abdc703c860e
|
4
|
+
data.tar.gz: 2ef33bf45124c8cc6008f585f0da6db7a9a74e6362a66ce94fcfe74fdc42093d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1989430231c2fa54376ebf8e95e899c02f1271e07b039de7a2ac5c7d830bd41b128c1888c652207d533314b2dd8c3cd7b8996bad1741395a9a8b7be4f093c3aa
|
7
|
+
data.tar.gz: cea1a292c37da0c86225065254e2ecc5547fb82905ed435c71ede05f4ab746273939c0f4020405e195bef38c257396a6c20ff43357760d40f261f3704d26762b
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# ActiveRecord::OrderByIds
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
ActiveRecord scope methods for ordering by an explicit list of values.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,25 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
This gem adds two methods to active record scopes: `.order_by_ids`, and the more general `.order_by`.
|
24
|
+
|
25
|
+
Examples:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
user1 = User.create(id: 1)
|
29
|
+
user2 = User.create(id: 2)
|
30
|
+
user3 = User.create(id: 3)
|
31
|
+
|
32
|
+
User.order_by_ids([2,3,1]) #=> [user2, user3, user1]
|
33
|
+
```
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
user3 = User.create(parent_id: 3)
|
37
|
+
user2 = User.create(parent_id: 2)
|
38
|
+
user1 = User.create(parent_id: 1)
|
39
|
+
|
40
|
+
User.order_by(parent_id: [2,3,1]) #=> [user2, user3, user1]
|
41
|
+
```
|
26
42
|
|
27
43
|
## Development
|
28
44
|
|
@@ -4,10 +4,16 @@ require "active_record"
|
|
4
4
|
module ActiveRecord
|
5
5
|
module OrderByIds
|
6
6
|
def order_by_ids ids
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
order_by(primary_key => ids)
|
8
|
+
end
|
9
|
+
|
10
|
+
def order_by hash
|
11
|
+
hash.reduce(reorder(false)) do |scope, (key, ids)|
|
12
|
+
fragments = []
|
13
|
+
fragments += ["#{key} NOT IN (#{ids.map(&:to_s).join(",")}) OR #{key} IS NULL"] if ids.any?
|
14
|
+
fragments += ids.reverse.map { |id| "#{key}=#{id}" }
|
15
|
+
scope.order!(Arel.sql(fragments.join(", ")))
|
16
|
+
end
|
11
17
|
end
|
12
18
|
end
|
13
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-order_by_ids
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -119,7 +119,6 @@ files:
|
|
119
119
|
- ".rspec"
|
120
120
|
- ".travis.yml"
|
121
121
|
- Gemfile
|
122
|
-
- Gemfile.lock
|
123
122
|
- LICENSE.txt
|
124
123
|
- README.md
|
125
124
|
- Rakefile
|
data/Gemfile.lock
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
active_record-order_by_ids (0.1.0)
|
5
|
-
activerecord
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
activemodel (6.0.2.1)
|
11
|
-
activesupport (= 6.0.2.1)
|
12
|
-
activerecord (6.0.2.1)
|
13
|
-
activemodel (= 6.0.2.1)
|
14
|
-
activesupport (= 6.0.2.1)
|
15
|
-
activesupport (6.0.2.1)
|
16
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
-
i18n (>= 0.7, < 2)
|
18
|
-
minitest (~> 5.1)
|
19
|
-
tzinfo (~> 1.1)
|
20
|
-
zeitwerk (~> 2.2)
|
21
|
-
appraisal (2.2.0)
|
22
|
-
bundler
|
23
|
-
rake
|
24
|
-
thor (>= 0.14.0)
|
25
|
-
byebug (11.1.1)
|
26
|
-
concurrent-ruby (1.1.6)
|
27
|
-
diff-lcs (1.3)
|
28
|
-
i18n (1.8.2)
|
29
|
-
concurrent-ruby (~> 1.0)
|
30
|
-
minitest (5.14.0)
|
31
|
-
rake (10.5.0)
|
32
|
-
rspec (3.9.0)
|
33
|
-
rspec-core (~> 3.9.0)
|
34
|
-
rspec-expectations (~> 3.9.0)
|
35
|
-
rspec-mocks (~> 3.9.0)
|
36
|
-
rspec-core (3.9.1)
|
37
|
-
rspec-support (~> 3.9.1)
|
38
|
-
rspec-expectations (3.9.0)
|
39
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
-
rspec-support (~> 3.9.0)
|
41
|
-
rspec-mocks (3.9.1)
|
42
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
-
rspec-support (~> 3.9.0)
|
44
|
-
rspec-support (3.9.2)
|
45
|
-
sqlite3 (1.4.2)
|
46
|
-
thor (1.0.1)
|
47
|
-
thread_safe (0.3.6)
|
48
|
-
tzinfo (1.2.6)
|
49
|
-
thread_safe (~> 0.1)
|
50
|
-
zeitwerk (2.2.2)
|
51
|
-
|
52
|
-
PLATFORMS
|
53
|
-
ruby
|
54
|
-
|
55
|
-
DEPENDENCIES
|
56
|
-
active_record-order_by_ids!
|
57
|
-
appraisal
|
58
|
-
bundler (~> 2.0)
|
59
|
-
byebug
|
60
|
-
rake (~> 10.0)
|
61
|
-
rspec (~> 3.0)
|
62
|
-
sqlite3
|
63
|
-
|
64
|
-
BUNDLED WITH
|
65
|
-
2.1.4
|