pluck_all 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,68 @@
1
1
  sudo: false
2
- env:
3
- global:
4
- - CC_TEST_REPORTER_ID=db72eba1ff8fb1329dae5fb9b9dcd234243899d7a464ceb374e14a05ead27b7c
5
2
  language: ruby
6
3
  rvm:
7
4
  - 2.2
8
- - 2.3
5
+ - 2.6
6
+ - 2.7
9
7
  services:
10
8
  - mongodb
9
+ env:
10
+ global:
11
+ - CC_TEST_REPORTER_ID=db72eba1ff8fb1329dae5fb9b9dcd234243899d7a464ceb374e14a05ead27b7c
12
+ matrix:
13
+ - ORM_TYPE=ACTIVE_RECORD
14
+ - ORM_TYPE=MONGOID
11
15
  gemfile:
12
- - gemfiles/3.2.gemfile
13
- - gemfiles/4.2.gemfile
14
- - gemfiles/5.0.gemfile
15
- - gemfiles/5.1.gemfile
16
- - gemfiles/5.2.gemfile
16
+ - gemfiles/active_record_32.gemfile
17
+ - gemfiles/active_record_42.gemfile
18
+ - gemfiles/active_record_50.gemfile
19
+ - gemfiles/active_record_51.gemfile
20
+ - gemfiles/active_record_52.gemfile
21
+ - gemfiles/active_record_60.gemfile
22
+ - gemfiles/mongoid_54.gemfile
23
+ - gemfiles/mongoid_64.gemfile
24
+ - gemfiles/mongoid_70.gemfile
25
+ matrix:
26
+ exclude:
27
+ - gemfile: gemfiles/active_record_32.gemfile
28
+ rvm: 2.6
29
+ - gemfile: gemfiles/active_record_32.gemfile
30
+ rvm: 2.7
31
+ - gemfile: gemfiles/active_record_32.gemfile
32
+ env: ORM_TYPE=MONGOID
33
+ - gemfile: gemfiles/active_record_42.gemfile
34
+ rvm: 2.7
35
+ - gemfile: gemfiles/active_record_42.gemfile
36
+ env: ORM_TYPE=MONGOID
37
+ - gemfile: gemfiles/active_record_50.gemfile
38
+ env: ORM_TYPE=MONGOID
39
+ - gemfile: gemfiles/active_record_51.gemfile
40
+ env: ORM_TYPE=MONGOID
41
+ - gemfile: gemfiles/active_record_52.gemfile
42
+ env: ORM_TYPE=MONGOID
43
+ - gemfile: gemfiles/active_record_60.gemfile
44
+ rvm: 2.2
45
+ - gemfile: gemfiles/active_record_60.gemfile
46
+ env: ORM_TYPE=MONGOID
47
+ - gemfile: gemfiles/mongoid_54.gemfile
48
+ rvm: 2.7
49
+ - gemfile: gemfiles/mongoid_54.gemfile
50
+ env: ORM_TYPE=ACTIVE_RECORD
51
+ - gemfile: gemfiles/mongoid_64.gemfile
52
+ env: ORM_TYPE=ACTIVE_RECORD
53
+ - gemfile: gemfiles/mongoid_70.gemfile
54
+ env: ORM_TYPE=ACTIVE_RECORD
17
55
  before_install:
18
- - gem install bundler
19
- - gem update --system
56
+ - if `ruby -e 'exit(RUBY_VERSION.to_f < 2.7)'`; then
57
+ gem i rubygems-update -v '< 3' && update_rubygems;
58
+ gem install bundler -v '< 2';
59
+ fi
20
60
  - gem --version
21
61
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
22
62
  - chmod +x ./cc-test-reporter
23
63
  - ./cc-test-reporter before-build
24
64
  script:
25
- - bundle exec rake test
65
+ - if [ "$ORM_TYPE" = "ACTIVE_RECORD" ]; then bundle exec rake test_active_record; fi
66
+ - if [ "$ORM_TYPE" = "MONGOID" ]; then bundle exec rake test_mongoid; fi
26
67
  after_script:
27
68
  - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -1,6 +1,34 @@
