pluck_all 2.0.4 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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: '../'
data/lib/pluck_all.rb CHANGED
@@ -1,3 +1,3 @@
1
- # frozen_string_literal: true
2
- require 'pluck_all/version'
3
- require 'pluck_all/hooks'
1
+ # frozen_string_literal: true
2
+ require 'pluck_all/version'
3
+ require 'pluck_all/hooks'
@@ -1,18 +1,18 @@
1
1
  module PluckAll
2
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
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')
9
7
  end
10
8
 
11
- # Mongoid
12
- begin
13
- require 'mongoid'
14
- require 'pluck_all/models/mongoid_extension'
9
+ private
10
+
11
+ def require_if_exists(path)
12
+ require path
13
+ return true
15
14
  rescue LoadError, Gem::LoadError
15
+ return false
16
16
  end
17
17
  end
18
18
  end
@@ -1,4 +1,6 @@
1
- require_relative 'patches/attribute_types'
1
+ require 'rails_compatibility/attribute_types'
2
+ require 'rails_compatibility/has_include'
3
+ require 'rails_compatibility/apply_join_dependency'
2
4
  require_relative 'patches/deserialize'
3
5
 
4
6
  class ActiveRecord::Relation
@@ -8,16 +10,24 @@ class ActiveRecord::Relation
8
10
  return self
9
11
  end
10
12
 
11
- def select_all(*column_names)
13
+ def select_all(column_names)
12
14
  relation = clone
15
+
16
+ # See: https://github.com/globalize/globalize/pull/707
17
+ if relation.klass.method_defined?(:translated_attribute_names) && (parsed = parse_translated_columns(column_names))
18
+ relation = relation.join_translations
19
+ column_names = parsed
20
+ end
21
+
13
22
  relation.select_values = [].freeze # cannot use `unscope(:select)` in Rails 3
14
- return klass.connection.select_all(relation.select(column_names).to_sql)
23
+
24
+ sql = relation.select(column_names.map(&to_sql_column_name)).to_sql
25
+ return klass.connection.select_all(sql)
15
26
  end
16
27
 
17
28
  if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
18
29
  def pluck_all(*column_names, cast_uploader_url: true)
19
- column_names.map!(&to_sql_column_name)
20
- result = select_all(*column_names)
30
+ result = select_all(column_names)
21
31
  result.map! do |attributes| # This map! behaves different to array#map!
22
32
  initialized_attributes = klass.initialize_attributes(attributes)
23
33
  attributes.each do |key, _attribute|
@@ -43,15 +53,12 @@ class ActiveRecord::Relation
43
53
  end
44
54
  else
45
55
  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!
56
+ has_include = RailsCompatibility.has_include?(self, column_names.first)
57
+ return RailsCompatibility.apply_join_dependency(self).pluck_all(*column_names) if has_include
58
+
59
+ result = select_all(column_names)
60
+ attribute_types = RailsCompatibility.attribute_types(klass)
61
+ result.map do |attributes| # This map behaves different to array#map
55
62
  attributes.each do |key, attribute|
56
63
  attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) # TODO: 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
57
64
  end
@@ -80,12 +87,13 @@ class ActiveRecord::Relation
80
87
  # ----------------------------------------------------------------
81
88
  def cast_carrier_wave_uploader_url(attributes)
82
89
  if defined?(CarrierWave) && klass.respond_to?(:uploaders)
90
+ @pluck_all_cast_need_columns ||= nil
83
91
  @pluck_all_cast_klass ||= klass
84
92
  @pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, _uploader| attributes.key?(key.to_s) }
85
93
  @pluck_all_uploaders.each do |key, _uploader|
86
94
  {}.tap do |hash|
87
95
  @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)
96
+ obj = @pluck_all_cast_klass.instantiate(hash)
89
97
  obj[key] = attributes[key_s = key.to_s]
90
98
  # https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
91
99
  attributes[key_s] = obj.send(key)
