find_with_order 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/find_with_order.gemspec +2 -2
- data/lib/find_with_order/version.rb +1 -1
- data/lib/find_with_order.rb +7 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce3d6c02d373b47ed41758e0b88cee251fb6722a
|
4
|
+
data.tar.gz: a89e7f837e84f7de799c809b4d3474987f0e9a86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 995d774548996433d25663c089a98322e81a78ad4be8ff4c1c8fab74ac8df59f50d42479201a57ce20dfcc764927247d5987695617823ca96473a0c0bcf69028
|
7
|
+
data.tar.gz: d9d904897d1dcdcbe4816e50e908f241ae3910c8f249a0b5e2f9e1a414375de09a6fa858e9806a9905b355355d96be4bd4e0400c9d62cdbaf684f2c2f6211067
|
data/README.md
CHANGED
@@ -28,9 +28,20 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
### Find records in the same order of input IDs
|
30
30
|
```rb
|
31
|
+
User.find([3, 1, 5]).map(&:id)
|
32
|
+
# => [1, 3, 5]
|
33
|
+
|
31
34
|
User.find_with_order([3, 1, 5]).map(&:id)
|
32
35
|
# => [3, 1, 5]
|
33
36
|
```
|
37
|
+
### Support order other columns
|
38
|
+
```rb
|
39
|
+
User.where(name: %w(Pearl John Kathenrie)).pluck(:name)
|
40
|
+
# => ['John', 'Pearl', 'Kathenrie']
|
41
|
+
|
42
|
+
User.where_with_order(:name, %w(Pearl John Kathenrie)).pluck(:name)
|
43
|
+
# => ['Pearl', 'John', 'Kathenrie']
|
44
|
+
```
|
34
45
|
|
35
46
|
## Development
|
36
47
|
|
data/find_with_order.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["khiav reoy"]
|
10
10
|
spec.email = ["mrtmrt15xn@yahoo.com.tw"]
|
11
11
|
|
12
|
-
spec.summary = %q{Find records in the same order of input
|
13
|
-
spec.description = %q{Find records in the same order of input
|
12
|
+
spec.summary = %q{Find records in the same order of input array.}
|
13
|
+
spec.description = %q{Find records in the same order of input array. Supports Rails 3+.}
|
14
14
|
spec.homepage = "https://github.com/khiav223577/find_with_order"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
data/lib/find_with_order.rb
CHANGED
@@ -3,9 +3,14 @@ require 'active_record'
|
|
3
3
|
|
4
4
|
class << ActiveRecord::Base
|
5
5
|
def find_with_order(ids)
|
6
|
+
return none if ids.blank?
|
6
7
|
ids = ids.uniq
|
7
|
-
return
|
8
|
-
|
8
|
+
return where(id: ids).order("field(id, #{ids.join(',')})").to_a
|
9
|
+
end
|
10
|
+
def where_with_order(column, ids)
|
11
|
+
return none if ids.blank?
|
12
|
+
ids = ids.uniq
|
13
|
+
return where(column => ids).order("field(#{column}, #{ids.map(&:inspect).join(',')})")
|
9
14
|
end
|
10
15
|
end
|
11
16
|
unless ActiveRecord::Base.respond_to?(:none) # extend only if not implement yet
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: find_with_order
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.3'
|
83
|
-
description: Find records in the same order of input
|
83
|
+
description: Find records in the same order of input array. Supports Rails 3+.
|
84
84
|
email:
|
85
85
|
- mrtmrt15xn@yahoo.com.tw
|
86
86
|
executables: []
|
@@ -124,5 +124,5 @@ rubyforge_project:
|
|
124
124
|
rubygems_version: 2.6.8
|
125
125
|
signing_key:
|
126
126
|
specification_version: 4
|
127
|
-
summary: Find records in the same order of input
|
127
|
+
summary: Find records in the same order of input array.
|
128
128
|
test_files: []
|