pluck_all 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e2e3f485c2caea545a255bafabcacf858a4b5ba
4
- data.tar.gz: 0b3c0e6ab93d853cfcd7c28130105b3ea0c0d309
3
+ metadata.gz: 5fca45f74cf96734cade27e1a8a328f9d9dc72c2
4
+ data.tar.gz: 6033dc1f3ac7ef9752d2795b538767df3084408a
5
5
  SHA512:
6
- metadata.gz: e287c8ededf69e125f4b08df26c7b9b93adca467649bec0352d2a9b5bdc716cdba2a759dd8a992147cfbb471b72887c0a9fab38c5d37f3fddc17fb07023156b5
7
- data.tar.gz: e06748b79bb73d4a4ae68949b9af43e29f04a9a2aba65ec03d8977863b500f6a3626d1a953809badf32b75a08e095e393ca2302fdab85edb811f920fd6bd5164
6
+ metadata.gz: 95fed15735f702eed093bee006ca233ac94248782e67c929e291c68b05597eca1cb91211b9e59fcd31542a2dad575fa2586cd03ddc4b73cc85148cb047508996
7
+ data.tar.gz: a9d363a864348ecd5b8b83e169ef268ae514b0f0154af4bfb3757a10d9dc1b58201dba6373cb3e824075a2cd204a023220fae757d84dd3853db7f3118fdb696c
data/README.md CHANGED
@@ -36,15 +36,16 @@ Or install it yourself as:
36
36
  ### pluck to array
37
37
  Behaves the same as the Rails 4 pluck, but you can use it in Rails 3
38
38
  ```rb
39
- User.where(:id => [1,2]).pluck_array(:id, :account)
39
+ User.where('id < 3').pluck_array(:id, :account)
40
40
  # => [[1, 'account1'], [2, 'account2']]
41
41
  ```
42
42
  ### pluck to hash
43
43
  Similar to `pluck_array`, but return hash instead.
44
44
  ```rb
45
- User.where(:id => [1,2]).pluck_all(:id, :account)
45
+ User.where('id < 3').pluck_all(:id, :account)
46
46
  # => [{"id"=>1, "account"=>"account1"}, {"id"=>2, "account"=>"account2"}]
47
- User.where(:id => [1,2]).pluck_all('id, account AS name')
47
+
48
+ User.where('id < 3').pluck_all('id, account AS name')
48
49
  # => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
49
50
  ```
50
51
 
data/gemfiles/3.2.gemfile CHANGED
@@ -2,11 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in pluck_all.gemspec
4
4
 
5
- gem "rails", "~> 3.2"
5
+ gem "activerecord", "~> 3.2"
6
6
 
7
7
  group :test do
8
8
  gem "simplecov"
9
9
  gem "codeclimate-test-reporter", "~> 1.0.0"
10
+ gem 'carrierwave', '~> 0.11.0'
10
11
  end
11
12
 
12
13
  gemspec :path => "../"
data/gemfiles/4.2.gemfile CHANGED
@@ -2,11 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in pluck_all.gemspec
4
4
 
5
- gem "rails", "~> 4.2"
5
+ gem "activerecord", "~> 4.2"
6
6
 
7
7
  group :test do
8
8
  gem "simplecov"
9
9
  gem "codeclimate-test-reporter", "~> 1.0.0"
10
+ gem 'carrierwave', '~> 0.11.0'
10
11
  end
11
12
 
12
13
  gemspec :path => "../"
data/gemfiles/5.0.gemfile CHANGED
@@ -2,11 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in pluck_all.gemspec
4
4
 
5
- gem "rails", "~> 5.0"
5
+ gem "activerecord", "~> 5.0"
6
6
 
7
7
  group :test do
8
8
  gem "simplecov"
9
9
  gem "codeclimate-test-reporter", "~> 1.0.0"
10
+ gem 'carrierwave', '~> 0.11.0'
10
11
  end
11
12
 
12
13
  gemspec :path => "../"
data/lib/pluck_all.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "pluck_all/version"
2
- require 'rails'
3
2
  require 'active_record'
4
3
 
5
4
  class ActiveRecord::Base
@@ -24,27 +23,34 @@ module ActiveRecord
24
23
  end
25
24
  end
26
25
  class ActiveRecord::Relation
27
- if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new('4.0.0')
26
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
28
27
  def pluck_all(*args)
29
28
  result = select_all(*args)
30
- result.map! do |attributes|
29
+ result.map! do |attributes| #This map! behaves different to array#map!
31
30
  initialized_attributes = klass.initialize_attributes(attributes)
32
31
  attributes.each do |key, attribute|
33
32
  attributes[key] = klass.type_cast_attribute(key, initialized_attributes) #TODO 現在AS過後的type cast會有一點問題
34
33
  end
34
+ cast_carrier_wave_uploader_url(attributes)
35
35
  end
36
36
  end
37
37
  else
38
38
  def pluck_all(*args)
39
39
  result = select_all(*args)
40
40
  attribute_types = klass.attribute_types
