pluck_all 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,22 +1,22 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  end
9
9
 
10
10
  Rake::TestTask.new(:test_active_record) do |t|
11
- t.libs << "test"
12
- t.libs << "lib"
11
+ t.libs << 'test'
12
+ t.libs << 'lib'
13
13
  t.test_files = FileList['test/active_record/**/*_test.rb']
14
14
  end
15
15
 
16
16
  Rake::TestTask.new(:test_mongoid) do |t|
17
- t.libs << "test"
18
- t.libs << "lib"
17
+ t.libs << 'test'
18
+ t.libs << 'lib'
19
19
  t.test_files = FileList['test/mongoid/**/*_test.rb']
20
20
  end
21
21
 
22
- task :default => :test
22
+ task default: :test
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "pluck_all"
3
+ require 'bundler/setup'
4
+ require 'pluck_all'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "pluck_all"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
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/active_record_42.gemfile
7
+
8
+ # Do any other automated setup that you need to do here
@@ -2,13 +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"
6
- gem "activerecord", "~> 3.2.0"
7
- gem "carrierwave", "~> 0.11.0"
5
+ gem 'sqlite3', '~> 1.3.0'
6
+ gem 'activerecord', '~> 3.2.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
8
 
9
9
  group :test do
10
- gem "simplecov"
10
+ gem 'simplecov'
11
11
  end
12
12
 
13
- gemspec :path => "../"
14
-
13
+ gemspec path: '../'
@@ -2,13 +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"
6
- gem "activerecord", "~> 4.2.0"
7
- gem "carrierwave", "~> 0.11.0"
5
+ gem 'sqlite3', '~> 1.3.0'
6
+ gem 'activerecord', '~> 4.2.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
8
 
9
9
  group :test do
10
- gem "simplecov"
10
+ gem 'simplecov'
11
11
  end
12
12
 
13
- gemspec :path => "../"
14
-
13
+ gemspec path: '../'
@@ -2,13 +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"
6
- gem "activerecord", "~> 5.0.0"
7
- gem "carrierwave", "~> 0.11.0"
5
+ gem 'sqlite3', '~> 1.3.0'
6
+ gem 'activerecord', '~> 5.0.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
8
 
9
9
  group :test do
10
- gem "simplecov"
10
+ gem 'simplecov'
11
11
  end
12
12
 
13
- gemspec :path => "../"
14
-
13
+ gemspec path: '../'
@@ -2,13 +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"
6
- gem "activerecord", "~> 5.1.0"
7
- gem "carrierwave", "~> 0.11.0"
5
+ gem 'sqlite3', '~> 1.3.0'
6
+ gem 'activerecord', '~> 5.1.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
8
 
9
9
  group :test do
10
- gem "simplecov"
10
+ gem 'simplecov'
11
11
  end
12
12
 
13
- gemspec :path => "../"
14
-
13
+ gemspec path: '../'
@@ -2,13 +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"
6
- gem "activerecord", "~> 5.2.0"
7
- gem "carrierwave", "~> 0.11.0"
5
+ gem 'sqlite3', '~> 1.3.0'
6
+ gem 'activerecord', '~> 5.2.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
8
 
9
9
  group :test do
10
- gem "simplecov"
10
+ gem 'simplecov'
11
11
  end
12
12
 
13
- gemspec :path => "../"
14
-
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 "mongoid", "~> 5.4.0"
5
+ gem 'mongoid', '~> 5.4.0'
6
6
 
7
7
  group :test do
8
- gem "simplecov"
8
+ gem 'simplecov'
9
9
  end
10
10
 
11
- gemspec :path => "../"
12
-
11
+ 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 "mongoid", "~> 6.4.0"
5
+ gem 'mongoid', '~> 6.4.0'
6
6
 
7
7
  group :test do
8
- gem "simplecov"
8
+ gem 'simplecov'
9
9
  end
10
10
 
11
- gemspec :path => "../"
12
-
11
+ 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 "mongoid", "~> 7.0.0"
5
+ gem 'mongoid', '~> 7.0.0'
6
6
 
7
7
  group :test do
