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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3afa8338134c4e7b9f441477d71e9424e55ec92d8dd6f357cf5bc4c767408e8d
4
- data.tar.gz: 01b929e254b8bd933c97d4966289a9963c53c698567e39910b518c8417768173
3
+ metadata.gz: f4b4601b1f55aa5a1b0978ca907b20498150c0c8e21007184cf2abdc703c860e
4
+ data.tar.gz: 2ef33bf45124c8cc6008f585f0da6db7a9a74e6362a66ce94fcfe74fdc42093d
5
5
  SHA512:
6
- metadata.gz: 1a0c9cd88df3ee766afdecab5d6e7c0f9e05bbeb99ac95925f2be2c61b83f9582385261c1822ea2a43e4f332747e9c15da1a3f65c3039e12f77fcc94640d716f
7
- data.tar.gz: 7e4dee5772a214d6857f162a8ef08eca473a7b7ab392d1f2e9fc24a1fd66fbf15d02c12e2c2374be85f8f4ff01bf525be69f42c30d6bd4d5bab354f154bc087c
6
+ metadata.gz: 1989430231c2fa54376ebf8e95e899c02f1271e07b039de7a2ac5c7d830bd41b128c1888c652207d533314b2dd8c3cd7b8996bad1741395a9a8b7be4f093c3aa
7
+ data.tar.gz: cea1a292c37da0c86225065254e2ecc5547fb82905ed435c71ede05f4ab746273939c0f4020405e195bef38c257396a6c20ff43357760d40f261f3704d26762b
data/.gitignore CHANGED
@@ -7,6 +7,7 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  /.ruby-version
10
+ /Gemfile.lock
10
11
 
11
12
  # rspec failure tracking
12
13
  .rspec_status
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # ActiveRecord::OrderByIds
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_record/order_by_ids`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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
- fragments = []
8
- fragments += ["#{primary_key} NOT IN (#{ids.map(&:to_s).join(",")})"] if ids.any?
9
- fragments += ids.reverse.map { |id| "#{primary_key}=#{id}" }
10
- reorder(Arel.sql(fragments.join(", ")))
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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module OrderByIds
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  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.1.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-02-11 00:00:00.000000000 Z
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
@@ -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