pluck_all 2.2.1 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +101 -0
- data/.gitignore +10 -10
- data/.rubocop.yml +1227 -1227
- data/CHANGELOG.md +73 -70
- data/CODE_OF_CONDUCT.md +48 -48
- data/LICENSE.txt +21 -21
- data/README.md +187 -167
- data/bin/console +0 -0
- data/bin/setup +8 -8
- data/gemfiles/active_record_32.gemfile +14 -13
- data/gemfiles/active_record_42.gemfile +16 -13
- data/gemfiles/active_record_50.gemfile +16 -13
- data/gemfiles/active_record_51.gemfile +16 -13
- data/gemfiles/active_record_52.gemfile +16 -13
- data/gemfiles/active_record_60.gemfile +16 -13
- data/gemfiles/active_record_61.gemfile +16 -13
- data/gemfiles/mongoid_54.gemfile +11 -11
- data/gemfiles/mongoid_64.gemfile +11 -11
- data/gemfiles/mongoid_70.gemfile +11 -11
- data/lib/pluck_all/models/active_record_extension.rb +139 -131
- data/lib/pluck_all/version.rb +4 -4
- metadata +4 -4
- data/.travis.yml +0 -73
data/bin/console
CHANGED
File without changes
|
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/active_record_42.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/active_record_42.gemfile
|
7
|
+
|
8
|
+
# Do any other automated setup that you need to do here
|
@@ -1,13 +1,14 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 3.2.0'
|
7
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 3.2.0'
|
7
|
+
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem 'simplecov', '< 0.18'
|
12
|
+
end
|
13
|
+
|
14
|
+
gemspec path: '../'
|
@@ -1,13 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 4.2.0'
|
7
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 4.2.0'
|
7
|
+
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
9
|
+
|
10
|
+
gem 'globalize'
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov', '< 0.18'
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec path: '../'
|
@@ -1,13 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 5.0.0'
|
7
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 5.0.0'
|
7
|
+
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
9
|
+
|
10
|
+
gem 'globalize'
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov', '< 0.18'
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec path: '../'
|
@@ -1,13 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 5.1.0'
|
7
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 5.1.0'
|
7
|
+
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
9
|
+
|
10
|
+
gem 'globalize'
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov', '< 0.18'
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec path: '../'
|
@@ -1,13 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 5.2.0'
|
7
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 5.2.0'
|
7
|
+
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
9
|
+
|
10
|
+
gem 'globalize'
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov', '< 0.18'
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec path: '../'
|
@@ -1,13 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.4.1'
|
6
|
-
gem 'activerecord', '~> 6.0.0'
|
7
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.4.1'
|
6
|
+
gem 'activerecord', '~> 6.0.0'
|
7
|
+
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
9
|
+
|
10
|
+
gem 'globalize'
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov', '< 0.18'
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec path: '../'
|
@@ -1,13 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.4.1'
|
6
|
-
gem 'activerecord', '~> 6.1.0'
|
7
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.4.1'
|
6
|
+
gem 'activerecord', '~> 6.1.0'
|
7
|
+
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
9
|
+
|
10
|
+
gem 'globalize'
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov', '< 0.18'
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec path: '../'
|
data/gemfiles/mongoid_54.gemfile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'mongoid', '~> 5.4.0'
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'simplecov'
|
9
|
-
end
|
10
|
-
|
11
|
-
gemspec path: '../'
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'mongoid', '~> 5.4.0'
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'simplecov', '< 0.18'
|
9
|
+
end
|
10
|
+
|
11
|
+
gemspec path: '../'
|
data/gemfiles/mongoid_64.gemfile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'mongoid', '~> 6.4.0'
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'simplecov'
|
9
|
-
end
|
10
|
-
|
11
|
-
gemspec path: '../'
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'mongoid', '~> 6.4.0'
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'simplecov', '< 0.18'
|
9
|
+
end
|
10
|
+
|
11
|
+
gemspec path: '../'
|
data/gemfiles/mongoid_70.gemfile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'mongoid', '~> 7.0.0'
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'simplecov'
|
9
|
-
end
|
10
|
-
|
11
|
-
gemspec path: '../'
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'mongoid', '~> 7.0.0'
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'simplecov', '< 0.18'
|
9
|
+
end
|
10
|
+
|
11
|
+
gemspec path: '../'
|
@@ -1,131 +1,139 @@
|
|
1
|
-
require 'rails_compatibility/attribute_types'
|
2
|
-
require_relative 'patches/deserialize'
|
3
|
-
|
4
|
-
class ActiveRecord::Relation
|
5
|
-
def cast_need_columns(column_names, _klass = nil)
|
6
|
-
@pluck_all_cast_need_columns = column_names.map(&:to_s)
|
7
|
-
@pluck_all_cast_klass = _klass
|
8
|
-
return self
|
9
|
-
end
|
10
|
-
|
11
|
-
def select_all(
|
12
|
-
relation = clone
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
def
|
123
|
-
where(nil).
|
124
|
-
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
1
|
+
require 'rails_compatibility/attribute_types'
|
2
|
+
require_relative 'patches/deserialize'
|
3
|
+
|
4
|
+
class ActiveRecord::Relation
|
5
|
+
def cast_need_columns(column_names, _klass = nil)
|
6
|
+
@pluck_all_cast_need_columns = column_names.map(&:to_s)
|
7
|
+
@pluck_all_cast_klass = _klass
|
8
|
+
return self
|
9
|
+
end
|
10
|
+
|
11
|
+
def select_all(column_names)
|
12
|
+
relation = clone
|
13
|
+
|
14
|
+
# See: https://github.com/globalize/globalize/pull/707
|
15
|
+
if respond_to?(:translated_attribute_names) && (parsed = parse_translated_columns(column_names))
|
16
|
+
relation = relation.join_translations
|
17
|
+
column_names = parsed
|
18
|
+
end
|
19
|
+
|
20
|
+
relation.select_values = [].freeze # cannot use `unscope(:select)` in Rails 3
|
21
|
+
|
22
|
+
sql = relation.select(column_names.map(&to_sql_column_name)).to_sql
|
23
|
+
return klass.connection.select_all(sql)
|
24
|
+
end
|
25
|
+
|
26
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
|
27
|
+
def pluck_all(*column_names, cast_uploader_url: true)
|
28
|
+
result = select_all(column_names)
|
29
|
+
result.map! do |attributes| # This map! behaves different to array#map!
|
30
|
+
initialized_attributes = klass.initialize_attributes(attributes)
|
31
|
+
attributes.each do |key, _attribute|
|
32
|
+
attributes[key] = klass.type_cast_attribute(key, initialized_attributes) # TODO: 現在AS過後的type cast會有一點問題
|
33
|
+
end
|
34
|
+
cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
|
35
|
+
next attributes
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def to_sql_column_name
|
42
|
+
proc do |column_name|
|
43
|
+
if column_name.is_a?(Arel::Attributes::Attribute)
|
44
|
+
"#{column_name.relation.name}.#{column_name.name}"
|
45
|
+
elsif column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
|
46
|
+
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
|
47
|
+
else
|
48
|
+
column_name.to_s
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
def pluck_all(*column_names, cast_uploader_url: true)
|
54
|
+
if has_include?(column_names.first)
|
55
|
+
# The `construct_relation_for_association_calculations` method was removed at Rails 5.2.
|
56
|
+
relation = Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.2.0') ? apply_join_dependency : construct_relation_for_association_calculations
|
57
|
+
return relation.pluck_all(*column_names)
|
58
|
+
end
|
59
|
+
|
60
|
+
result = select_all(column_names)
|
61
|
+
attribute_types = RailsCompatibility.attribute_types(klass)
|
62
|
+
result.map do |attributes| # This map behaves different to array#map
|
63
|
+
attributes.each do |key, attribute|
|
64
|
+
attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) # TODO: 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
|
65
|
+
end
|
66
|
+
cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
|
67
|
+
next attributes
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def to_sql_column_name
|
74
|
+
proc do |column_name|
|
75
|
+
if column_name.is_a?(Arel::Attributes::Attribute)
|
76
|
+
"#{column_name.relation.name}.#{column_name.name}"
|
77
|
+
elsif column_name.is_a?(Symbol) && attribute_alias?(column_name)
|
78
|
+
attribute_alias(column_name)
|
79
|
+
else
|
80
|
+
column_name.to_s
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# ----------------------------------------------------------------
|
87
|
+
# ● Support casting CarrierWave url
|
88
|
+
# ----------------------------------------------------------------
|
89
|
+
def cast_carrier_wave_uploader_url(attributes)
|
90
|
+
if defined?(CarrierWave) && klass.respond_to?(:uploaders)
|
91
|
+
@pluck_all_cast_need_columns ||= nil
|
92
|
+
@pluck_all_cast_klass ||= klass
|
93
|
+
@pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, _uploader| attributes.key?(key.to_s) }
|
94
|
+
@pluck_all_uploaders.each do |key, _uploader|
|
95
|
+
{}.tap do |hash|
|
96
|
+
@pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns
|
97
|
+
obj = @pluck_all_cast_klass.instantiate(hash)
|
98
|
+
obj[key] = attributes[key_s = key.to_s]
|
99
|
+
# https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
|
100
|
+
attributes[key_s] = obj.send(key)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
return attributes
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class ActiveRecord::Relation
|
109
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.2')
|
110
|
+
def pluck_array(*args)
|
111
|
+
return pluck_all(*args, cast_uploader_url: false).map do |hash|
|
112
|
+
result = hash.values # P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
|
113
|
+
next (args.one? ? result.first : result)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
else
|
117
|
+
alias pluck_array pluck if not method_defined?(:pluck_array)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class << ActiveRecord::Base
|
122
|
+
def cast_need_columns(*args)
|
123
|
+
where(nil).cast_need_columns(*args)
|
124
|
+
end
|
125
|
+
|
126
|
+
def pluck_all(*args)
|
127
|
+
where(nil).pluck_all(*args)
|
128
|
+
end
|
129
|
+
|
130
|
+
def pluck_array(*args)
|
131
|
+
where(nil).pluck_array(*args)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
module ActiveRecord::NullRelation
|
136
|
+
def pluck_all(*_args)
|
137
|
+
[]
|
138
|
+
end
|
139
|
+
end
|