find_with_order 1.1.1 → 1.3.1

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
- SHA1:
3
- metadata.gz: d955f79fc86d2b1d9ddd2e4b0e5576fb2d89d252
4
- data.tar.gz: d0eb726b23436e935c67065ec57ba4423fac9657
2
+ SHA256:
3
+ metadata.gz: 015a48fca352ef1bb4b6aab8a206ad690dfea36462f5b33f7807bee6e786144a
4
+ data.tar.gz: beb3426fda4db3e29cb0ab0908ffcda2911f6564a6673699d468f6ea3e86a702
5
5
  SHA512:
6
- metadata.gz: 8abb85b303feae5550fafad62a2db381c16350829427f4ce46da796e3a15fcc0e28fa2cf25b3b168407ebe81cb98280fab6a584dc91ebe4214ed4bad6cab949a
7
- data.tar.gz: 33ad5f2bd2f4957828d02072ca4c4feca2b35c39d8c85021e3820cc8fb9882a540f5ee306d74c392ead5fca7ce954886355af12ebacd2a20d0813d2efde39e04
6
+ metadata.gz: 100044ac0115988577753964bea0b55b333943a688163caf36230a93ae06fccd27874d94cc0bd5b8644d946d33e0ac73c0f6427223a379824c548ca709d8b656
7
+ data.tar.gz: 0bc3c0bfa032cce6fad60a22e06f9ae3e3842f46054481eb3657a2b61ffb836af93771c15f7429f23548dd75cb0ad120361a0cdf2b97706c1889be007046e4ea
@@ -0,0 +1,98 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ paths-ignore:
6
+ - 'README.md'
7
+ - 'CHANGELOG.md'
8
+ pull_request:
9
+ branches: [ master ]
10
+ paths-ignore:
11
+ - 'README.md'
12
+ - 'CHANGELOG.md'
13
+
14
+ jobs:
15
+ test:
16
+ runs-on: ubuntu-latest
17
+ name: Test
18
+ if: "contains(github.event.commits[0].message, '[ci skip]') == false"
19
+ strategy:
20
+ fail-fast: false
21
+ matrix:
22
+ db:
23
+ - mysql
24
+ - pg
25
+ ruby:
26
+ # - 2.2 # skip Ruby 2.2 since mysql2 raises segmentation fault at 0x0000000000000000
27
+ # See: https://github.community/t/mysql2-segmentation-fault-at-0x0000000000000000/159283
28
+ - 2.6
29
+ - 2.7
30
+ gemfile:
31
+ - 3.2.gemfile
32
+ - 4.2.gemfile
33
+ - 5.0.gemfile
34
+ - 5.1.gemfile
35
+ - 5.2.gemfile
36
+ - 6.0.gemfile
37
+ - 6.1.gemfile
38
+ exclude:
39
+ - gemfile: 3.2.gemfile
40
+ ruby: 2.6
41
+ - gemfile: 3.2.gemfile
42
+ ruby: 2.7
43
+ - gemfile: 4.2.gemfile
44
+ ruby: 2.7
45
+ - gemfile: 6.0.gemfile
46
+ ruby: 2.2
47
+ - gemfile: 6.1.gemfile
48
+ ruby: 2.2
49
+ env:
50
+ BUNDLE_GEMFILE: "gemfiles/${{ matrix.gemfile }}"
51
+ DB: "${{ matrix.db }}"
52
+
53
+ services:
54
+ mysql:
55
+ image: mysql:5.6
56
+ env:
57
+ MYSQL_ROOT_PASSWORD: root_password
58
+ MYSQL_USER: developer
59
+ MYSQL_PASSWORD: developer_password
60
+ MYSQL_DATABASE: github_actions_test
61
+ ports:
62
+ - 3306:3306
63
+ # Set health checks to wait until mysql has started
64
+ options: >-
65
+ --health-cmd "mysqladmin ping"
66
+ --health-interval 10s
67
+ --health-timeout 5s
68
+ --health-retries 3
69
+ postgres:
70
+ image: postgres:9.6
71
+ env:
72
+ POSTGRES_USER: developer
73
+ POSTGRES_PASSWORD: developer_password
74
+ POSTGRES_DB: github_actions_test
75
+ ports:
76
+ - 5432:5432
77
+ # Set health checks to wait until postgres has started
78
+ options: >-
79
+ --health-cmd pg_isready
80
+ --health-interval 10s
81
+ --health-timeout 5s
82
+ --health-retries 3
83
+
84
+ steps:
85
+ - name: Checkout
86
+ uses: actions/checkout@v2
87
+ - name: Setup Ruby
88
+ uses: ruby/setup-ruby@v1
89
+ with:
90
+ ruby-version: ${{ matrix.ruby }}
91
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
92
+ - name: Run tests
93
+ run: bundle exec rake
94
+ - name: Publish code coverage
95
+ if: ${{ success() && env.CC_TEST_REPORTER_ID }}
96
+ uses: paambaati/codeclimate-action@v2.7.5
97
+ env:
98
+ CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
data/.gitignore CHANGED
@@ -1,9 +1,10 @@
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/
10
+ *.gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,32 @@
1
+ ## Change Log
2
+
3
+ ### [v1.3.0](https://github.com/khiav223577/find_with_order/compare/v1.2.1...v1.3.0) 2019/05/12
4
+ - [#18](https://github.com/khiav223577/find_with_order/pull/18) Support psql uuid type (@khiav223577)
5
+ - [#17](https://github.com/khiav223577/find_with_order/pull/17) Remove deprecated codeclimate-test-reporter gem and update travis config (@khiav223577)
6
+ - [#16](https://github.com/khiav223577/find_with_order/pull/16) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
7
+ - [#14](https://github.com/khiav223577/find_with_order/pull/14) test rails 5.2 (@khiav223577)
8
+ - [#13](https://github.com/khiav223577/find_with_order/pull/13) should test both 5.0.x and 5.1.x (@khiav223577)
9
+ - [#12](https://github.com/khiav223577/find_with_order/pull/12) test find_with_order in rails 5.1.x (@khiav223577)
10
+
11
+ ### [v1.2.1](https://github.com/khiav223577/find_with_order/compare/v1.2.0...v1.2.1) 2017/09/21
12
+ - [#11](https://github.com/khiav223577/find_with_order/pull/11) ambiguous id when not specify table name (@khiav223577)
13
+
14
+ ### [v1.2.0](https://github.com/khiav223577/find_with_order/compare/v1.1.1...v1.2.0) 2017/07/01
15
+ - [#10](https://github.com/khiav223577/find_with_order/pull/10) Supports #with_order method (@khiav223577)
16
+
17
+ ### [v1.1.1](https://github.com/khiav223577/find_with_order/compare/v1.1.0...v1.1.1) 2017/06/22
18
+ - [#9](https://github.com/khiav223577/find_with_order/pull/9) Restrict dependency to activerecord (@khiav223577)
19
+ - [#8](https://github.com/khiav223577/find_with_order/pull/8) try to fix travis (@khiav223577)
20
+
21
+ ### [v1.1.0](https://github.com/khiav223577/find_with_order/compare/v1.0.1...v1.1.0) 2017/03/22
22
+ - [#7](https://github.com/khiav223577/find_with_order/pull/7) Add PostgreSQL support (@khiav223577)
23
+
24
+ ### [v1.0.1](https://github.com/khiav223577/find_with_order/compare/v1.0.0...v1.0.1) 2017/03/03
25
+ - [#6](https://github.com/khiav223577/find_with_order/pull/6) Fix ambiguous id (@khiav223577)
26
+
27
+ ### [v1.0.0](https://github.com/khiav223577/find_with_order/compare/v0.0.2...v1.0.0) 2017/02/23
28
+ - [#3](https://github.com/khiav223577/find_with_order/pull/3) where with order (@khiav223577)
29
+
30
+ ### [v0.0.2](https://github.com/khiav223577/find_with_order/compare/v0.0.1...v0.0.2) 2017/02/22
31
+ - [#2](https://github.com/khiav223577/find_with_order/pull/2) Fix Rails 3 doesn't have #none method (@khiav223577)
32
+ - [#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,81 +1,103 @@
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 in the same order of input IDs
31
- ```rb
32
- User.find([3, 1, 5]).map(&:id)
33
- # => [1, 3, 5]
34
-
35
- User.find_with_order([3, 1, 5]).map(&:id)
36
- # => [3, 1, 5]
37
- ```
38
- ### Support order other columns
39
- ```rb
40
- User.where(name: %w(Pearl John Kathenrie)).pluck(:name)
41
- # => ['John', 'Pearl', 'Kathenrie']
42
-
43
- User.where_with_order(:name, %w(Pearl John Kathenrie)).pluck(:name)
44
- # => ['Pearl', 'John', 'Kathenrie']
45
- ```
46
-
47
- ## Benchmark
48
- ### Compare with manually sorting in rails
49
-
50
- ```
51
- user system total real
52
- Find with order 0.050000 0.010000 0.060000 ( 0.074975)
53
- Find then sort by index 2.520000 0.120000 2.640000 ( 3.238615)
54
- Find then sort by hash mapping 1.410000 0.070000 1.480000 ( 1.737176)
55
- ```
56
- [test script](https://github.com/khiav223577/find_with_order/issues/4)
57
-
58
- ### Compare with order_as_specified
59
- ```
60
- user system total real
61
- order_as_specified 0.020000 0.000000 0.020000 ( 0.703773)
62
- where_with_order 0.020000 0.000000 0.020000 ( 0.031723)
63
- ```
64
- [test script](https://github.com/khiav223577/find_with_order/issues/4#issuecomment-307376453)
65
-
66
-
67
- ## Development
68
-
69
- 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.
70
-
71
- 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).
72
-
73
- ## Contributing
74
-
75
- 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.
76
-
77
-
78
- ## License
79
-
80
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
81
-
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://github.com/khiav223577/find_with_order/workflows/Ruby/badge.svg)](https://github.com/khiav223577/find_with_order/actions)
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
+ ## Supports
13
+ - Ruby 2.2 ~ 2.7
14
+ - Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0
15
+ - MySQL, PostgreSQL
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'find_with_order'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install find_with_order
32
+
33
+ ## Usage
34
+
35
+ ### Find records with the order of IDs: `find_with_order`
36
+ ```rb
37
+ ids = [3, 1, 5]
38
+
39
+ User.find(ids).map(&:id)
40
+ # => [1, 3, 5]
41
+
42
+ User.find_with_order(ids).map(&:id)
43
+ # => [3, 1, 5]
44
+ ```
45
+
46
+ ### Support ordering by other columns: `where_with_order`
47
+ ```rb
48
+ names = %w(Pearl John Doggy)
49
+
50
+ User.where(name: names).pluck(:name)
51
+ # => ['John', 'Pearl', 'Doggy']
52
+
53
+ User.where_with_order(:name, names).pluck(:name)
54
+ # => ['Pearl', 'John', 'Doggy']
55
+ ```
56
+
57
+ ### Support ordering only part of results: `with_order`
58
+ ```rb
59
+ names = %w(Pearl John)
60
+
61
+ User.leader.with_order(:name, names).pluck(:name)
62
+ # => ['Pearl', 'John', 'Doggy']
63
+
64
+ User.leader.with_order(:name, names, null_first: true).pluck(:name)
65
+ # => ['Doggy', 'Pearl', 'John']
66
+ ```
67
+
68
+
69
+ ## Benchmark
70
+ ### Compare with manually sorting in rails
71
+
72
+ ```
73
+ user system total real
74
+ Find with order 0.050000 0.010000 0.060000 ( 0.074975)
75
+ Find then sort by index 2.520000 0.120000 2.640000 ( 3.238615)
76
+ Find then sort by hash mapping 1.410000 0.070000 1.480000 ( 1.737176)
77
+ ```
78
+ [test script](https://github.com/khiav223577/find_with_order/issues/4)
79
+
80
+ ### Compare with order_as_specified
81
+ ```
82
+ user system total real
83
+ order_as_specified 0.020000 0.000000 0.020000 ( 0.703773)
84
+ where_with_order 0.020000 0.000000 0.020000 ( 0.031723)
85
+ ```
86
+ [test script](https://github.com/khiav223577/find_with_order/issues/4#issuecomment-307376453)
87
+
88
+
89
+ ## Development
90
+
91
+ 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.
92
+
93
+ 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).
94
+
95
+ ## Contributing
96
+
97
+ 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.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
103
+
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
@@ -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
- spec.add_development_dependency "bundler", "~> 1.11"
31
- spec.add_development_dependency "rake", "~> 12.0"
32
- spec.add_development_dependency "minitest", "~> 5.0"
33
- spec.add_development_dependency "mysql2", ">= 0.3"
34
- spec.add_development_dependency "pg", "~> 0.18"
35
-
36
- spec.add_dependency "activerecord", ">= 3"
37
-
38
- end
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' => 'https://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
- # Specify your gem's dependencies in rails_or.gemspec
4
-
5
- gem "activerecord", "~> 3.2"
6
-
7
- group :test do
8
- case ENV['DB']
9
- when "mysql" ; gem 'mysql2' , '0.3.21'
10
- when "postgres" ; gem 'pg', '~> 0.18'
11
- end
12
- gem "simplecov"
13
- gem "codeclimate-test-reporter", "~> 1.0.0"
14
- end
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.4.10'
8
+ when 'pg' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem "simplecov", '< 0.18'
11
+ end
12
+
13
+ gemspec :path => "../"
14
+
data/gemfiles/4.2.gemfile CHANGED
@@ -1,17 +1,14 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in rails_or.gemspec
4
-
5
- gem "activerecord", "~> 4.2"
6
-
7
- group :test do
8
- case ENV['DB']
9
- when "mysql" ; gem 'mysql2' , '0.3.21'
10
- when "postgres" ; gem 'pg', '~> 0.18'
11
- end
12
- gem "simplecov"
13
- gem "codeclimate-test-reporter", "~> 1.0.0"
14
- end
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.4.10'
8
+ when 'pg' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem "simplecov", '< 0.18'
11
+ end
12
+
13
+ gemspec :path => "../"
14
+
data/gemfiles/5.0.gemfile CHANGED
@@ -1,17 +1,14 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in rails_or.gemspec
4
-
5
- gem "activerecord", "~> 5.0"
6
-
7
- group :test do
8
- case ENV['DB']
9
- when "mysql" ; gem 'mysql2' , '0.3.21'
10
- when "postgres" ; gem 'pg', '~> 0.18'
11
- end
12
- gem "simplecov"
13
- gem "codeclimate-test-reporter", "~> 1.0.0"
14
- end
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.4.10'
8
+ when 'pg' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem "simplecov", '< 0.18'
11
+ end
12
+
13
+ gemspec :path => "../"
14
+
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activerecord", "~> 5.1.0"
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2' , '0.4.10'
8
+ when 'pg' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem "simplecov", '< 0.18'
11
+ end
12
+
13
+ gemspec :path => "../"
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activerecord", "~> 5.2.0"
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2' , '0.5.1'
8
+ when 'pg' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem "simplecov", '< 0.18'
11
+ end
12
+
13
+ gemspec :path => "../"
14
+
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activerecord", "~> 6.0.0"
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2' , '0.5.1'
8
+ when 'pg' ; gem 'pg', '~> 0.18'
9
+ end
10
+ gem "simplecov", '< 0.18'
11
+ end
12
+
13
+ gemspec :path => "../"
14
+
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 6.1.4'
4
+
5
+ group :test do
6
+ case ENV['DB']
7
+ when 'mysql' ; gem 'mysql2' , '0.5.1'
8
+ when 'pg' ; gem 'pg', '~> 1.1'
9
+ end
10
+ gem "simplecov", '< 0.18'
11
+ end
12
+
13
+ gemspec :path => "../"
14
+
@@ -1,10 +1,25 @@
1
- module FindWithOrder
2
- module MysqlSupport
3
- def self.find_with_order(relation, ids)
4
- return relation.where(id: ids).order("field(#{relation.table_name}.id, #{ids.join(',')})").to_a
5
- end
6
- def self.where_with_order(relation, column, ids)
7
- return relation.where(column => ids).order("field(#{column}, #{ids.map(&:inspect).join(',')})")
8
- end
9
- end
10
- 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(Arel.sql("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(Arel.sql("field(#{column}, #{ids.map(&:inspect).join(',')})")) if null_first
22
+ return relation.order(Arel.sql("field(#{column}, #{ids.reverse.map(&:inspect).join(',')}) DESC"))
23
+ end
24
+ end
25
+ end
@@ -1,26 +1,53 @@
1
- module FindWithOrder
2
- module PGSupport
3
- def self.find_with_order(relation, ids)
4
- # 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
5
- return relation.where(id: ids)
6
- .joins("JOIN (SELECT id.val, row_number() over() FROM (VALUES(#{ids.join('),(')})) AS id(val)) AS id ON (#{relation.table_name}.id = id.val)")
7
- .order('row_number')
8
- end
9
- def self.where_with_order(relation, column, ids)
10
- relation = relation.where(column => ids)
11
- case ids.first
12
- when Numeric
13
- # return relation.order("array_position(ARRAY[#{ids.join(',')}], #{column})") #array_position is only support in PG >= 9.5
14
- return relation.joins("JOIN (SELECT id.val, row_number() over() FROM (VALUES(#{ids.join('),(')})) AS id(val)) AS id ON (#{column} = id.val)")
15
- .order('row_number')
16
- when String
17
- ids.map!{|s| ActiveRecord::Base.connection.quote_string(s) }
18
- # return relation.order("array_position(ARRAY['#{ids.join("','")}']::varchar[], #{column})") #array_position is only support in PG >= 9.5
19
- return relation.joins("JOIN (SELECT id.val, row_number() over() FROM (VALUES('#{ids.join("'),('")}')) AS id(val)) AS id ON (#{column} = id.val)")
20
- .order('row_number')
21
- else
22
- raise "not support type: #{ids.first.class}"
23
- end
24
- end
25
- end
26
- end
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.1.1"
3
- end
1
+ module FindWithOrder
2
+ VERSION = "1.3.1"
3
+ end
@@ -1,26 +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
- class << ActiveRecord::Base
7
- def find_with_order(ids)
8
- return none if ids.blank?
9
- ids = ids.uniq
10
- return FindWithOrder::PGSupport.find_with_order(self, ids) if defined?(PG)
11
- return FindWithOrder::MysqlSupport.find_with_order(self, ids)
12
- end
13
- def where_with_order(column, ids)
14
- return none if ids.blank?
15
- ids = ids.uniq
16
- return FindWithOrder::PGSupport.where_with_order(self, column, ids) if defined?(PG)
17
- return FindWithOrder::MysqlSupport.where_with_order(self, column, ids)
18
- end
19
- end
20
- unless ActiveRecord::Base.respond_to?(:none) # extend only if not implement yet
21
- class ActiveRecord::Base
22
- def self.none #For Rails 3
23
- where('1=0')
24
- end
25
- end
26
- 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
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.1.1
4
+ version: 1.3.1
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-06-22 00:00:00.000000000 Z
11
+ date: 2021-10-19 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: '1.11'
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: '1.11'
32
+ version: 3.x
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: rake
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -102,8 +108,9 @@ executables: []
102
108
  extensions: []
103
109
  extra_rdoc_files: []
104
110
  files:
111
+ - ".github/workflows/ruby.yml"
105
112
  - ".gitignore"
106
- - ".travis.yml"
113
+ - CHANGELOG.md
107
114
  - CODE_OF_CONDUCT.md
108
115
  - LICENSE.txt
109
116
  - README.md
@@ -114,6 +121,10 @@ 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
126
+ - gemfiles/6.0.gemfile
127
+ - gemfiles/6.1.gemfile
117
128
  - lib/find_with_order.rb
118
129
  - lib/find_with_order/mysql_support.rb
119
130
  - lib/find_with_order/pg_support.rb
@@ -121,7 +132,12 @@ files:
121
132
  homepage: https://github.com/khiav223577/find_with_order
122
133
  licenses:
123
134
  - MIT
124
- metadata: {}
135
+ metadata:
136
+ homepage_uri: https://github.com/khiav223577/find_with_order
137
+ changelog_uri: https://github.com/khiav223577/find_with_order/blob/master/CHANGELOG.md
138
+ source_code_uri: https://github.com/khiav223577/find_with_order
139
+ documentation_uri: https://www.rubydoc.info/gems/find_with_order
140
+ bug_tracker_uri: https://github.com/khiav223577/find_with_order/issues
125
141
  post_install_message:
126
142
  rdoc_options: []
127
143
  require_paths:
@@ -137,8 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
153
  - !ruby/object:Gem::Version
138
154
  version: '0'
139
155
  requirements: []
140
- rubyforge_project:
141
- rubygems_version: 2.6.8
156
+ rubygems_version: 3.2.14
142
157
  signing_key:
143
158
  specification_version: 4
144
159
  summary: A simple way to find records in the same order of input array.
data/.travis.yml DELETED
@@ -1,28 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2
4
- - 2.3
5
- sudo: required
6
- dist: trusty
7
- env:
8
- - DB=mysql
9
- - DB=pg
10
- services:
11
- - postgresql
12
- before_install:
13
- - gem install bundler
14
- - gem update --system
15
- - gem --version
16
- before_script:
17
- - mysql -u root -e 'CREATE DATABASE travis_ci_test;'
18
- - psql -c 'create database travis_ci_test;' -U postgres
19
- gemfile:
20
- - gemfiles/3.2.gemfile
21
- - gemfiles/4.2.gemfile
22
- - gemfiles/5.0.gemfile
23
- addons:
24
- code_climate:
25
- repo_token: 6384ecacad280dc863595e6e369a43299e157c0e7588b636e025a1808a3f5581
26
- after_success:
27
- - bundle exec codeclimate-test-reporter
28
-