pluck_all 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.travis.yml +31 -7
- data/CHANGELOG.md +3 -0
- data/README.md +2 -2
- data/Rakefile +12 -0
- data/gemfiles/{3.2.gemfile → active_record_32.gemfile} +2 -2
- data/gemfiles/{4.2.gemfile → active_record_42.gemfile} +2 -2
- data/gemfiles/{5.0.gemfile → active_record_50.gemfile} +2 -2
- data/gemfiles/{5.1.gemfile → active_record_51.gemfile} +2 -2
- data/gemfiles/{5.2.gemfile → active_record_52.gemfile} +2 -2
- data/gemfiles/{no_mongoid.gemfile → mongoid_54.gemfile} +1 -2
- data/gemfiles/mongoid_64.gemfile +12 -0
- data/gemfiles/mongoid_70.gemfile +12 -0
- data/lib/pluck_all.rb +2 -130
- data/lib/pluck_all/hooks.rb +21 -0
- data/lib/pluck_all/models/active_record_extension.rb +125 -0
- data/lib/pluck_all/{mongoid_pluck_all.rb → models/mongoid_extension.rb} +23 -9
- data/lib/pluck_all/version.rb +1 -1
- data/pluck_all.gemspec +2 -4
- metadata +26 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f4e9c99236d4d19b7bce24b3d03f816856c12bf2
|
4
|
+
data.tar.gz: 32b12e58e910477f6b07309cbd36517ceeb1bc03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 609f211a5cec23ea23ed82e9e817eb65fbbe8dba4e06773b1cb996deaa0672aba6f5b5d93fb895701c952c4486a1ee7331fa039f4580ee46d099843314612867
|
7
|
+
data.tar.gz: 2700b74ec0c40ba5cc8c3deb69ac432b9cd7857c1de01a91efdf466cf539888418eefa8fe0b7d5d1f836e9d6a6382a16a32ff8afb652d3854c1a8fa93eea6d51
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -8,13 +8,36 @@ rvm:
|
|
8
8
|
- 2.3
|
9
9
|
services:
|
10
10
|
- mongodb
|
11
|
+
env:
|
12
|
+
- ORM_TYPE=ACTIVE_RECORD
|
13
|
+
- ORM_TYPE=MONGOID
|
11
14
|
gemfile:
|
12
|
-
- gemfiles/
|
13
|
-
- gemfiles/
|
14
|
-
- gemfiles/
|
15
|
-
- gemfiles/
|
16
|
-
- gemfiles/
|
17
|
-
- gemfiles/
|
15
|
+
- gemfiles/active_record_32.gemfile
|
16
|
+
- gemfiles/active_record_42.gemfile
|
17
|
+
- gemfiles/active_record_50.gemfile
|
18
|
+
- gemfiles/active_record_51.gemfile
|
19
|
+
- gemfiles/active_record_52.gemfile
|
20
|
+
- gemfiles/mongoid_54.gemfile
|
21
|
+
- gemfiles/mongoid_64.gemfile
|
22
|
+
- gemfiles/mongoid_70.gemfile
|
23
|
+
matrix:
|
24
|
+
exclude:
|
25
|
+
- gemfile: gemfiles/active_record_32.gemfile
|
26
|
+
env: ORM_TYPE=MONGOID
|
27
|
+
- gemfile: gemfiles/active_record_42.gemfile
|
28
|
+
env: ORM_TYPE=MONGOID
|
29
|
+
- gemfile: gemfiles/active_record_50.gemfile
|
30
|
+
env: ORM_TYPE=MONGOID
|
31
|
+
- gemfile: gemfiles/active_record_51.gemfile
|
32
|
+
env: ORM_TYPE=MONGOID
|
33
|
+
- gemfile: gemfiles/active_record_52.gemfile
|
34
|
+
env: ORM_TYPE=MONGOID
|
35
|
+
- gemfile: gemfiles/mongoid_54.gemfile
|
36
|
+
env: ORM_TYPE=ACTIVE_RECORD
|
37
|
+
- gemfile: gemfiles/mongoid_64.gemfile
|
38
|
+
env: ORM_TYPE=ACTIVE_RECORD
|
39
|
+
- gemfile: gemfiles/mongoid_70.gemfile
|
40
|
+
env: ORM_TYPE=ACTIVE_RECORD
|
18
41
|
before_install:
|
19
42
|
- gem install bundler
|
20
43
|
- gem update --system
|
@@ -23,6 +46,7 @@ before_install:
|
|
23
46
|
- chmod +x ./cc-test-reporter
|
24
47
|
- ./cc-test-reporter before-build
|
25
48
|
script:
|
26
|
-
- bundle exec rake
|
49
|
+
- if [ "$ORM_TYPE" = "ACTIVE_RECORD" ]; then bundle exec rake test_active_record; fi
|
50
|
+
- if [ "$ORM_TYPE" = "MONGOID" ]; then bundle exec rake test_mongoid; fi
|
27
51
|
after_script:
|
28
52
|
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
## Change Log
|
2
2
|
|
3
|
+
### [v2.0.1](https://github.com/khiav223577/pluck_all/compare/v2.0.0...v2.0.1) 2018/05/27
|
4
|
+
- [#25](https://github.com/khiav223577/pluck_all/pull/25) Fix that project without mongoid will raise LoadError (@khiav223577)
|
5
|
+
|
3
6
|
### [v2.0.0](https://github.com/khiav223577/pluck_all/compare/v1.2.4...v2.0.0) 2018/05/27
|
4
7
|
- [#24](https://github.com/khiav223577/pluck_all/pull/24) Support Mongoid! (@khiav223577)
|
5
8
|
- [#23](https://github.com/khiav223577/pluck_all/pull/23) Refactoring Coding Style (@khiav223577)
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
[![Code Climate](https://codeclimate.com/github/khiav223577/pluck_all/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/pluck_all)
|
7
7
|
[![Test Coverage](https://codeclimate.com/github/khiav223577/pluck_all/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/pluck_all/coverage)
|
8
8
|
|
9
|
-
Pluck multiple columns/attributes in Rails 3, 4, 5, and can return data as hash instead of only array.
|
9
|
+
Pluck multiple columns/attributes in Rails 3, 4, 5, and can return data as hash instead of only array. Also supports Mongoid.
|
10
10
|
|
11
11
|
This Gem stands on the shoulders of this article: [Plucking Multiple Columns in Rails 3](http://meltingice.net/2013/06/11/pluck-multiple-columns-rails/).
|
12
12
|
And modified to support not only Rail 3.
|
@@ -115,7 +115,7 @@ User.where(xxx).cast_need_columns(%i(id, name)).pluck_all(:id, :name, :profile_p
|
|
115
115
|
|
116
116
|
## Development
|
117
117
|
|
118
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake
|
118
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test_active_record` or `rake test_mongoid` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
119
119
|
|
120
120
|
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).
|
121
121
|
|
data/Rakefile
CHANGED
@@ -7,4 +7,16 @@ Rake::TestTask.new(:test) do |t|
|
|
7
7
|
t.test_files = FileList['test/**/*_test.rb']
|
8
8
|
end
|
9
9
|
|
10
|
+
Rake::TestTask.new(:test_active_record) do |t|
|
11
|
+
t.libs << "test"
|
12
|
+
t.libs << "lib"
|
13
|
+
t.test_files = FileList['test/active_record/**/*_test.rb']
|
14
|
+
end
|
15
|
+
|
16
|
+
Rake::TestTask.new(:test_mongoid) do |t|
|
17
|
+
t.libs << "test"
|
18
|
+
t.libs << "lib"
|
19
|
+
t.test_files = FileList['test/mongoid/**/*_test.rb']
|
20
|
+
end
|
21
|
+
|
10
22
|
task :default => :test
|
@@ -2,12 +2,12 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pluck_all.gemspec
|
4
4
|
|
5
|
+
gem "sqlite3", "~> 1.3.0"
|
5
6
|
gem "activerecord", "~> 3.2.0"
|
7
|
+
gem "carrierwave", "~> 0.11.0"
|
6
8
|
|
7
9
|
group :test do
|
8
10
|
gem "simplecov"
|
9
|
-
gem 'mongoid', '~> 3.1.7'
|
10
|
-
gem 'carrierwave', '~> 0.11.0'
|
11
11
|
end
|
12
12
|
|
13
13
|
gemspec :path => "../"
|
@@ -2,12 +2,12 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pluck_all.gemspec
|
4
4
|
|
5
|
+
gem "sqlite3", "~> 1.3.0"
|
5
6
|
gem "activerecord", "~> 4.2.0"
|
7
|
+
gem "carrierwave", "~> 0.11.0"
|
6
8
|
|
7
9
|
group :test do
|
8
10
|
gem "simplecov"
|
9
|
-
gem 'mongoid', '~> 5.4.0'
|
10
|
-
gem 'carrierwave', '~> 0.11.0'
|
11
11
|
end
|
12
12
|
|
13
13
|
gemspec :path => "../"
|
@@ -2,12 +2,12 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pluck_all.gemspec
|
4
4
|
|
5
|
+
gem "sqlite3", "~> 1.3.0"
|
5
6
|
gem "activerecord", "~> 5.0.0"
|
7
|
+
gem "carrierwave", "~> 0.11.0"
|
6
8
|
|
7
9
|
group :test do
|
8
10
|
gem "simplecov"
|
9
|
-
gem 'mongoid', '~> 6.0.3'
|
10
|
-
gem 'carrierwave', '~> 0.11.0'
|
11
11
|
end
|
12
12
|
|
13
13
|
gemspec :path => "../"
|
@@ -2,12 +2,12 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pluck_all.gemspec
|
4
4
|
|
5
|
+
gem "sqlite3", "~> 1.3.0"
|
5
6
|
gem "activerecord", "~> 5.1.0"
|
7
|
+
gem "carrierwave", "~> 0.11.0"
|
6
8
|
|
7
9
|
group :test do
|
8
10
|
gem "simplecov"
|
9
|
-
gem 'mongoid', '~> 7.0.1'
|
10
|
-
gem 'carrierwave', '~> 0.11.0'
|
11
11
|
end
|
12
12
|
|
13
13
|
gemspec :path => "../"
|
@@ -2,12 +2,12 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pluck_all.gemspec
|
4
4
|
|
5
|
+
gem "sqlite3", "~> 1.3.0"
|
5
6
|
gem "activerecord", "~> 5.2.0"
|
7
|
+
gem "carrierwave", "~> 0.11.0"
|
6
8
|
|
7
9
|
group :test do
|
8
10
|
gem "simplecov"
|
9
|
-
gem 'mongoid', '~> 7.0.1'
|
10
|
-
gem 'carrierwave', '~> 0.11.0'
|
11
11
|
end
|
12
12
|
|
13
13
|
gemspec :path => "../"
|
@@ -2,11 +2,10 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pluck_all.gemspec
|
4
4
|
|
5
|
-
gem "
|
5
|
+
gem "mongoid", "~> 5.4.0"
|
6
6
|
|
7
7
|
group :test do
|
8
8
|
gem "simplecov"
|
9
|
-
gem 'carrierwave', '~> 0.11.0'
|
10
9
|
end
|
11
10
|
|
12
11
|
gemspec :path => "../"
|
data/lib/pluck_all.rb
CHANGED
@@ -1,132 +1,4 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require
|
3
|
-
require
|
4
|
-
begin
|
5
|
-
require 'mongoid'
|
6
|
-
require 'pluck_all/mongoid_pluck_all'
|
7
|
-
rescue LoadError, Gem::LoadError
|
2
|
+
require "pluck_all/version"
|
3
|
+
require "pluck_all/hooks"
|
8
4
|
|
9
|
-
end
|
10
|
-
|
11
|
-
class ActiveRecord::Base
|
12
|
-
if !defined?(attribute_types) && defined?(column_types)
|
13
|
-
class << self
|
14
|
-
# column_types was changed to attribute_types in Rails 5
|
15
|
-
alias_method :attribute_types, :column_types
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
module ActiveRecord
|
21
|
-
[
|
22
|
-
*([Type::Value, Type::Integer, Type::Serialized] if defined?(Type::Value)),
|
23
|
-
*([Enum::EnumType] if defined?(Enum::EnumType)),
|
24
|
-
].each do |s|
|
25
|
-
s.class_eval do
|
26
|
-
if !method_defined?(:deserialize) && method_defined?(:type_cast_from_database)
|
27
|
-
# column_types was changed to attribute_types in Rails 5
|
28
|
-
alias deserialize type_cast_from_database
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class ActiveRecord::Relation
|
35
|
-
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
|
36
|
-
def pluck_all(*args)
|
37
|
-
result = select_all(*args)
|
38
|
-
result.map! do |attributes| # This map! behaves different to array#map!
|
39
|
-
initialized_attributes = klass.initialize_attributes(attributes)
|
40
|
-
attributes.each do |key, attribute|
|
41
|
-
attributes[key] = klass.type_cast_attribute(key, initialized_attributes) #TODO 現在AS過後的type cast會有一點問題
|
42
|
-
end
|
43
|
-
cast_carrier_wave_uploader_url(attributes)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
else
|
47
|
-
def pluck_all(*args)
|
48
|
-
result = select_all(*args)
|
49
|
-
attribute_types = klass.attribute_types
|
50
|
-
result.map! do |attributes| # This map! behaves different to array#map!
|
51
|
-
attributes.each do |key, attribute|
|
52
|
-
attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) #TODO 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
|
53
|
-
end
|
54
|
-
cast_carrier_wave_uploader_url(attributes)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def cast_need_columns(column_names, _klass = nil)
|
60
|
-
@pluck_all_cast_need_columns = column_names.map(&:to_s)
|
61
|
-
@pluck_all_cast_klass = _klass
|
62
|
-
return self
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
def select_all(*args)
|
68
|
-
args.map! do |column_name|
|
69
|
-
if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
|
70
|
-
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
|
71
|
-
else
|
72
|
-
column_name.to_s
|
73
|
-
end
|
74
|
-
end
|
75
|
-
relation = clone
|
76
|
-
return klass.connection.select_all(relation.select(args).to_sql)
|
77
|
-
#return klass.connection.select_all(relation.arel)
|
78
|
-
end
|
79
|
-
|
80
|
-
# ----------------------------------------------------------------
|
81
|
-
# ● Support casting CarrierWave url
|
82
|
-
# ----------------------------------------------------------------
|
83
|
-
def cast_carrier_wave_uploader_url(attributes)
|
84
|
-
if defined?(CarrierWave) && klass.respond_to?(:uploaders)
|
85
|
-
@pluck_all_cast_klass ||= klass
|
86
|
-
@pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, uploader| attributes.key?(key.to_s) }
|
87
|
-
@pluck_all_uploaders.each do |key, uploader|
|
88
|
-
hash = {}
|
89
|
-
@pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns
|
90
|
-
obj = @pluck_all_cast_klass.new(hash)
|
91
|
-
obj[key] = attributes[key_s = key.to_s]
|
92
|
-
#https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
|
93
|
-
attributes[key_s] = obj.send(key)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
return attributes
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
class ActiveRecord::Relation
|
101
|
-
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.2')
|
102
|
-
def pluck_array(*args)
|
103
|
-
return pluck_all(*args).map{|hash|
|
104
|
-
result = hash.values #P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
|
105
|
-
next (result.one? ? result.first : result)
|
106
|
-
}
|
107
|
-
end
|
108
|
-
else
|
109
|
-
alias_method :pluck_array, :pluck if not method_defined?(:pluck_array)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
|
114
|
-
class << ActiveRecord::Base
|
115
|
-
def cast_need_columns(*args)
|
116
|
-
where(nil).cast_need_columns(*args)
|
117
|
-
end
|
118
|
-
|
119
|
-
def pluck_all(*args)
|
120
|
-
where(nil).pluck_all(*args)
|
121
|
-
end
|
122
|
-
|
123
|
-
def pluck_array(*args)
|
124
|
-
where(nil).pluck_array(*args)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
module ActiveRecord::NullRelation
|
129
|
-
def pluck_all(*args)
|
130
|
-
[]
|
131
|
-
end
|
132
|
-
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module PluckAll
|
2
|
+
class Hooks
|
3
|
+
def self.init
|
4
|
+
# ActiveRecord
|
5
|
+
begin
|
6
|
+
require 'active_record'
|
7
|
+
require 'pluck_all/models/active_record_extension'
|
8
|
+
rescue LoadError, Gem::LoadError
|
9
|
+
end
|
10
|
+
|
11
|
+
# Mongoid
|
12
|
+
begin
|
13
|
+
require 'mongoid'
|
14
|
+
require 'pluck_all/models/mongoid_extension'
|
15
|
+
rescue LoadError, Gem::LoadError
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
PluckAll::Hooks.init
|
@@ -0,0 +1,125 @@
|
|
1
|
+
class ActiveRecord::Base
|
2
|
+
if !defined?(attribute_types) && defined?(column_types)
|
3
|
+
class << self
|
4
|
+
# column_types was changed to attribute_types in Rails 5
|
5
|
+
alias_method :attribute_types, :column_types
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActiveRecord
|
11
|
+
[
|
12
|
+
*([Type::Value, Type::Integer, Type::Serialized] if defined?(Type::Value)),
|
13
|
+
*([Enum::EnumType] if defined?(Enum::EnumType)),
|
14
|
+
].each do |s|
|
15
|
+
s.class_eval do
|
16
|
+
if !method_defined?(:deserialize) && method_defined?(:type_cast_from_database)
|
17
|
+
# column_types was changed to attribute_types in Rails 5
|
18
|
+
alias deserialize type_cast_from_database
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class ActiveRecord::Relation
|
25
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
|
26
|
+
def pluck_all(*args, cast_uploader_url: true)
|
27
|
+
result = select_all(*args)
|
28
|
+
result.map! do |attributes| # This map! behaves different to array#map!
|
29
|
+
initialized_attributes = klass.initialize_attributes(attributes)
|
30
|
+
attributes.each do |key, attribute|
|
31
|
+
attributes[key] = klass.type_cast_attribute(key, initialized_attributes) #TODO 現在AS過後的type cast會有一點問題
|
32
|
+
end
|
33
|
+
cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
|
34
|
+
next attributes
|
35
|
+
end
|
36
|
+
end
|
37
|
+
else
|
38
|
+
def pluck_all(*args, cast_uploader_url: true)
|
39
|
+
result = select_all(*args)
|
40
|
+
attribute_types = klass.attribute_types
|
41
|
+
result.map! do |attributes| # This map! behaves different to array#map!
|
42
|
+
attributes.each do |key, attribute|
|
43
|
+
attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) #TODO 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
|
44
|
+
end
|
45
|
+
cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
|
46
|
+
next attributes
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def cast_need_columns(column_names, _klass = nil)
|
52
|
+
@pluck_all_cast_need_columns = column_names.map(&:to_s)
|
53
|
+
@pluck_all_cast_klass = _klass
|
54
|
+
return self
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def select_all(*args)
|
60
|
+
args.map! do |column_name|
|
61
|
+
if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
|
62
|
+
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
|
63
|
+
else
|
64
|
+
column_name.to_s
|
65
|
+
end
|
66
|
+
end
|
67
|
+
relation = clone
|
68
|
+
return klass.connection.select_all(relation.select(args).to_sql)
|
69
|
+
#return klass.connection.select_all(relation.arel)
|
70
|
+
end
|
71
|
+
|
72
|
+
# ----------------------------------------------------------------
|
73
|
+
# ● Support casting CarrierWave url
|
74
|
+
# ----------------------------------------------------------------
|
75
|
+
def cast_carrier_wave_uploader_url(attributes)
|
76
|
+
if defined?(CarrierWave) && klass.respond_to?(:uploaders)
|
77
|
+
@pluck_all_cast_klass ||= klass
|
78
|
+
@pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, uploader| attributes.key?(key.to_s) }
|
79
|
+
@pluck_all_uploaders.each do |key, uploader|
|
80
|
+
{}.tap do |hash|
|
81
|
+
@pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns
|
82
|
+
obj = @pluck_all_cast_klass.new(hash)
|
83
|
+
obj[key] = attributes[key_s = key.to_s]
|
84
|
+
#https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
|
85
|
+
attributes[key_s] = obj.send(key)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
return attributes
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class ActiveRecord::Relation
|
94
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.2')
|
95
|
+
def pluck_array(*args)
|
96
|
+
return pluck_all(*args, cast_uploader_url: false).map{|hash|
|
97
|
+
result = hash.values #P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
|
98
|
+
next (args.one? ? result.first : result)
|
99
|
+
}
|
100
|
+
end
|
101
|
+
else
|
102
|
+
alias_method :pluck_array, :pluck if not method_defined?(:pluck_array)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
class << ActiveRecord::Base
|
108
|
+
def cast_need_columns(*args)
|
109
|
+
where(nil).cast_need_columns(*args)
|
110
|
+
end
|
111
|
+
|
112
|
+
def pluck_all(*args)
|
113
|
+
where(nil).pluck_all(*args)
|
114
|
+
end
|
115
|
+
|
116
|
+
def pluck_array(*args)
|
117
|
+
where(nil).pluck_array(*args)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
module ActiveRecord::NullRelation
|
122
|
+
def pluck_all(*args)
|
123
|
+
[]
|
124
|
+
end
|
125
|
+
end
|
@@ -1,5 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module Mongoid
|
3
|
+
module Document::ClassMethods
|
4
|
+
def pluck_array(*fields)
|
5
|
+
where(nil).pluck_array(*fields)
|
6
|
+
end
|
7
|
+
|
8
|
+
def pluck_all(*fields)
|
9
|
+
where(nil).pluck_all(*fields)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
module Findable
|
4
14
|
delegate :pluck_all, :pluck_array, to: :with_default_scope
|
5
15
|
end
|
@@ -21,9 +31,7 @@ module Mongoid
|
|
21
31
|
def pluck_array(*fields)
|
22
32
|
normalized_select = get_normalized_select(fields)
|
23
33
|
get_query_data(normalized_select).reduce([]) do |plucked, doc|
|
24
|
-
values = normalized_select.keys.map
|
25
|
-
n =~ /\./ ? doc[n.partition('.')[0]] : doc[n]
|
26
|
-
end
|
34
|
+
values = normalized_select.keys.map(&plucked_value_mapper(:array, doc))
|
27
35
|
plucked << (values.size == 1 ? values.first : values)
|
28
36
|
end
|
29
37
|
end
|
@@ -31,23 +39,29 @@ module Mongoid
|
|
31
39
|
def pluck_all(*fields)
|
32
40
|
normalized_select = get_normalized_select(fields)
|
33
41
|
get_query_data(normalized_select).reduce([]) do |plucked, doc|
|
34
|
-
values = normalized_select.keys.map
|
35
|
-
|
36
|
-
end.to_h
|
37
|
-
plucked << values
|
42
|
+
values = normalized_select.keys.map(&plucked_value_mapper(:all, doc))
|
43
|
+
plucked << values.to_h
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
41
47
|
private
|
42
48
|
|
49
|
+
def plucked_value_mapper(type, doc)
|
50
|
+
Proc.new do |n|
|
51
|
+
values = [n, n =~ /\./ ? doc[n.partition('.')[0]] : doc[n]]
|
52
|
+
case type
|
53
|
+
when :array then values[1]
|
54
|
+
when :all then values
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
43
58
|
def get_query_data(normalized_select)
|
44
59
|
return (@view ? @view.projection(normalized_select) : query.dup.select(normalized_select))
|
45
60
|
end
|
46
61
|
|
47
62
|
def get_normalized_select(fields)
|
48
|
-
|
63
|
+
fields.each_with_object({}) do |f, hash|
|
49
64
|
hash[klass.database_field_name(f)] = 1
|
50
|
-
hash
|
51
65
|
end
|
52
66
|
end
|
53
67
|
end
|
data/lib/pluck_all/version.rb
CHANGED
data/pluck_all.gemspec
CHANGED
@@ -27,11 +27,9 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
+
spec.add_dependency "activesupport", ">= 3.0.0"
|
31
|
+
|
30
32
|
spec.add_development_dependency "bundler", "~> 1.11"
|
31
33
|
spec.add_development_dependency "rake", "~> 12.0"
|
32
|
-
spec.add_development_dependency "sqlite3", "~> 1.3"
|
33
34
|
spec.add_development_dependency "minitest", "~> 5.0"
|
34
|
-
|
35
|
-
spec.add_dependency "activerecord", ">= 3"
|
36
|
-
|
37
35
|
end
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluck_all
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
type: :
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 3.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '1.11'
|
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
|
-
version: '
|
40
|
+
version: '1.11'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '12.0'
|
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
|
-
version: '
|
54
|
+
version: '12.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,20 +66,6 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '5.0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: activerecord
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '3'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '3'
|
83
69
|
description: Pluck multiple columns/attributes and return array of hashes. Support
|
84
70
|
Rails 3, 4, 5. If you have a Rails 3 project, and want to pluck not only one column,
|
85
71
|
feel free to use this gem and no need to worry about upgrading to Rails 4, 5 in
|
@@ -99,14 +85,18 @@ files:
|
|
99
85
|
- Rakefile
|
100
86
|
- bin/console
|
101
87
|
- bin/setup
|
102
|
-
- gemfiles/
|
103
|
-
- gemfiles/
|
104
|
-
- gemfiles/
|
105
|
-
- gemfiles/
|
106
|
-
- gemfiles/
|
107
|
-
- gemfiles/
|
88
|
+
- gemfiles/active_record_32.gemfile
|
89
|
+
- gemfiles/active_record_42.gemfile
|
90
|
+
- gemfiles/active_record_50.gemfile
|
91
|
+
- gemfiles/active_record_51.gemfile
|
92
|
+
- gemfiles/active_record_52.gemfile
|
93
|
+
- gemfiles/mongoid_54.gemfile
|
94
|
+
- gemfiles/mongoid_64.gemfile
|
95
|
+
- gemfiles/mongoid_70.gemfile
|
108
96
|
- lib/pluck_all.rb
|
109
|
-
- lib/pluck_all/
|
97
|
+
- lib/pluck_all/hooks.rb
|
98
|
+
- lib/pluck_all/models/active_record_extension.rb
|
99
|
+
- lib/pluck_all/models/mongoid_extension.rb
|
110
100
|
- lib/pluck_all/version.rb
|
111
101
|
- pluck_all.gemspec
|
112
102
|
homepage: https://github.com/khiav223577/pluck_all
|
@@ -129,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
119
|
version: '0'
|
130
120
|
requirements: []
|
131
121
|
rubyforge_project:
|
132
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.6.13
|
133
123
|
signing_key:
|
134
124
|
specification_version: 4
|
135
125
|
summary: Pluck multiple columns/attributes and return array of hashes. Support Rails
|