pluck_all 2.0.4 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,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
+
9
+ group :test do
10
+ gem 'simplecov'
11
+ end
12
+
13
+ gemspec path: '../'
@@ -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
+ class << self
4
+ def init
5
+ require 'pluck_all/models/active_record_extension' if require_if_exists('active_record')
6
+ require 'pluck_all/models/mongoid_extension' if require_if_exists('mongoid')
7
+ end
8
+
9
+ private
10
+
11
+ def require_if_exists(path)
12
+ require path
13
+ return true
14
+ rescue LoadError, Gem::LoadError
15
+ return false
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ PluckAll::Hooks.init
@@ -1,130 +1,130 @@
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
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
+ 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 = RailsCompatibility.attribute_types(klass)
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.instantiate(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
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
2
- module PluckAll
3
- VERSION = '2.0.4'
4
- end
1
+ # frozen_string_literal: true
2
+ module PluckAll
3
+ VERSION = '2.1.0'
4
+ end
@@ -26,8 +26,16 @@ Gem::Specification.new do |spec|
26
26
  spec.bindir = 'exe'
27
27
  spec.executables = spec.files.grep(%r{^exe/}){|f| File.basename(f) }
28
28
  spec.require_paths = ['lib']
29
+ spec.metadata = {
30
+ 'homepage_uri' => 'https://github.com/khiav223577/pluck_all',
31
+ 'changelog_uri' => 'https://github.com/khiav223577/pluck_all/blob/master/CHANGELOG.md',
32
+ 'source_code_uri' => 'https://github.com/khiav223577/pluck_all',
33
+ 'documentation_uri' => 'https://www.rubydoc.info/gems/pluck_all',
34
+ 'bug_tracker_uri' => 'https://github.com/khiav223577/pluck_all/issues',
35
+ }
29
36
 
30
37
  spec.add_dependency 'activesupport', '>= 3.0.0'
38
+ spec.add_dependency 'rails_compatibility', '>= 0.0.2'
31
39
 
32
40
  spec.add_development_dependency 'bundler', '>= 1.17', '< 3.x'
33
41
  spec.add_development_dependency 'rake', '~> 12.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluck_all
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.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: 2019-04-04 00:00:00.000000000 Z
11
+ date: 2020-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails_compatibility
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.2
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -97,6 +111,7 @@ files:
97
111
  - gemfiles/active_record_50.gemfile
98
112
  - gemfiles/active_record_51.gemfile
99
113
  - gemfiles/active_record_52.gemfile
114
+ - gemfiles/active_record_60.gemfile
100
115
  - gemfiles/mongoid_54.gemfile
101
116
  - gemfiles/mongoid_64.gemfile
102
117
  - gemfiles/mongoid_70.gemfile
@@ -104,14 +119,18 @@ files:
104
119
  - lib/pluck_all/hooks.rb
105
120
  - lib/pluck_all/models/active_record_extension.rb
106
121
  - lib/pluck_all/models/mongoid_extension.rb
107
- - lib/pluck_all/models/patches/attribute_types.rb
108
122
  - lib/pluck_all/models/patches/deserialize.rb
109
123
  - lib/pluck_all/version.rb
110
124
  - pluck_all.gemspec
111
125
  homepage: https://github.com/khiav223577/pluck_all
112
126
  licenses:
113
127
  - MIT
114
- metadata: {}
128
+ metadata:
129
+ homepage_uri: https://github.com/khiav223577/pluck_all
130
+ changelog_uri: https://github.com/khiav223577/pluck_all/blob/master/CHANGELOG.md
131
+ source_code_uri: https://github.com/khiav223577/pluck_all
132
+ documentation_uri: https://www.rubydoc.info/gems/pluck_all
133
+ bug_tracker_uri: https://github.com/khiav223577/pluck_all/issues
115
134
  post_install_message:
116
135
  rdoc_options: []
117
136
  require_paths:
@@ -127,8 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
146
  - !ruby/object:Gem::Version
128
147
  version: '0'
129
148
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.7.6
149
+ rubygems_version: 3.0.3
132
150
  signing_key:
133
151
  specification_version: 4
134
152
  summary: Pluck multiple columns/attributes and return array of hashes. Support Rails
@@ -1,8 +0,0 @@
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 attribute_types column_types
6
- end
7
- end
8
- end