pluck_all 1.0.1 → 1.1.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 +17 -3
- data/lib/pluck_all.rb +19 -0
- data/lib/pluck_all/version.rb +1 -1
- metadata +17 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e697fd5b0c59ad9243518c984b07a58be7ade969
|
4
|
+
data.tar.gz: cd1028ac15f36d590fd5ccd7cc792831942612a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2329535ea233b1f41184d33581bc2d59c5bab09a41ae7da3244df25e5c901084240bccab2ed31c5260c2164f6c705d9cc49b352ae1a4400597922ca8b437c1e
|
7
|
+
data.tar.gz: a0f77faddbea3a6725bc30387eb53b7ff8836da324924dd52cb42614206064d4f6b51f943072cb06282812329e7b44df8d2196ebb106b63089574f901dfccb4e
|
data/README.md
CHANGED
@@ -5,8 +5,13 @@
|
|
5
5
|
|
6
6
|
Plucking Multiple Columns in Rails 3. Also support in Rails 4, and Rails 5
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
This Gem standing on the shoulders of this [article](http://meltingice.net/2013/06/11/pluck-multiple-columns-rails/).
|
9
|
+
And modify it to support not only Rail 3.
|
10
|
+
|
11
|
+
If you have a Rails 3 project, and want to pluck not only one column,
|
12
|
+
feel free to use this gem and no need to worry about upgrading to Rails 4, 5 in the future will break this.
|
13
|
+
|
14
|
+
|
10
15
|
|
11
16
|
## Installation
|
12
17
|
|
@@ -25,10 +30,19 @@ Or install it yourself as:
|
|
25
30
|
$ gem install pluck_all
|
26
31
|
|
27
32
|
## Usage
|
28
|
-
|
33
|
+
### pluck to array
|
34
|
+
Behaves the same as the Rails 4 pluck, but you can use it in Rails 3
|
35
|
+
```rb
|
36
|
+
User.where(:id => [1,2]).pluck_array(:id, :account)
|
37
|
+
# => [[1, 'account1'], [2, 'account2']]
|
38
|
+
```
|
39
|
+
### pluck to hash
|
40
|
+
Similar to pluck_array, but return a hash instead.
|
29
41
|
```rb
|
30
42
|
User.where(:id => [1,2]).pluck_all(:id, :account)
|
31
43
|
# => [{"id"=>1, "account"=>"account1"}, {"id"=>2, "account"=>"account2"}]
|
44
|
+
User.where(:id => [1,2]).pluck_all('id, account AS name')
|
45
|
+
# => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
|
32
46
|
```
|
33
47
|
|
34
48
|
## Development
|
data/lib/pluck_all.rb
CHANGED
@@ -60,8 +60,27 @@ private
|
|
60
60
|
#return klass.connection.select_all(relation.arel)
|
61
61
|
end
|
62
62
|
end
|
63
|
+
|
64
|
+
|
65
|
+
class ActiveRecord::Relation
|
66
|
+
if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new('4.0.2')
|
67
|
+
def pluck_array(*args)
|
68
|
+
return pluck_all(*args).map{|hash|
|
69
|
+
result = hash.values #P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
|
70
|
+
next (result.one? ? result.first : result)
|
71
|
+
}
|
72
|
+
end
|
73
|
+
else
|
74
|
+
alias_method :pluck_array, :pluck if not method_defined?(:pluck_array)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
63
79
|
class ActiveRecord::Base
|
64
80
|
def self.pluck_all(*args)
|
65
81
|
self.where('').pluck_all(*args)
|
66
82
|
end
|
83
|
+
def self.pluck_array(*args)
|
84
|
+
self.where('').pluck_array(*args)
|
85
|
+
end
|
67
86
|
end
|
data/lib/pluck_all/version.rb
CHANGED
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluck_all
|
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: 2016-12-
|
11
|
+
date: 2016-12-21 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
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.11'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.11'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.3'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '5.0'
|
62
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
68
|
version: '5.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '3'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3'
|
83
83
|
description: If you have a rails 3 project, and want to pluck not only one column,
|
@@ -89,8 +89,8 @@ executables: []
|
|
89
89
|
extensions: []
|
90
90
|
extra_rdoc_files: []
|
91
91
|
files:
|
92
|
-
-
|
93
|
-
-
|
92
|
+
- .gitignore
|
93
|
+
- .travis.yml
|
94
94
|
- CODE_OF_CONDUCT.md
|
95
95
|
- LICENSE.txt
|
96
96
|
- README.md
|
@@ -113,17 +113,17 @@ require_paths:
|
|
113
113
|
- lib
|
114
114
|
required_ruby_version: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
|
-
- -
|
116
|
+
- - '>='
|
117
117
|
- !ruby/object:Gem::Version
|
118
118
|
version: '0'
|
119
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
120
|
requirements:
|
121
|
-
- -
|
121
|
+
- - '>='
|
122
122
|
- !ruby/object:Gem::Version
|
123
123
|
version: '0'
|
124
124
|
requirements: []
|
125
125
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
126
|
+
rubygems_version: 2.0.14
|
127
127
|
signing_key:
|
128
128
|
specification_version: 4
|
129
129
|
summary: Plucking Multiple Columns in Rails 3. Also support in Rails 4, and Rails
|