@@ -1,70 +1,70 @@
1
- # frozen_string_literal: true
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
-
13
- module Findable
14
- delegate :pluck_all, :pluck_array, to: :with_default_scope
15
- end
16
-
17
- module Contextual
18
- delegate :pluck_all, :pluck_array, to: :context
19
-
20
- class None
21
- def pluck_array(*)
22
- []
23
- end
24
-
25
- def pluck_all(*)
26
- []
27
- end
28
- end
29
-
30
- class Mongo
31
- def pluck_array(*fields)
32
- normalized_select = get_normalized_select(fields)
33
- get_query_data(normalized_select).reduce([]) do |plucked, doc|
34
- values = normalized_select.keys.map(&plucked_value_mapper(:array, doc))
35
- plucked << (values.size == 1 ? values.first : values)
36
- end
37
- end
38
-
39
- def pluck_all(*fields)
40
- normalized_select = get_normalized_select(fields)
41
- get_query_data(normalized_select).reduce([]) do |plucked, doc|
42
- values = normalized_select.keys.map(&plucked_value_mapper(:all, doc))
43
- plucked << values.to_h
44
- end
45
- end
46
-
47
- private
48
-
49
- def plucked_value_mapper(type, doc)
50
- proc 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
58
-
59
- def get_query_data(normalized_select)
60
- return (@view ? @view.projection(normalized_select) : query.dup.select(normalized_select))
61
- end
62
-
63
- def get_normalized_select(fields)
64
- fields.each_with_object({}) do |f, hash|
65
- hash[klass.database_field_name(f)] = 1
66
- end
67
- end
68
- end
69
- end
70
- end
1
+ # frozen_string_literal: true
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
+
13
+ module Findable
14
+ delegate :pluck_all, :pluck_array, to: :with_default_scope
15
+ end
16
+
17
+ module Contextual
18
+ delegate :pluck_all, :pluck_array, to: :context
19
+
20
+ class None
21
+ def pluck_array(*)
22
+ []
23
+ end
24
+
25
+ def pluck_all(*)
26
+ []
27
+ end
28
+ end
29
+
30
+ class Mongo
31
+ def pluck_array(*fields)
32
+ normalized_select = get_normalized_select(fields)
33
+ get_query_data(normalized_select).reduce([]) do |plucked, doc|
34
+ values = normalized_select.keys.map(&plucked_value_mapper(:array, doc))
35
+ plucked << (values.size == 1 ? values.first : values)
36
+ end
37
+ end
38
+
39
+ def pluck_all(*fields)
40
+ normalized_select = get_normalized_select(fields)
41
+ get_query_data(normalized_select).reduce([]) do |plucked, doc|
42
+ values = normalized_select.keys.map(&plucked_value_mapper(:all, doc))
43
+ plucked << values.to_h
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def plucked_value_mapper(type, doc)
50
+ proc 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
58
+
59
+ def get_query_data(normalized_select)
60
+ return (@view ? @view.projection(normalized_select) : query.dup.select(normalized_select))
61
+ end
62
+
63
+ def get_normalized_select(fields)
64
+ fields.each_with_object({}) do |f, hash|
65
+ hash[klass.database_field_name(f)] = 1
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,13 +1,13 @@
1
- module ActiveRecord
2
- [
3
- *([Type::Value, Type::Integer, Type::Serialized] if defined?(Type::Value)),
4
- *([Enum::EnumType] if defined?(Enum::EnumType)),
5
- ].each do |s|
6
- s.class_eval do
7
- if !method_defined?(:deserialize) && method_defined?(:type_cast_from_database)
8
- # deserialize was changed to type_cast_from_database in Rails 5
9
- alias_method :deserialize, :type_cast_from_database
10
- end
11
- end
12
- end
13
- end
1
+ module ActiveRecord
2
+ [
3
+ *([Type::Value, Type::Integer, Type::Serialized] if defined?(Type::Value)),
4
+ *([Enum::EnumType] if defined?(Enum::EnumType)),
5
+ ].each do |s|
6
+ s.class_eval do
7
+ if !method_defined?(:deserialize) && method_defined?(:type_cast_from_database)
8
+ # deserialize was changed to type_cast_from_database in Rails 5
9
+ alias_method :deserialize, :type_cast_from_database
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module PluckAll
3
- VERSION = '2.0.4'
3
+ VERSION = '2.3.2'
4
4
  end
