find_with_order 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +9 -9
- data/.travis.yml +37 -28
- data/CHANGELOG.md +24 -0
- data/CODE_OF_CONDUCT.md +48 -48
- data/LICENSE.txt +21 -21
- data/README.md +98 -92
- data/Rakefile +10 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/find_with_order.gemspec +45 -38
- data/gemfiles/3.2.gemfile +14 -17
- data/gemfiles/4.2.gemfile +14 -17
- data/gemfiles/5.0.gemfile +14 -17
- data/gemfiles/5.1.gemfile +14 -0
- data/gemfiles/5.2.gemfile +14 -0
- data/lib/find_with_order.rb +37 -37
- data/lib/find_with_order/mysql_support.rb +25 -23
- data/lib/find_with_order/pg_support.rb +53 -36
- data/lib/find_with_order/version.rb +3 -3
- metadata +22 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a8ac84fed8e09261dce7efef6fe2bae0702d7c026ca6188de2f6a49109da6c29
|
4
|
+
data.tar.gz: 28ae1da04f85c941705888c8d1f9310ed1729fed90d9e3527117b987baadd49f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c4a224be2b0f44db2a666c987b9ad37d2d6b66efa38153027420b757882160900fe6cea1318a13c714bafbc7fbe91b3a774007bb3920c2a450e250863043dd5
|
7
|
+
data.tar.gz: 279b4902229e08b051ea971b82ff969d6b17ab25d065d3ca202ce3bbe8b768469a5df435bd80899e1122fc51ef54cf931100906a74bd61a7d90a1f2f85568db3
|
data/.gitignore
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
/.bundle/
|
2
|
-
/.yardoc
|
3
|
-
/Gemfile.lock
|
4
|
-
/_yardoc/
|
5
|
-
/coverage/
|
6
|
-
/doc/
|
7
|
-
/pkg/
|
8
|
-
/spec/reports/
|
9
|
-
/tmp/
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
data/.travis.yml
CHANGED
@@ -1,28 +1,37 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
-
|
9
|
-
|
10
|
-
|
11
|
-
-
|
12
|
-
|
13
|
-
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
-
|
18
|
-
-
|
19
|
-
|
20
|
-
-
|
21
|
-
-
|
22
|
-
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
-
|
28
|
-
|
1
|
+
sudo: false
|
2
|
+
env:
|
3
|
+
global:
|
4
|
+
- CC_TEST_REPORTER_ID=6384ecacad280dc863595e6e369a43299e157c0e7588b636e025a1808a3f5581
|
5
|
+
language: ruby
|
6
|
+
rvm:
|
7
|
+
- 2.2
|
8
|
+
- 2.3
|
9
|
+
env:
|
10
|
+
- DB=mysql
|
11
|
+
- DB=pg
|
12
|
+
services:
|
13
|
+
- postgresql
|
14
|
+
addons:
|
15
|
+
postgresql: "9.4"
|
16
|
+
before_install:
|
17
|
+
- gem i rubygems-update -v '<3' && update_rubygems
|
18
|
+
- gem install bundler -v 1.17.3
|
19
|
+
- gem --version
|
20
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
21
|
+
- chmod +x ./cc-test-reporter
|
22
|
+
- ./cc-test-reporter before-build
|
23
|
+
before_script:
|
24
|
+
- mysql -V
|
25
|
+
- mysql -u root -e 'CREATE DATABASE travis_ci_test;'
|
26
|
+
- psql -c "SELECT version();"
|
27
|
+
- psql -c 'create database travis_ci_test;' -U postgres
|
28
|
+
gemfile:
|
29
|
+
- gemfiles/3.2.gemfile
|
30
|
+
- gemfiles/4.2.gemfile
|
31
|
+
- gemfiles/5.0.gemfile
|
32
|
+
- gemfiles/5.1.gemfile
|
33
|
+
- gemfiles/5.2.gemfile
|
34
|
+
script:
|
35
|
+
- bundle exec rake test
|
36
|
+
after_script:
|
37
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
## Change Log
|
2
|
+
|
3
|
+
### v1.2.1 2017/09/21
|
4
|
+
- [#11](https://github.com/khiav223577/find_with_order/pull/11) ambiguous id when not specify table name (@khiav223577)
|
5
|
+
|
6
|
+
### v1.2.0 2017/07/01
|
7
|
+
- [#10](https://github.com/khiav223577/find_with_order/pull/10) Supports #with_order method (@khiav223577)
|
8
|
+
|
9
|
+
### v1.1.1 2017/06/22
|
10
|
+
- [#9](https://github.com/khiav223577/find_with_order/pull/9) Restrict dependency to activerecord (@khiav223577)
|
11
|
+
- [#8](https://github.com/khiav223577/find_with_order/pull/8) try to fix travis (@khiav223577)
|
12
|
+
|
13
|
+
### v1.1.0 2017/03/22
|
14
|
+
- [#7](https://github.com/khiav223577/find_with_order/pull/7) Add PostgreSQL support (@khiav223577)
|
15
|
+
|
16
|
+
### v1.0.1 2017/03/03
|
17
|
+
- [#6](https://github.com/khiav223577/find_with_order/pull/6) Fix ambiguous id (@khiav223577)
|
18
|
+
|
19
|
+
### v1.0.0 2017/02/23
|
20
|
+
- [#3](https://github.com/khiav223577/find_with_order/pull/3) where with order (@khiav223577)
|
21
|
+
|
22
|
+
### v0.0.2 2017/02/22
|
23
|
+
- [#2](https://github.com/khiav223577/find_with_order/pull/2) Fix Rails 3 doesn't have #none method (@khiav223577)
|
24
|
+
- [#1](https://github.com/khiav223577/find_with_order/pull/1) add find_with_order (@khiav223577)
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -1,49 +1,49 @@
|
|
1
|
-
# Contributor Code of Conduct
|
2
|
-
|
3
|
-
As contributors and maintainers of this project, and in the interest of
|
4
|
-
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
-
contribute through reporting issues, posting feature requests, updating
|
6
|
-
documentation, submitting pull requests or patches, and other activities.
|
7
|
-
|
8
|
-
We are committed to making participation in this project a harassment-free
|
9
|
-
experience for everyone, regardless of level of experience, gender, gender
|
10
|
-
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
-
body size, race, ethnicity, age, religion, or nationality.
|
12
|
-
|
13
|
-
Examples of unacceptable behavior by participants include:
|
14
|
-
|
15
|
-
* The use of sexualized language or imagery
|
16
|
-
* Personal attacks
|
17
|
-
* Trolling or insulting/derogatory comments
|
18
|
-
* Public or private harassment
|
19
|
-
* Publishing other's private information, such as physical or electronic
|
20
|
-
addresses, without explicit permission
|
21
|
-
* Other unethical or unprofessional conduct
|
22
|
-
|
23
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
-
threatening, offensive, or harmful.
|
28
|
-
|
29
|
-
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
-
fairly and consistently applying these principles to every aspect of managing
|
31
|
-
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
-
Conduct may be permanently removed from the project team.
|
33
|
-
|
34
|
-
This code of conduct applies both within project spaces and in public spaces
|
35
|
-
when an individual is representing the project or its community.
|
36
|
-
|
37
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
-
reported by contacting a project maintainer at mrtmrt15xn@yahoo.com.tw. All
|
39
|
-
complaints will be reviewed and investigated and will result in a response that
|
40
|
-
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
-
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
-
incident.
|
43
|
-
|
44
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
-
version 1.3.0, available at
|
46
|
-
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
-
|
48
|
-
[homepage]: http://contributor-covenant.org
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at mrtmrt15xn@yahoo.com.tw. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
49
|
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/LICENSE.txt
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2017 khiav reoy
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 khiav reoy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,92 +1,98 @@
|
|
1
|
-
|
2
|
-
# FindWithOrder
|
3
|
-
|
4
|
-
[![Gem Version](https://img.shields.io/gem/v/find_with_order.svg?style=flat)](https://rubygems.org/gems/find_with_order)
|
5
|
-
[![Build Status](https://travis-ci.org/khiav223577/find_with_order.svg?branch=master)](https://travis-ci.org/khiav223577/find_with_order)
|
6
|
-
[![RubyGems](http://img.shields.io/gem/dt/find_with_order.svg?style=flat)](https://rubygems.org/gems/find_with_order)
|
7
|
-
[![Code Climate](https://codeclimate.com/github/khiav223577/find_with_order/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/find_with_order)
|
8
|
-
[![Test Coverage](https://codeclimate.com/github/khiav223577/find_with_order/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/find_with_order/coverage)
|
9
|
-
|
10
|
-
Find records in the same order of input array.
|
11
|
-
|
12
|
-
## Installation
|
13
|
-
|
14
|
-
Add this line to your application's Gemfile:
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
gem 'find_with_order'
|
18
|
-
```
|
19
|
-
|
20
|
-
And then execute:
|
21
|
-
|
22
|
-
$ bundle
|
23
|
-
|
24
|
-
Or install it yourself as:
|
25
|
-
|
26
|
-
$ gem install find_with_order
|
27
|
-
|
28
|
-
## Usage
|
29
|
-
|
30
|
-
### Find records
|
31
|
-
```rb
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
```
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
##
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
1
|
+
|
2
|
+
# FindWithOrder
|
3
|
+
|
4
|
+
[![Gem Version](https://img.shields.io/gem/v/find_with_order.svg?style=flat)](https://rubygems.org/gems/find_with_order)
|
5
|
+
[![Build Status](https://travis-ci.org/khiav223577/find_with_order.svg?branch=master)](https://travis-ci.org/khiav223577/find_with_order)
|
6
|
+
[![RubyGems](http://img.shields.io/gem/dt/find_with_order.svg?style=flat)](https://rubygems.org/gems/find_with_order)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/khiav223577/find_with_order/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/find_with_order)
|
8
|
+
[![Test Coverage](https://codeclimate.com/github/khiav223577/find_with_order/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/find_with_order/coverage)
|
9
|
+
|
10
|
+
Find records in the same order of input array.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'find_with_order'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install find_with_order
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
### Find records with the order of IDs: `find_with_order`
|
31
|
+
```rb
|
32
|
+
ids = [3, 1, 5]
|
33
|
+
|
34
|
+
User.find(ids).map(&:id)
|
35
|
+
# => [1, 3, 5]
|
36
|
+
|
37
|
+
User.find_with_order(ids).map(&:id)
|
38
|
+
# => [3, 1, 5]
|
39
|
+
```
|
40
|
+
|
41
|
+
### Support ordering by other columns: `where_with_order`
|
42
|
+
```rb
|
43
|
+
names = %w(Pearl John Kathenrie)
|
44
|
+
|
45
|
+
User.where(name: names).pluck(:name)
|
46
|
+
# => ['John', 'Pearl', 'Kathenrie']
|
47
|
+
|
48
|
+
User.where_with_order(:name, names).pluck(:name)
|
49
|
+
# => ['Pearl', 'John', 'Kathenrie']
|
50
|
+
```
|
51
|
+
|
52
|
+
### Support ordering only part of results: `with_order`
|
53
|
+
```rb
|
54
|
+
names = %w(Pearl John)
|
55
|
+
|
56
|
+
User.leader.with_order(:name, names).pluck(:name)
|
57
|
+
# => ['Pearl', 'John', 'Kathenrie']
|
58
|
+
|
59
|
+
User.leader.with_order(:name, names, null_first: true).pluck(:name)
|
60
|
+
# => ['Kathenrie', 'Pearl', 'John']
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
## Benchmark
|
65
|
+
### Compare with manually sorting in rails
|
66
|
+
|
67
|
+
```
|
68
|
+
user system total real
|
69
|
+
Find with order 0.050000 0.010000 0.060000 ( 0.074975)
|
70
|
+
Find then sort by index 2.520000 0.120000 2.640000 ( 3.238615)
|
71
|
+
Find then sort by hash mapping 1.410000 0.070000 1.480000 ( 1.737176)
|
72
|
+
```
|
73
|
+
[test script](https://github.com/khiav223577/find_with_order/issues/4)
|
74
|
+
|
75
|
+
### Compare with order_as_specified
|
76
|
+
```
|
77
|
+
user system total real
|
78
|
+
order_as_specified 0.020000 0.000000 0.020000 ( 0.703773)
|
79
|
+
where_with_order 0.020000 0.000000 0.020000 ( 0.031723)
|
80
|
+
```
|
81
|
+
[test script](https://github.com/khiav223577/find_with_order/issues/4#issuecomment-307376453)
|
82
|
+
|
83
|
+
|
84
|
+
## Development
|
85
|
+
|
86
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test DB=mysql` / `rake test DB=pg` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
87
|
+
|
88
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/find_with_order. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
93
|
+
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
98
|
+
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rake/testtask"
|
3
|
-
|
4
|
-
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs << "test"
|
6
|
-
t.libs << "lib"
|
7
|
-
t.test_files = FileList['test/**/*_test.rb']
|
8
|
-
end
|
9
|
-
|
10
|
-
task :default => :test
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :test
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "find_with_order"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "find_with_order"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
set -vx
|
5
|
-
|
6
|
-
bundle install --gemfile=gemfiles/4.2.gemfile
|
7
|
-
|
8
|
-
# Do any other automated setup that you need to do here
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
set -euo pipefail
|
3
|
+
IFS=$'\n\t'
|
4
|
+
set -vx
|
5
|
+
|
6
|
+
bundle install --gemfile=gemfiles/4.2.gemfile
|
7
|
+
|
8
|
+
# Do any other automated setup that you need to do here
|
data/find_with_order.gemspec
CHANGED
@@ -1,38 +1,45 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'find_with_order/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "find_with_order"
|
8
|
-
spec.version = FindWithOrder::VERSION
|
9
|
-
spec.authors = ["khiav reoy"]
|
10
|
-
spec.email = ["mrtmrt15xn@yahoo.com.tw"]
|
11
|
-
|
12
|
-
spec.summary = %q{A simple way to find records in the same order of input array.}
|
13
|
-
spec.description = %q{A simple way to find records in the same order of input array. Has better performance than manually sorting. Supports Rails 3+.}
|
14
|
-
spec.homepage = "https://github.com/khiav223577/find_with_order"
|
15
|
-
spec.license = "MIT"
|
16
|
-
|
17
|
-
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
-
# delete this section to allow pushing this gem to any host.
|
19
|
-
#if spec.respond_to?(:metadata)
|
20
|
-
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
-
#else
|
22
|
-
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
-
#end
|
24
|
-
|
25
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
-
spec.bindir = "exe"
|
27
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
-
spec.require_paths = ["lib"]
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'find_with_order/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "find_with_order"
|
8
|
+
spec.version = FindWithOrder::VERSION
|
9
|
+
spec.authors = ["khiav reoy"]
|
10
|
+
spec.email = ["mrtmrt15xn@yahoo.com.tw"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple way to find records in the same order of input array.}
|
13
|
+
spec.description = %q{A simple way to find records in the same order of input array. Has better performance than manually sorting. Supports Rails 3+.}
|
14
|
+
spec.homepage = "https://github.com/khiav223577/find_with_order"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
#if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
#else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
#end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
spec.metadata = {
|
30
|
+
'homepage_uri' => 'https://github.com/khiav223577/find_with_order',
|
31
|
+
'changelog_uri' => 'https://github.com/khiav223577/find_with_order/blob/master/CHANGELOG.md',
|
32
|
+
'source_code_uri' => 'https://github.com/khiav223577/find_with_order',
|
33
|
+
'documentation_uri' => 'http://www.rubydoc.info/gems/find_with_order',
|
34
|
+
'bug_tracker_uri' => 'https://github.com/khiav223577/find_with_order/issues',
|
35
|
+
}
|
36
|
+
|
37
|
+
spec.add_development_dependency 'bundler', '>= 1.17', '< 3.x'
|
38
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
39
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
40
|
+
spec.add_development_dependency "mysql2", ">= 0.3"
|
41
|
+
spec.add_development_dependency "pg", "~> 0.18"
|
42
|
+
|
43
|
+
spec.add_dependency "activerecord", ">= 3"
|
44
|
+
|
45
|
+
end
|
data/gemfiles/3.2.gemfile
CHANGED
@@ -1,17 +1,14 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
17
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem "activerecord", "~> 3.2.0"
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
case ENV['DB']
|
7
|
+
when "mysql" ; gem 'mysql2' , '0.3.21'
|
8
|
+
when "postgres" ; gem 'pg', '~> 0.18'
|
9
|
+
end
|
10
|
+
gem "simplecov"
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec :path => "../"
|
14
|
+
|
data/gemfiles/4.2.gemfile
CHANGED
@@ -1,17 +1,14 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
17
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem "activerecord", "~> 4.2.0"
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
case ENV['DB']
|
7
|
+
when "mysql" ; gem 'mysql2' , '0.3.21'
|
8
|
+
when "postgres" ; gem 'pg', '~> 0.18'
|
9
|
+
end
|
10
|
+
gem "simplecov"
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec :path => "../"
|
14
|
+
|
data/gemfiles/5.0.gemfile
CHANGED
@@ -1,17 +1,14 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
17
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem "activerecord", "~> 5.0.0"
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
case ENV['DB']
|
7
|
+
when "mysql" ; gem 'mysql2' , '0.3.21'
|
8
|
+
when "postgres" ; gem 'pg', '~> 0.18'
|
9
|
+
end
|
10
|
+
gem "simplecov"
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec :path => "../"
|
14
|
+
|
data/lib/find_with_order.rb
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
require "find_with_order/version"
|
2
|
-
require "find_with_order/mysql_support"
|
3
|
-
require "find_with_order/pg_support"
|
4
|
-
require 'active_record'
|
5
|
-
|
6
|
-
module FindWithOrder
|
7
|
-
class << self
|
8
|
-
def supporter
|
9
|
-
return FindWithOrder::PGSupport if defined?(PG)
|
10
|
-
return FindWithOrder::MysqlSupport
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class << ActiveRecord::Base
|
16
|
-
def find_with_order(ids)
|
17
|
-
return none if ids.blank?
|
18
|
-
return FindWithOrder.supporter.find_with_order(self, ids.uniq)
|
19
|
-
end
|
20
|
-
|
21
|
-
def where_with_order(column, ids)
|
22
|
-
return none if ids.blank?
|
23
|
-
return FindWithOrder.supporter.where_with_order(self, column, ids.uniq)
|
24
|
-
end
|
25
|
-
|
26
|
-
def with_order(column, ids, null_first: false)
|
27
|
-
FindWithOrder.supporter.with_order(self, column, ids, null_first: null_first)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
unless ActiveRecord::Base.respond_to?(:none) # extend only if not implement yet
|
32
|
-
class ActiveRecord::Base
|
33
|
-
def self.none #For Rails 3
|
34
|
-
where('1=0')
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
1
|
+
require "find_with_order/version"
|
2
|
+
require "find_with_order/mysql_support"
|
3
|
+
require "find_with_order/pg_support"
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
module FindWithOrder
|
7
|
+
class << self
|
8
|
+
def supporter
|
9
|
+
return FindWithOrder::PGSupport if defined?(PG)
|
10
|
+
return FindWithOrder::MysqlSupport
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class << ActiveRecord::Base
|
16
|
+
def find_with_order(ids)
|
17
|
+
return none if ids.blank?
|
18
|
+
return FindWithOrder.supporter.find_with_order(self, ids.uniq)
|
19
|
+
end
|
20
|
+
|
21
|
+
def where_with_order(column, ids)
|
22
|
+
return none if ids.blank?
|
23
|
+
return FindWithOrder.supporter.where_with_order(self, column, ids.uniq)
|
24
|
+
end
|
25
|
+
|
26
|
+
def with_order(column, ids, null_first: false)
|
27
|
+
FindWithOrder.supporter.with_order(self, column, ids, null_first: null_first)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
unless ActiveRecord::Base.respond_to?(:none) # extend only if not implement yet
|
32
|
+
class ActiveRecord::Base
|
33
|
+
def self.none #For Rails 3
|
34
|
+
where('1=0')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,23 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
column = column
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FindWithOrder::MysqlSupport
|
4
|
+
class << self
|
5
|
+
def find_with_order(relation, ids)
|
6
|
+
relation.where(id: ids)
|
7
|
+
.order("field(#{relation.table_name}.id, #{ids.join(',')})")
|
8
|
+
.to_a
|
9
|
+
end
|
10
|
+
|
11
|
+
def where_with_order(relation, column, ids)
|
12
|
+
with_order(relation.where(column => ids), column, ids, null_first: true)
|
13
|
+
end
|
14
|
+
|
15
|
+
def with_order(relation, column, ids, null_first: false)
|
16
|
+
if column.is_a?(Symbol) and relation.column_names.include?(column.to_s)
|
17
|
+
column = "#{relation.connection.quote_table_name(relation.table_name)}.#{relation.connection.quote_column_name(column)}"
|
18
|
+
else
|
19
|
+
column = column.to_s
|
20
|
+
end
|
21
|
+
return relation.order("field(#{column}, #{ids.map(&:inspect).join(',')})") if null_first
|
22
|
+
return relation.order("field(#{column}, #{ids.reverse.map(&:inspect).join(',')}) DESC")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,36 +1,53 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FindWithOrder::PGSupport
|
4
|
+
class << self
|
5
|
+
def find_with_order(relation, ids)
|
6
|
+
# return relation.where(id: ids).order("array_position(ARRAY[#{ids.join(',')}], #{relation.table_name}.id)").to_a #array_position is only support in PG >= 9.5
|
7
|
+
id_val = uuid?(relation, 'id') ? '(id.val)::uuid' : 'id.val'
|
8
|
+
values = to_sql_values(ids)
|
9
|
+
return relation.where(id: ids)
|
10
|
+
.joins("JOIN (SELECT id.val, row_number() over() FROM (VALUES(#{values})) AS id(val)) AS id ON (#{relation.table_name}.id = #{id_val})")
|
11
|
+
.order('row_number')
|
12
|
+
end
|
13
|
+
|
14
|
+
def where_with_order(relation, column, ids)
|
15
|
+
with_order(relation.where(column => ids), column, ids)
|
16
|
+
end
|
17
|
+
|
18
|
+
def with_order(relation, column, ids, null_first: false)
|
19
|
+
id_val = uuid?(relation, column) ? '(id.val)::uuid' : 'id.val'
|
20
|
+
if column.is_a?(Symbol) and relation.column_names.include?(column.to_s)
|
21
|
+
column = "#{relation.connection.quote_table_name(relation.table_name)}.#{relation.connection.quote_column_name(column)}"
|
22
|
+
else
|
23
|
+
column = column.to_s
|
24
|
+
end
|
25
|
+
ids = ids.reverse if null_first
|
26
|
+
values = to_sql_values(ids)
|
27
|
+
return relation.joins("LEFT JOIN (SELECT id.val, row_number() over() FROM (VALUES(#{values})) AS id(val)) AS id ON (#{column} = #{id_val})")
|
28
|
+
.order(null_first ? 'row_number DESC' : 'row_number')
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def to_sql_values(ids)
|
34
|
+
case ids.first
|
35
|
+
when Numeric
|
36
|
+
return ids.join('),(')
|
37
|
+
# return relation.order("array_position(ARRAY[#{ids.join(',')}], #{column})") #array_position is only support in PG >= 9.5
|
38
|
+
when String
|
39
|
+
ids = ids.map{|s| ActiveRecord::Base.connection.quote_string(s) }
|
40
|
+
return "'#{ids.join("'),('")}'"
|
41
|
+
# return relation.order("array_position(ARRAY['#{ids.join("','")}']::varchar[], #{column})") #array_position is only support in PG >= 9.5
|
42
|
+
else
|
43
|
+
raise "not support type: #{ids.first.class}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def uuid?(relation, column)
|
48
|
+
column_info = relation.columns_hash[column.to_s]
|
49
|
+
return false if !column_info
|
50
|
+
return column_info.type == :uuid
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module FindWithOrder
|
2
|
-
VERSION = "1.
|
3
|
-
end
|
1
|
+
module FindWithOrder
|
2
|
+
VERSION = "1.3.0"
|
3
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: find_with_order
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.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:
|
11
|
+
date: 2019-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
- - "<"
|
18
21
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
22
|
+
version: 3.x
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.17'
|
30
|
+
- - "<"
|
25
31
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
32
|
+
version: 3.x
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,6 +110,7 @@ extra_rdoc_files: []
|
|
104
110
|
files:
|
105
111
|
- ".gitignore"
|
106
112
|
- ".travis.yml"
|
113
|
+
- CHANGELOG.md
|
107
114
|
- CODE_OF_CONDUCT.md
|
108
115
|
- LICENSE.txt
|
109
116
|
- README.md
|
@@ -114,6 +121,8 @@ files:
|
|
114
121
|
- gemfiles/3.2.gemfile
|
115
122
|
- gemfiles/4.2.gemfile
|
116
123
|
- gemfiles/5.0.gemfile
|
124
|
+
- gemfiles/5.1.gemfile
|
125
|
+
- gemfiles/5.2.gemfile
|
117
126
|
- lib/find_with_order.rb
|
118
127
|
- lib/find_with_order/mysql_support.rb
|
119
128
|
- lib/find_with_order/pg_support.rb
|
@@ -121,7 +130,12 @@ files:
|
|
121
130
|
homepage: https://github.com/khiav223577/find_with_order
|
122
131
|
licenses:
|
123
132
|
- MIT
|
124
|
-
metadata:
|
133
|
+
metadata:
|
134
|
+
homepage_uri: https://github.com/khiav223577/find_with_order
|
135
|
+
changelog_uri: https://github.com/khiav223577/find_with_order/blob/master/CHANGELOG.md
|
136
|
+
source_code_uri: https://github.com/khiav223577/find_with_order
|
137
|
+
documentation_uri: http://www.rubydoc.info/gems/find_with_order
|
138
|
+
bug_tracker_uri: https://github.com/khiav223577/find_with_order/issues
|
125
139
|
post_install_message:
|
126
140
|
rdoc_options: []
|
127
141
|
require_paths:
|
@@ -138,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
152
|
version: '0'
|
139
153
|
requirements: []
|
140
154
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.6
|
155
|
+
rubygems_version: 2.7.6
|
142
156
|
signing_key:
|
143
157
|
specification_version: 4
|
144
158
|
summary: A simple way to find records in the same order of input array.
|