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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb63ce7cc3c9a7a3f40cc9a3654dff8341b83693
4
- data.tar.gz: 92bed035a6036c6c8b8ef35fa7ef59962ce3f42c
3
+ metadata.gz: ce3d6c02d373b47ed41758e0b88cee251fb6722a
4
+ data.tar.gz: a89e7f837e84f7de799c809b4d3474987f0e9a86
5
5
  SHA512:
6
- metadata.gz: 6f812bb289265d92ee0ee6b3cf49265b2e8de116e20001f0bedf25169665361c466fdd938182cc0e8b4b1784d5ed2b3bfd5359e684a27c482eb2af543f7e8f79
7
- data.tar.gz: fee919bde9d1e21ba3e6a4921b1e2d6e4e266b1ab29aabc5c8881a18317b99142cb7b7fbb2ac6e5013c33d8fe2ec9a8c74639bf38de0167d0c4a3f614730d5c5
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
 
@@ -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 IDs.}
13
- spec.description = %q{Find records in the same order of input IDs. Supports Rails 3+.}
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
 
@@ -1,3 +1,3 @@
1
1
  module FindWithOrder
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -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 none if ids.empty?
8
- where(id: ids).order("field(id, #{ids.join(',')})")
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.2
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-22 00:00:00.000000000 Z
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 IDs. Supports Rails 3+.
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 IDs.
127
+ summary: Find records in the same order of input array.
128
128
  test_files: []