41
- result.map! do |attributes| #這邊的map似乎跟array.map!不一樣,result的值不會變
41
+ result.map! do |attributes| #This map! behaves different to array#map!
42
42
  attributes.each do |key, attribute|
43
43
  attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) #TODO 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
44
44
  end
45
+ cast_carrier_wave_uploader_url(attributes)
45
46
  end
46
47
  end
47
48
  end
49
+ def cast_need_columns(column_names, _klass = nil)
50
+ @pluck_all_cast_need_columns = column_names.map(&:to_s)
51
+ @pluck_all_cast_klass = _klass || klass
52
+ return self
53
+ end
48
54
  private
49
55
  def select_all(*args)
50
56
  args.map! do |column_name|
@@ -58,11 +64,25 @@ private
58
64
  return klass.connection.select_all(relation.select(args).to_sql)
59
65
  #return klass.connection.select_all(relation.arel)
60
66
  end
67
+ #----------------------------------
68
+ # Support casting CarrierWave url
69
+ #----------------------------------
70
+ def cast_carrier_wave_uploader_url(attributes)
71
+ if defined?(CarrierWave) and @pluck_all_cast_klass
72
+ @pluck_all_cast_klass.uploaders.each do |key, uploader|
73
+ next if (value = attributes[key.to_s]) == nil
74
+ obj = @pluck_all_cast_klass.new
75
+ obj[key] = value
76
+ @pluck_all_cast_need_columns.each{|s| obj[s] = attributes[s] }
77
+ attributes[key.to_s] = obj.send(:_mounter, key).uploader.to_s #uploaders.first
78
+ end
79
+ end
80
+ return attributes
81
+ end
61
82
  end
62
83
 
63
-
64
84
  class ActiveRecord::Relation
65
- if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new('4.0.2')
85
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.2')
66
86
  def pluck_array(*args)