1
1
  ## Change Log
2
2
 
3
- ### [upcoming](https://github.com/khiav223577/pluck_all/compare/v1.2.4...HEAD) 2018/04/05
3
+ ### [upcoming](https://github.com/khiav223577/pluck_all/compare/v2.0.4...HEAD) 2019/12/19
4
+ - [#43](https://github.com/khiav223577/pluck_all/pull/43) Use rails_compatibility to get attribute_types for better compatibility (@khiav223577)
5
+ - [#41](https://github.com/khiav223577/pluck_all/pull/41) Support Rails 6.0 (@khiav223577)
6
+
7
+ ### [v2.0.4](https://github.com/khiav223577/pluck_all/compare/v2.0.3...v2.0.4) 2019/04/04
8
+ - [#40](https://github.com/khiav223577/pluck_all/pull/40) Fix: inconsistent with pluck when having `select` on relation (@khiav223577)
9
+ - [#39](https://github.com/khiav223577/pluck_all/pull/39) Fix issue#38 (@MasashiYokota)
10
+ - [#36](https://github.com/khiav223577/pluck_all/pull/36) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
11
+ - [#35](https://github.com/khiav223577/pluck_all/pull/35) refactor #test_pluck_with_includes (@khiav223577)
12
+ - [#34](https://github.com/khiav223577/pluck_all/pull/34) Provide Arel support as well (@snkashis)
13
+ - [#33](https://github.com/khiav223577/pluck_all/pull/33) Fix gemfile path in bin/setup (@snkashis)
14
+ - [#32](https://github.com/khiav223577/pluck_all/pull/32) Move patches into separate files (@khiav223577)
15
+
16
+ ### [v2.0.3](https://github.com/khiav223577/pluck_all/compare/v2.0.2...v2.0.3) 2018/07/19
17
+ - [#30](https://github.com/khiav223577/pluck_all/pull/30) Fix: includes + pluck_all results in strange output (@khiav223577)
18
+
19
+ ### [v2.0.2](https://github.com/khiav223577/pluck_all/compare/v2.0.1...v2.0.2) 2018/06/29
20
+ - [#28](https://github.com/khiav223577/pluck_all/pull/28) Fix test cases and the result format may be wrong when plucking multiple columns with nil value may (@khiav223577)
21
+ - [#27](https://github.com/khiav223577/pluck_all/pull/27) [Feature] Mongoid Hooks And Tests Separation (@berniechiu)
22
+ - [#26](https://github.com/khiav223577/pluck_all/pull/26) [FIX] Wrong Module Skipped (@berniechiu)
23
+
24
+ ### [v2.0.1](https://github.com/khiav223577/pluck_all/compare/v2.0.0...v2.0.1) 2018/05/27
25
+ - [#25](https://github.com/khiav223577/pluck_all/pull/25) Fix that project without mongoid will raise LoadError (@khiav223577)
26
+
27
+ ### [v2.0.0](https://github.com/khiav223577/pluck_all/compare/v1.2.4...v2.0.0) 2018/05/27
28
+ - [#24](https://github.com/khiav223577/pluck_all/pull/24) Support Mongoid! (@khiav223577)
29
+ - [#23](https://github.com/khiav223577/pluck_all/pull/23) Refactoring Coding Style (@khiav223577)
30
+ - [#22](https://github.com/khiav223577/pluck_all/pull/22) test Rails 5.2 (@khiav223577)
31
+ - [#21](https://github.com/khiav223577/pluck_all/pull/21) should test both 5.0.x and 5.1.x (@khiav223577)
4
32
  - [#20](https://github.com/khiav223577/pluck_all/pull/20) test pluck_all in rails 5.1.x (@khiav223577)
5
33
  - [#19](https://github.com/khiav223577/pluck_all/pull/19) add test cases to test `join` with table name and `alias` (@khiav223577)
6
34
 
@@ -34,6 +62,6 @@
34
62
  ### [v1.1.0](https://github.com/khiav223577/pluck_all/compare/v1.0.1...v1.1.0) 2016/12/21
35
63
  - [#3](https://github.com/khiav223577/pluck_all/pull/3) Feature/pluck array (@khiav223577)
36
64
 
37
- ### v1.0.1 2016/12/20
65
+ ### [v1.0.1](https://github.com/khiav223577/pluck_all/compare/v1.0.0...v1.0.1) 2016/12/20
38
66
  - [#2](https://github.com/khiav223577/pluck_all/pull/2) test with multiple rails version (@khiav223577)
39
67
  - [#1](https://github.com/khiav223577/pluck_all/pull/1) add basic test cases (@khiav223577)
data/README.md CHANGED
@@ -6,15 +6,17 @@
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, 6, and can return data as hash instead of only array. Also supports `Mongoid`.
10
10
 
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/).
11
+ This Gem stands on the shoulders of this article: [Plucking Multiple Columns in Rails 3](https://meltingice.dev/2013/06/11/pluck-multiple-columns-rails/).
12
12
  And modified to support not only Rail 3.
13
13
 
14
14
  If you have a Rails 3 project, and want to pluck not only one column,
15
- feel free to use this gem and no need to worry about upgrading to Rails 4, 5 in the future will break this.
16
-
15
+ feel free to use this gem and no need to worry about upgrading to Rails 4, 5, 6 in the future will break this.
17
16
 
17
+ ## Supports
18
+ - Ruby 2.2 ~ 2.7
19
+ - Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0
18
20
 
19
21
  ## Installation
20
22
 
@@ -36,7 +38,7 @@ Or install it yourself as:
36
38
 
37
39
  ### pluck to array
38
40
 
39
- Behaves the same as the Rails 4 pluck, but you can use it in Rails 3
41
+ Behaves the same as `#pluck` method, but you can use it to pluck multiple columns in Rails 3
40
42
 
41
43
  ```rb
42
44
  User.where('id < 3').pluck_array(:id, :account)
@@ -45,12 +47,15 @@ User.where('id < 3').pluck_array(:id, :account)
45
47
 
46
48
  ### pluck to hash
47
49
 
48
- Similar to `pluck_array`, but return hash instead.
50
+ Similar to `#pluck` method, but return array of hashes instead.
49
51
 
50
52
  ```rb
51
53
  User.where('id < 3').pluck_all(:id, :account)
52
54
  # => [{"id"=>1, "account"=>"account1"}, {"id"=>2, "account"=>"account2"}]
53
55
 
56
+ User.where('id < 3').pluck_all(:id, 'account AS name')
57
+ # => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
58
+
54
59
  User.where('id < 3').pluck_all('id, account AS name')
55
60
  # => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
56
61
  ```
@@ -85,18 +90,50 @@ select + map 10.530000 0.660000 11.190000 ( 12.550974)
85
90
  select + as_json 49.040000 1.120000 50.160000 ( 55.417534)
86
91
  pluck_all 3.310000 0.100000 3.410000 ( 3.527775)
87
92
  ```
93
+ Test by `benchmark-ips` and `limit 100` in each iteration:
94
+ ```
95
+ Warming up --------------------------------------
96
+ map 1.000 i/100ms
97
+ select + map 28.000 i/100ms
98
+ select + as_json 7.000 i/100ms
99
+ pluck_all 54.000 i/100ms
100
+ Calculating -------------------------------------
101
+ map 14.230 (± 0.0%) i/s - 72.000 in 5.065349s
102
+ select + map 281.638 (± 4.6%) i/s - 1.428k in 5.081216s
103
+ select + as_json 73.241 (± 4.1%) i/s - 371.000 in 5.076235s
104
+ pluck_all 539.057 (± 6.7%) i/s - 2.700k in 5.034858s
105
+
106
+ Comparison:
107
+ pluck_all: 539.1 i/s
108
+ select + map: 281.6 i/s - 1.91x slower
109
+ select + as_json: 73.2 i/s - 7.36x slower
110
+ map: 14.2 i/s - 37.88x slower
111
+ ```
88
112
  [test script](https://github.com/khiav223577/pluck_all/issues/18)
89
113
 
90
114
  ### Compare with [pluck_to_hash](https://github.com/girishso/pluck_to_hash) gem
91
115
 
92
- `pluck_all` has better performace since it uses raw `hash` data from `ActiveRecord::Base.connection.select_all`, while `pluck_to_hash` uses `pluck` method, which calls `ActiveRecord::Base.connection.select_all` and transfers the raw `hash` data to `array` format, and then transfer the data to `hash` format again. The following benchmark test uses same datebase as above.
116
+ `pluck_all` has better performace since it uses raw `hash` data from `ActiveRecord::Base.connection.select_all`, while `pluck_to_hash` uses `pluck` method, which calls `ActiveRecord::Base.connection.select_all` and transfers the raw `hash` data to `array` format, and then transfer the data to `hash` format again. The following benchmark shows the performance difference:
93
117
 
94
118
  ```rb
95
119
                                        user     system     total       real
96
120
  pluck_to_hash 2.960000 0.130000 3.090000 ( 3.421640)
97
121
  pluck_all 2.160000 0.120000 2.280000 ( 2.605118)
98
122
  ```
99
- [test script](https://github.com/khiav223577/pluck_all/issues/18#issuecomment-325407080)
123
+ Tested by `benchmark-ips` and `limit 1000` in each iteration:
124
+ ```
125
+ Warming up --------------------------------------
126
+ pluck_to_hash 7.000 i/100ms
127
+ pluck_all 9.000 i/100ms
128
+ Calculating -------------------------------------
129
+ pluck_to_hash 84.526 (± 4.7%) i/s - 427.000 in 5.065792s
130
+ pluck_all 95.133 (± 4.2%) i/s - 477.000 in 5.021555s
131
+
132
+ Comparison:
133
+ pluck_all: 95.1 i/s
134
+ pluck_to_hash: 84.5 i/s - 1.13x slower
135
+ ```
136
+ See the [test script](https://github.com/khiav223577/pluck_all/issues/18#issuecomment-325407080) for more details.
100
137
 
101
138
  ## Other Support
102
139
  ### Support Pluck Carrierwave Uploader (if you use carrierwave)
@@ -110,12 +147,12 @@ User.where(xxx).map(&:profile_pic)
110
147
  If the uploader use something like: `model.id`, `model.name`
111
148
  You may have to send these columns manually:
112
149
  ```rb
113
- User.where(xxx).cast_need_columns(%i(id, name)).pluck_all(:id, :name, :profile_pic).map{|s| s['profile_pic'] }
150
+ User.where(xxx).cast_need_columns(%i[id name]).pluck_all(:id, :name, :profile_pic).map{|s| s['profile_pic'] }
114
151
  ```
115
152
 
116
153
  ## Development
117
154
 
118
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
155
+ 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
156
 
120
157
  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
158
 
data/Rakefile CHANGED
@@ -1,10 +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
- task :default => :test
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
+
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
@@ -3,6 +3,6 @@ set -euo pipefail
3
3
  IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
- bundle install --gemfile=gemfiles/4.2.gemfile
6
+ bundle install --gemfile=gemfiles/active_record_42.gemfile
7
7
 
8
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.3.0'
6
+ gem 'activerecord', '~> 3.2.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
+
9
+ group :test do
10
+ gem 'simplecov'
11
+ end
12
+
13
+ gemspec path: '../'
@@ -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.3.0'
6
+ gem 'activerecord', '~> 4.2.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
+
9
+ group :test do
10
+ gem 'simplecov'
11
+ end
12
+
13
+ gemspec path: '../'
@@ -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.3.0'
6
+ gem 'activerecord', '~> 5.0.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
+
9
+ group :test do
10
+ gem 'simplecov'
11
+ end
12
+
13
+ gemspec path: '../'
@@ -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.3.0'
6
+ gem 'activerecord', '~> 5.1.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
+
9
+ group :test do
10
+ gem 'simplecov'
11
+ end
12
+
13
+ gemspec path: '../'
@@ -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.3.0'
6
+ gem 'activerecord', '~> 5.2.0'
7
+ gem 'carrierwave', '~> 0.11.0'
8
+
9
+ group :test do
10
+ gem 'simplecov'
11
+ end
12
+
13
+ gemspec path: '../'
@@ -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: '../'
@@ -0,0 +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: '../'
@@ -0,0 +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: '../'
@@ -0,0 +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,131 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
  require 'pluck_all/version'
3
- require 'active_record'
4
- begin
5
- require 'mongoid'
6
- require 'pluck_all/mongoid_pluck_all'
7
- rescue Gem::LoadError
8
- end
9
-
10
- class ActiveRecord::Base
11
- if !defined?(attribute_types) && defined?(column_types)
12
- class << self
13
- # column_types was changed to attribute_types in Rails 5
14
- alias_method :attribute_types, :column_types
15
- end
16
- end
17
- end
18
-
19
- module ActiveRecord
20
- [
21
- *([Type::Value, Type::Integer, Type::Serialized] if defined?(Type::Value)),
22
- *([Enum::EnumType] if defined?(Enum::EnumType)),
23
- ].each do |s|
24
- s.class_eval do
25
- if !method_defined?(:deserialize) && method_defined?(:type_cast_from_database)
26
- # column_types was changed to attribute_types in Rails 5
27
- alias deserialize type_cast_from_database
28
- end
29
- end
30
- end
31
- end
32
-
33
- class ActiveRecord::Relation
34
- if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
35
- def pluck_all(*args)
36
- result = select_all(*args)
37
- result.map! do |attributes| # This map! behaves different to array#map!
38
- initialized_attributes = klass.initialize_attributes(attributes)
39
- attributes.each do |key, attribute|
40
- attributes[key] = klass.type_cast_attribute(key, initialized_attributes) #TODO 現在AS過後的type cast會有一點問題
41
- end
42
- cast_carrier_wave_uploader_url(attributes)
43
- end
44
- end
45
- else
46
- def pluck_all(*args)
47
- result = select_all(*args)
48
- attribute_types = klass.attribute_types
49
- result.map! do |attributes| # This map! behaves different to array#map!
50
- attributes.each do |key, attribute|
51
- attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) #TODO 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
52
- end
53
- cast_carrier_wave_uploader_url(attributes)
54
- end
55
- end
56
- end
57
-
58
- def cast_need_columns(column_names, _klass = nil)
59
- @pluck_all_cast_need_columns = column_names.map(&:to_s)
60
- @pluck_all_cast_klass = _klass
61
- return self
62
- end
63
-
64
- private
65
-
66
- def select_all(*args)
67
- args.map! do |column_name|
68
- if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
69
- "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
70
- else
71
- column_name.to_s
72
- end
73
- end
74
- relation = clone
75
- return klass.connection.select_all(relation.select(args).to_sql)
76
- #return klass.connection.select_all(relation.arel)
77
- end
78
-
79
- # ----------------------------------------------------------------
80
- # ● Support casting CarrierWave url
81
- # ----------------------------------------------------------------
82
- def cast_carrier_wave_uploader_url(attributes)
83
- if defined?(CarrierWave) && klass.respond_to?(:uploaders)
84
- @pluck_all_cast_klass ||= klass
85
- @pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, uploader| attributes.key?(key.to_s) }
86
- @pluck_all_uploaders.each do |key, uploader|
87
- hash = {}
88
- @pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns
89
- obj = @pluck_all_cast_klass.new(hash)
90
- obj[key] = attributes[key_s = key.to_s]
91
- #https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
92
- attributes[key_s] = obj.send(key)
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).map{|hash|
103
- result = hash.values #P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
104
- next (result.one? ? result.first : result)
105
- }
106
- end
107
- else
108
- alias_method :pluck_array, :pluck if not method_defined?(:pluck_array)
109
- end
110
- end
111
-
112
-
113
- class << ActiveRecord::Base
114
- def cast_need_columns(*args)
115
- where(nil).cast_need_columns(*args)
116
- end
117
-
118
- def pluck_all(*args)
119
- where(nil).pluck_all(*args)
120
- end
121
-
122
- def pluck_array(*args)
123
- where(nil).pluck_array(*args)
124
- end
125
- end
126
-
127
- module ActiveRecord::NullRelation
128
- def pluck_all(*args)
129
- []
130
- end
131
- end
3
+ require 'pluck_all/hooks'