find_with_order 1.0.1 → 1.1.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 +4 -4
- data/.travis.yml +7 -1
- data/README.md +4 -3
- data/find_with_order.gemspec +2 -1
- data/gemfiles/3.2.gemfile +4 -1
- data/gemfiles/4.2.gemfile +4 -1
- data/gemfiles/5.0.gemfile +4 -1
- data/lib/find_with_order.rb +6 -2
- data/lib/find_with_order/mysql_support.rb +10 -0
- data/lib/find_with_order/pg_support.rb +26 -0
- data/lib/find_with_order/version.rb +1 -1
- metadata +26 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b55d8a4b8781f20d1a7d637f7d0c1ff0a71f802
|
4
|
+
data.tar.gz: 95b37ac957f5fce50237e4869ce4702aa3a524a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4bfb50a7c782f329b8834ac21cac0dfe78e4dd7b3d027c7d0ac479f27cde0e51c2cbbec240a8f8b2cb72e7e49d421ea47e1992a4debf77f7896e0ebdfe5546c
|
7
|
+
data.tar.gz: 8a02ff25f68581747bd2a641ae147e900fe9e26e6d95269a02e1e7504caf04644d260e986cce0d2904a41af79787ac2c0c3025f686f378f08cbaa39cadab7ff4
|
data/.travis.yml
CHANGED
@@ -4,12 +4,18 @@ rvm:
|
|
4
4
|
- 2.3
|
5
5
|
sudo: required
|
6
6
|
dist: trusty
|
7
|
+
env:
|
8
|
+
- DB=mysql
|
9
|
+
- DB=pg
|
10
|
+
services:
|
11
|
+
- postgresql
|
7
12
|
before_install:
|
8
13
|
- gem install bundler
|
9
14
|
- gem update --system
|
10
15
|
- gem --version
|
11
16
|
before_script:
|
12
|
-
- mysql -u root -e 'CREATE DATABASE
|
17
|
+
- mysql -u root -e 'CREATE DATABASE travis_ci_test;'
|
18
|
+
- psql -c 'create database travis_ci_test;' -U postgres
|
13
19
|
gemfile:
|
14
20
|
- gemfiles/3.2.gemfile
|
15
21
|
- gemfiles/4.2.gemfile
|
data/README.md
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
+
|
2
|
+
# FindWithOrder
|
3
|
+
|
1
4
|
[](https://rubygems.org/gems/find_with_order)
|
2
5
|
[](https://travis-ci.org/khiav223577/find_with_order)
|
3
6
|
[](https://rubygems.org/gems/find_with_order)
|
4
7
|
[](https://codeclimate.com/github/khiav223577/find_with_order)
|
5
8
|
[](https://codeclimate.com/github/khiav223577/find_with_order/coverage)
|
6
9
|
|
7
|
-
# FindWithOrder
|
8
|
-
|
9
10
|
Find records in the same order of input array.
|
10
11
|
|
11
12
|
## Installation
|
@@ -55,7 +56,7 @@ Find then sort by hash mapping 1.120000 0.010000 1.130000 ( 1.192891)
|
|
55
56
|
|
56
57
|
## Development
|
57
58
|
|
58
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
59
|
+
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.
|
59
60
|
|
60
61
|
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).
|
61
62
|
|
data/find_with_order.gemspec
CHANGED
@@ -30,8 +30,9 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency "bundler", "~> 1.11"
|
31
31
|
spec.add_development_dependency "rake", "~> 12.0"
|
32
32
|
spec.add_development_dependency "minitest", "~> 5.0"
|
33
|
+
spec.add_development_dependency "mysql2", ">= 0.3"
|
34
|
+
spec.add_development_dependency "pg", "~> 0.18"
|
33
35
|
|
34
36
|
spec.add_dependency "rails", ">= 3"
|
35
|
-
spec.add_dependency "mysql2", ">= 0.3"
|
36
37
|
|
37
38
|
end
|
data/gemfiles/3.2.gemfile
CHANGED
@@ -5,7 +5,10 @@ source 'https://rubygems.org'
|
|
5
5
|
gem "activerecord", "~> 3.2"
|
6
6
|
|
7
7
|
group :test do
|
8
|
-
|
8
|
+
case ENV['DB']
|
9
|
+
when "mysql" ; gem 'mysql2' , '0.3.21'
|
10
|
+
when "postgres" ; gem 'pg', '~> 0.18'
|
11
|
+
end
|
9
12
|
gem "simplecov"
|
10
13
|
gem "codeclimate-test-reporter", "~> 1.0.0"
|
11
14
|
end
|
data/gemfiles/4.2.gemfile
CHANGED
@@ -5,7 +5,10 @@ source 'https://rubygems.org'
|
|
5
5
|
gem "activerecord", "~> 4.2"
|
6
6
|
|
7
7
|
group :test do
|
8
|
-
|
8
|
+
case ENV['DB']
|
9
|
+
when "mysql" ; gem 'mysql2' , '0.3.21'
|
10
|
+
when "postgres" ; gem 'pg', '~> 0.18'
|
11
|
+
end
|
9
12
|
gem "simplecov"
|
10
13
|
gem "codeclimate-test-reporter", "~> 1.0.0"
|
11
14
|
end
|
data/gemfiles/5.0.gemfile
CHANGED
@@ -5,7 +5,10 @@ source 'https://rubygems.org'
|
|
5
5
|
gem "activerecord", "~> 5.0"
|
6
6
|
|
7
7
|
group :test do
|
8
|
-
|
8
|
+
case ENV['DB']
|
9
|
+
when "mysql" ; gem 'mysql2' , '0.3.21'
|
10
|
+
when "postgres" ; gem 'pg', '~> 0.18'
|
11
|
+
end
|
9
12
|
gem "simplecov"
|
10
13
|
gem "codeclimate-test-reporter", "~> 1.0.0"
|
11
14
|
end
|
data/lib/find_with_order.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
require "find_with_order/version"
|
2
|
+
require "find_with_order/mysql_support"
|
3
|
+
require "find_with_order/pg_support"
|
2
4
|
require 'active_record'
|
3
5
|
|
4
6
|
class << ActiveRecord::Base
|
5
7
|
def find_with_order(ids)
|
6
8
|
return none if ids.blank?
|
7
9
|
ids = ids.uniq
|
8
|
-
return
|
10
|
+
return FindWithOrder::PGSupport.find_with_order(self, ids) if defined?(PG)
|
11
|
+
return FindWithOrder::MysqlSupport.find_with_order(self, ids)
|
9
12
|
end
|
10
13
|
def where_with_order(column, ids)
|
11
14
|
return none if ids.blank?
|
12
15
|
ids = ids.uniq
|
13
|
-
return
|
16
|
+
return FindWithOrder::PGSupport.where_with_order(self, column, ids) if defined?(PG)
|
17
|
+
return FindWithOrder::MysqlSupport.where_with_order(self, column, ids)
|
14
18
|
end
|
15
19
|
end
|
16
20
|
unless ActiveRecord::Base.respond_to?(:none) # extend only if not implement yet
|
@@ -0,0 +1,10 @@
|
|
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
|
@@ -0,0 +1,26 @@
|
|
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
|
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: 1.0
|
4
|
+
version: 1.1.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-03-
|
11
|
+
date: 2017-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,33 +53,47 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: mysql2
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '3'
|
62
|
-
type: :
|
61
|
+
version: '0.3'
|
62
|
+
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '3'
|
68
|
+
version: '0.3'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: pg
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.18'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.18'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rails
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - ">="
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
89
|
+
version: '3'
|
76
90
|
type: :runtime
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
94
|
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
96
|
+
version: '3'
|
83
97
|
description: A simple way to find records in the same order of input array. Has better
|
84
98
|
performance than manually sorting. Supports Rails 3+.
|
85
99
|
email:
|
@@ -101,6 +115,8 @@ files:
|
|
101
115
|
- gemfiles/4.2.gemfile
|
102
116
|
- gemfiles/5.0.gemfile
|
103
117
|
- lib/find_with_order.rb
|
118
|
+
- lib/find_with_order/mysql_support.rb
|
119
|
+
- lib/find_with_order/pg_support.rb
|
104
120
|
- lib/find_with_order/version.rb
|
105
121
|
homepage: https://github.com/khiav223577/find_with_order
|
106
122
|
licenses:
|
@@ -122,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
138
|
version: '0'
|
123
139
|
requirements: []
|
124
140
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
141
|
+
rubygems_version: 2.4.8
|
126
142
|
signing_key:
|
127
143
|
specification_version: 4
|
128
144
|
summary: A simple way to find records in the same order of input array.
|