8
- gem "simplecov"
8
+ gem 'simplecov'
9
9
  end
10
10
 
11
- gemspec :path => "../"
12
-
11
+ gemspec path: '../'
@@ -1,4 +1,3 @@
1
1
  # frozen_string_literal: true
2
- require "pluck_all/version"
3
- require "pluck_all/hooks"
4
-
2
+ require 'pluck_all/version'
3
+ require 'pluck_all/hooks'
@@ -1,21 +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
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
@@ -1,142 +1,130 @@
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
- def cast_need_columns(column_names, _klass = nil)
26
- @pluck_all_cast_need_columns = column_names.map(&:to_s)
27
- @pluck_all_cast_klass = _klass
28
- return self
29
- end
30
-
31
- def select_all(*column_names)
32
- relation = clone
33
- return klass.connection.select_all(relation.select(column_names).to_sql)
34
- end
35
-
36
- if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
37
- def pluck_all(*column_names, cast_uploader_url: true)
38
- column_names.map!(&to_sql_column_name)
39
- result = select_all(*column_names)
40
- result.map! do |attributes| # This map! behaves different to array#map!
41
- initialized_attributes = klass.initialize_attributes(attributes)
42
- attributes.each do |key, attribute|
43
- attributes[key] = klass.type_cast_attribute(key, initialized_attributes) #TODO 現在AS過後的type cast會有一點問題
44
- end
45
- cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
46
- next attributes
47
- end
48
- end
49
-
50
- private
51
-
52
- def to_sql_column_name
53
- proc do |column_name|
54
- if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
55
- "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
56
- else
57
- column_name.to_s
58
- end
59
- end
60
- end
61
- else
62
- def pluck_all(*column_names, cast_uploader_url: true)
63
- column_names.map!(&to_sql_column_name)
64
- return construct_relation_for_association_calculations.pluck_all(*column_names) if has_include?(column_names.first)
65
- result = select_all(*column_names)
66
- attribute_types = klass.attribute_types
67
- result.map! do |attributes| # This map! behaves different to array#map!
68
- attributes.each do |key, attribute|
69
- attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) #TODO 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
70
- end
71
- cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
72
- next attributes
73
- end
74
- end
75
-
76
- private
77
-
78
- def to_sql_column_name
79
- proc do |column_name|
80
- if column_name.is_a?(Symbol) && attribute_alias?(column_name)
81
- attribute_alias(column_name)
82
- else
83
- column_name.to_s
84
- end
85
- end
86
- end
87
- end
88
-
89
- # ----------------------------------------------------------------
90
- # ● Support casting CarrierWave url
91
- # ----------------------------------------------------------------
92
- def cast_carrier_wave_uploader_url(attributes)
93
- if defined?(CarrierWave) && klass.respond_to?(:uploaders)
94
- @pluck_all_cast_klass ||= klass
95
- @pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, uploader| attributes.key?(key.to_s) }
96
- @pluck_all_uploaders.each do |key, uploader|
97
- {}.tap do |hash|
98
- @pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns
99
- obj = @pluck_all_cast_klass.new(hash)
100
- obj[key] = attributes[key_s = key.to_s]
101
- #https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
102
- attributes[key_s] = obj.send(key)
103
- end
104
- end
105
- end
106
- return attributes
107
- end
108
- end
109
-
110
- class ActiveRecord::Relation
111
- if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.2')
112
- def pluck_array(*args)
113
- return pluck_all(*args, cast_uploader_url: false).map{|hash|
114
- result = hash.values #P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
115
- next (args.one? ? result.first : result)
116
- }
117
- end
118
- else
119
- alias_method :pluck_array, :pluck if not method_defined?(:pluck_array)
120
- end
121
- end
122
-
123
-
124
- class << ActiveRecord::Base
125
- def cast_need_columns(*args)
126
- where(nil).cast_need_columns(*args)
127
- end
128
-
129
- def pluck_all(*args)
130
- where(nil).pluck_all(*args)
131
- end
132
-
133
- def pluck_array(*args)
134
- where(nil).pluck_array(*args)
135
- end
136
- end
137
-
138
- module ActiveRecord::NullRelation
139
- def pluck_all(*args)
140
- []
141
- end
142
- end
1
+ require_relative 'patches/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
+ relation.select_values = [].freeze # cannot use `unscope(:select)` in Rails 3
14
+ return klass.connection.select_all(relation.select(column_names).to_sql)
15
+ end
16
+
17
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
18
+ def pluck_all(*column_names, cast_uploader_url: true)
19
+ column_names.map!(&to_sql_column_name)
20
+ result = select_all(*column_names)
21
+ result.map! do |attributes| # This map! behaves different to array#map!
22
+ initialized_attributes = klass.initialize_attributes(attributes)
23
+ attributes.each do |key, _attribute|
24
+ attributes[key] = klass.type_cast_attribute(key, initialized_attributes) # TODO: 現在AS過後的type cast會有一點問題
25
+ end
26
+ cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
27
+ next attributes
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def to_sql_column_name
34
+ proc do |column_name|
35
+ if column_name.is_a?(Arel::Attributes::Attribute)
36
+ "#{column_name.relation.name}.#{column_name.name}"
37
+ elsif column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
38
+ "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
39
+ else
40
+ column_name.to_s
41
+ end
42
+ end
43
+ end
44
+ else
45
+ def pluck_all(*column_names, cast_uploader_url: true)
46
+ column_names.map!(&to_sql_column_name)
47
+ if has_include?(column_names.first)
48
+ # The `construct_relation_for_association_calculations` method was removed at Rails 5.2.
49
+ relation = Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.2.0') ? apply_join_dependency : construct_relation_for_association_calculations
50
+ return relation.pluck_all(*column_names)
51
+ end
52
+ result = select_all(*column_names)
53
+ attribute_types = klass.attribute_types
54
+ result.map! do |attributes| # This map! behaves different to array#map!
55
+ attributes.each do |key, attribute|
56
+ attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) # TODO: 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
57
+ end
58
+ cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
59
+ next attributes
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def to_sql_column_name
66
+ proc do |column_name|
67
+ if column_name.is_a?(Arel::Attributes::Attribute)
68
+ "#{column_name.relation.name}.#{column_name.name}"
69
+ elsif column_name.is_a?(Symbol) && attribute_alias?(column_name)
70
+ attribute_alias(column_name)
71
+ else
72
+ column_name.to_s
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ # ----------------------------------------------------------------
79
+ # Support casting CarrierWave url
80
+ # ----------------------------------------------------------------
81
+ def cast_carrier_wave_uploader_url(attributes)
82
+ if defined?(CarrierWave) && klass.respond_to?(:uploaders)
83
+ @pluck_all_cast_klass ||= klass
84
+ @pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, _uploader| attributes.key?(key.to_s) }
85
+ @pluck_all_uploaders.each do |key, _uploader|
86
+ {}.tap do |hash|
87
+ @pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns
88
+ obj = @pluck_all_cast_klass.new(hash)
89
+ obj[key] = attributes[key_s = key.to_s]
90
+ # https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
91
+ attributes[key_s] = obj.send(key)
92
+ end
93
+ end
94
+ end
95
+ return attributes
96
+ end
97
+ end
98
+
99
+ class ActiveRecord::Relation
100
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.2')
101
+ def pluck_array(*args)
102
+ return pluck_all(*args, cast_uploader_url: false).map do |hash|
103
+ result = hash.values # P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
104
+ next (args.one? ? result.first : result)
105
+ end
106
+ end
107
+ else
108
+ alias pluck_array pluck if not method_defined?(:pluck_array)
109
+ end
110
+ end
111
+
112
+ class << ActiveRecord::Base
113
+ def cast_need_columns(*args)
114
+ where(nil).cast_need_columns(*args)
115
+ end
116
+
117
+ def pluck_all(*args)
118
+ where(nil).pluck_all(*args)
119
+ end
120
+
121
+ def pluck_array(*args)
122
+ where(nil).pluck_array(*args)
123
+ end
124
+ end
125
+
126
+ module ActiveRecord::NullRelation
127
+ def pluck_all(*_args)
128
+ []
129
+ end
130
+ end