data/pluck_all.gemspec CHANGED
@@ -1,35 +1,43 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'pluck_all/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'pluck_all'
8
- spec.version = PluckAll::VERSION
9
- spec.authors = ['khiav reoy']
10
- spec.email = ['mrtmrt15xn@yahoo.com.tw']
11
-
12
- spec.summary = 'Pluck multiple columns/attributes and return array of hashes. Support Rails 3, 4, 5.'
13
- spec.description = 'Pluck multiple columns/attributes and return array of hashes. Support Rails 3, 4, 5. If you have a Rails 3 project, and want to pluck not only one column, feel free to use this gem and no need to worry about upgrading to Rails 4, 5 in the future will break this.'
14
- spec.homepage = 'https://github.com/khiav223577/pluck_all'
15
- spec.license = 'MIT'
16
-
17
- # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
- # delete this section to allow pushing this gem to any host.
19
- # if spec.respond_to?(:metadata)
20
- # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
- # else
22
- # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
- # end
24
-
25
- spec.files = `git ls-files -z`.split("\x0").reject{|f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = 'exe'
27
- spec.executables = spec.files.grep(%r{^exe/}){|f| File.basename(f) }
28
- spec.require_paths = ['lib']
29
-
30
- spec.add_dependency 'activesupport', '>= 3.0.0'
31
-
32
- spec.add_development_dependency 'bundler', '>= 1.17', '< 3.x'
33
- spec.add_development_dependency 'rake', '~> 12.0'
34
- spec.add_development_dependency 'minitest', '~> 5.0'
35
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pluck_all/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pluck_all'
8
+ spec.version = PluckAll::VERSION
9
+ spec.authors = ['khiav reoy']
10
+ spec.email = ['mrtmrt15xn@yahoo.com.tw']
11
+
12
+ spec.summary = 'Pluck multiple columns/attributes and return array of hashes. Support Rails 3, 4, 5.'
13
+ spec.description = 'Pluck multiple columns/attributes and return array of hashes. Support Rails 3, 4, 5. If you have a Rails 3 project, and want to pluck not only one column, feel free to use this gem and no need to worry about upgrading to Rails 4, 5 in the future will break this.'
14
+ spec.homepage = 'https://github.com/khiav223577/pluck_all'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject{|f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}){|f| File.basename(f) }
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
+ }
36
+
37
+ spec.add_dependency 'activesupport', '>= 3.0.0'
38
+ spec.add_dependency 'rails_compatibility', '>= 0.0.7'
39
+
40
+ spec.add_development_dependency 'bundler', '>= 1.17', '< 3.x'
41
+ spec.add_development_dependency 'rake', '~> 12.0'
42
+ spec.add_development_dependency 'minitest', '~> 5.0'
43
+ end
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.3.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: 2019-04-04 00:00:00.000000000 Z
11
+ date: 2021-06-10 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.7
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.7
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -82,9 +96,9 @@ executables: []
82
96
  extensions: []
83
97
  extra_rdoc_files: []
84
98
  files:
99
+ - ".github/workflows/ruby.yml"
85
100
  - ".gitignore"
86
101
  - ".rubocop.yml"
87
- - ".travis.yml"
88
102
  - CHANGELOG.md
89
103
  - CODE_OF_CONDUCT.md
90
104
  - LICENSE.txt
@@ -97,6 +111,8 @@ 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
115
+ - gemfiles/active_record_61.gemfile
100
116
  - gemfiles/mongoid_54.gemfile
101
117
  - gemfiles/mongoid_64.gemfile
102
118
  - gemfiles/mongoid_70.gemfile
@@ -104,14 +120,18 @@ files:
104
120
  - lib/pluck_all/hooks.rb
105
121
  - lib/pluck_all/models/active_record_extension.rb
106
122
  - lib/pluck_all/models/mongoid_extension.rb
107
- - lib/pluck_all/models/patches/attribute_types.rb
108
123
  - lib/pluck_all/models/patches/deserialize.rb
109
124
  - lib/pluck_all/version.rb
110
125
  - pluck_all.gemspec
111
126
  homepage: https://github.com/khiav223577/pluck_all
112
127
  licenses:
113
128
  - MIT
114
- metadata: {}
129
+ metadata:
130
+ homepage_uri: https://github.com/khiav223577/pluck_all
131
+ changelog_uri: https://github.com/khiav223577/pluck_all/blob/master/CHANGELOG.md
132
+ source_code_uri: https://github.com/khiav223577/pluck_all
133
+ documentation_uri: https://www.rubydoc.info/gems/pluck_all
134
+ bug_tracker_uri: https://github.com/khiav223577/pluck_all/issues
115
135
  post_install_message:
116
136
  rdoc_options: []
117
137
  require_paths:
@@ -127,8 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
147
  - !ruby/object:Gem::Version
128
148
  version: '0'
129
149
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.7.6
150
+ rubygems_version: 3.2.14
132
151
  signing_key:
133
152
  specification_version: 4
134
153
  summary: Pluck multiple columns/attributes and return array of hashes. Support Rails