67
87
  return pluck_all(*args).map{|hash|
68
88
  result = hash.values #P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
@@ -76,6 +96,9 @@ end
76
96
 
77
97
 
78
98
  class ActiveRecord::Base
99
+ def self.cast_need_columns(*args)
100
+ self.where('').cast_need_columns(*args)
101
+ end
79
102
  def self.pluck_all(*args)
80
103
  self.where('').pluck_all(*args)
81
104
  end
@@ -1,3 +1,3 @@
1
1
  module PluckAll
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
data/pluck_all.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["khiav reoy"]
10
10
  spec.email = ["mrtmrt15xn@yahoo.com.tw"]
11
11
 
12
- spec.summary = %q{Pluck multiple attributes in Rails 3, 4, 5, and can return data as hash instead of only array.}
13
- spec.description = %q{Pluck multiple attributes in Rails 3, 4, 5, and can return data as hash instead of only array. 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.}
12
+ spec.summary = %q{Pluck multiple columns/attributes in Rails 3, 4, 5, and can return data as hash instead of only array.}
13
+ spec.description = %q{Pluck multiple columns/attributes in Rails 3, 4, 5, and can return data as hash instead of only array. 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
14
  spec.homepage = "https://github.com/khiav223577/pluck_all"
15
15
  spec.license = "MIT"
16
16
 
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ["lib"]
29
29
 
30
30
  spec.add_development_dependency "bundler", "~> 1.11"
31
- spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rake", "~> 12.0"
32
32
  spec.add_development_dependency "sqlite3", "~> 1.3"
33
33
  spec.add_development_dependency "minitest", "~> 5.0"
34
34
 
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: 1.1.2
4
+ version: 1.2.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: 2017-01-06 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '12.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sqlite3
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,10 +80,10 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3'
83
- description: Pluck multiple attributes in Rails 3, 4, 5, and can return data as hash
84
- instead of only array. If you have a Rails 3 project, and want to pluck not only
85
- one column, feel free to use this gem and no need to worry about upgrading to Rails
86
- 4, 5 in the future will break this.
83
+ description: Pluck multiple columns/attributes in Rails 3, 4, 5, and can return data
84
+ as hash instead of only array. If you have a Rails 3 project, and want to pluck
85
+ not only one column, feel free to use this gem and no need to worry about upgrading
86
+ to Rails 4, 5 in the future will break this.
87
87
  email:
88
88
  - mrtmrt15xn@yahoo.com.tw
89
89
  executables: []
@@ -100,7 +100,6 @@ files:
100
100
  - bin/setup
101
101
  - gemfiles/3.2.gemfile
102
102
  - gemfiles/4.2.gemfile
103
- - gemfiles/4.2.gemfile.lock
104
103
  - gemfiles/5.0.gemfile
105
104
  - lib/pluck_all.rb
106
105
  - lib/pluck_all/version.rb
@@ -128,6 +127,6 @@ rubyforge_project:
128
127
  rubygems_version: 2.4.8
129
128
  signing_key:
130
129
  specification_version: 4
131
- summary: Pluck multiple attributes in Rails 3, 4, 5, and can return data as hash instead
132
- of only array.
130
+ summary: Pluck multiple columns/attributes in Rails 3, 4, 5, and can return data as
131
+ hash instead of only array.
133
132
  test_files: []
@@ -1,127 +0,0 @@
1
- PATH
2
- remote: ../
3
- specs:
4
- pluck_all (1.1.0)
5
- rails (>= 3)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actionmailer (4.2.7.1)
11
- actionpack (= 4.2.7.1)
12
- actionview (= 4.2.7.1)
13
- activejob (= 4.2.7.1)
14
- mail (~> 2.5, >= 2.5.4)
15
- rails-dom-testing (~> 1.0, >= 1.0.5)
16
- actionpack (4.2.7.1)
17
- actionview (= 4.2.7.1)
18
- activesupport (= 4.2.7.1)
19
- rack (~> 1.6)
20
- rack-test (~> 0.6.2)
21
- rails-dom-testing (~> 1.0, >= 1.0.5)
22
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
- actionview (4.2.7.1)
24
- activesupport (= 4.2.7.1)
25
- builder (~> 3.1)
26
- erubis (~> 2.7.0)
27
- rails-dom-testing (~> 1.0, >= 1.0.5)
28
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
- activejob (4.2.7.1)
30
- activesupport (= 4.2.7.1)
31
- globalid (>= 0.3.0)
32
- activemodel (4.2.7.1)
33
- activesupport (= 4.2.7.1)
34
- builder (~> 3.1)
35
- activerecord (4.2.7.1)
36
- activemodel (= 4.2.7.1)
37
- activesupport (= 4.2.7.1)
38
- arel (~> 6.0)
39
- activesupport (4.2.7.1)
40
- i18n (~> 0.7)
41
- json (~> 1.7, >= 1.7.7)
42
- minitest (~> 5.1)
43
- thread_safe (~> 0.3, >= 0.3.4)
44
- tzinfo (~> 1.1)
45
- arel (6.0.4)
46
- builder (3.2.2)
47
- codeclimate-test-reporter (1.0.3)
48
- simplecov
49
- concurrent-ruby (1.0.4)
50
- docile (1.1.5)
51
- erubis (2.7.0)
52
- globalid (0.3.7)
53
- activesupport (>= 4.1.0)
54
- i18n (0.7.0)
55
- json (1.8.3)
56
- loofah (2.0.3)
57
- nokogiri (>= 1.5.9)
58
- mail (2.6.4)
59
- mime-types (>= 1.16, < 4)
60
- mime-types (3.1)
61
- mime-types-data (~> 3.2015)
62
- mime-types-data (3.2016.0521)
63
- mini_portile2 (2.1.0)
64
- minitest (5.10.1)
65
- nokogiri (1.7.0)
66
- mini_portile2 (~> 2.1.0)
67
- rack (1.6.5)
68
- rack-test (0.6.3)
69
- rack (>= 1.0)
70
- rails (4.2.7.1)
71
- actionmailer (= 4.2.7.1)
72
- actionpack (= 4.2.7.1)
73
- actionview (= 4.2.7.1)
74
- activejob (= 4.2.7.1)
75
- activemodel (= 4.2.7.1)
76
- activerecord (= 4.2.7.1)
77
- activesupport (= 4.2.7.1)
78
- bundler (>= 1.3.0, < 2.0)
79
- railties (= 4.2.7.1)
80
- sprockets-rails
81
- rails-deprecated_sanitizer (1.0.3)
82
- activesupport (>= 4.2.0.alpha)
83
- rails-dom-testing (1.0.8)
84
- activesupport (>= 4.2.0.beta, < 5.0)
85
- nokogiri (~> 1.6)
86
- rails-deprecated_sanitizer (>= 1.0.1)
87
- rails-html-sanitizer (1.0.3)
88
- loofah (~> 2.0)
89
- railties (4.2.7.1)
90
- actionpack (= 4.2.7.1)
91
- activesupport (= 4.2.7.1)
92
- rake (>= 0.8.7)
93
- thor (>= 0.18.1, < 2.0)
94
- rake (10.5.0)
95
- simplecov (0.12.0)
96
- docile (~> 1.1.0)
97
- json (>= 1.8, < 3)
98
- simplecov-html (~> 0.10.0)
99
- simplecov-html (0.10.0)
100
- sprockets (3.7.1)
101
- concurrent-ruby (~> 1.0)
102
- rack (> 1, < 3)
103
- sprockets-rails (3.2.0)
104
- actionpack (>= 4.0)
105
- activesupport (>= 4.0)
106
- sprockets (>= 3.0.0)
107
- sqlite3 (1.3.12)
108
- thor (0.19.4)
109
- thread_safe (0.3.5)
110
- tzinfo (1.2.2)
111
- thread_safe (~> 0.1)
112
-
113
- PLATFORMS
114
- ruby
115
-
116
- DEPENDENCIES
117
- bundler (~> 1.11)
118
- codeclimate-test-reporter (~> 1.0.0)
119
- minitest (~> 5.0)
120
- pluck_all!
121
- rails (~> 4.2)
122
- rake (~> 10.0)
123
- simplecov
124
- sqlite3 (~> 1.3)
125
-
126
- BUNDLED WITH
127
- 1.11.2