pluck_all 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fca45f74cf96734cade27e1a8a328f9d9dc72c2
4
- data.tar.gz: 6033dc1f3ac7ef9752d2795b538767df3084408a
3
+ metadata.gz: f437fda05144e7f3e3954b6240f2b693755ab275
4
+ data.tar.gz: 764277729c198b2dee6c6e30c1d07c5f31341be0
5
5
  SHA512:
6
- metadata.gz: 95fed15735f702eed093bee006ca233ac94248782e67c929e291c68b05597eca1cb91211b9e59fcd31542a2dad575fa2586cd03ddc4b73cc85148cb047508996
7
- data.tar.gz: a9d363a864348ecd5b8b83e169ef268ae514b0f0154af4bfb3757a10d9dc1b58201dba6373cb3e824075a2cd204a023220fae757d84dd3853db7f3118fdb696c
6
+ metadata.gz: e3d83b3af48aff47a6435a7091571aeea203b53c877307f3af625bf55830be2af3dd9a007e144ba8aa73e05bae61aec34b390d03bba208eacba6cd454332273f
7
+ data.tar.gz: 44366aa3181d79553bf243c5aec694f12f324bfbe857cf6758203d1450f7074a85a17d323b3255bfbeaa2651c3160af3fec33b78627dd788a7fe85d49dbf4580
data/README.md CHANGED
@@ -49,6 +49,20 @@ User.where('id < 3').pluck_all('id, account AS name')
49
49
  # => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
50
50
  ```
51
51
 
52
+ ### Support Pluck Carrierwave Uploader (if you use carrierwave)
53
+ ```rb
54
+ User.where(xxx).pluck_all(:profile_pic).map{|s| s['profile_pic'] }
55
+ ```
56
+ is the same as
57
+ ```rb
58
+ User.where(xxx).map(&:profile_pic)
59
+ ```
60
+ If the uploader use something like: `model.id`, `model.name`
61
+ You should send these columns manually:
62
+ ```rb
63
+ User.where(xxx).cast_need_columns(%i(id, name)).pluck_all(:id, :name, :profile_pic).map{|s| s['profile_pic'] }
64
+ ```
65
+
52
66
  ## Development
53
67
 
54
68
  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.
data/lib/pluck_all.rb CHANGED
@@ -48,7 +48,7 @@ class ActiveRecord::Relation
48
48
  end
49
49
  def cast_need_columns(column_names, _klass = nil)
50
50
  @pluck_all_cast_need_columns = column_names.map(&:to_s)
51
- @pluck_all_cast_klass = _klass || klass
51
+ @pluck_all_cast_klass = _klass
52
52
  return self
53
53
  end
54
54
  private
@@ -68,13 +68,14 @@ private
68
68
  # Support casting CarrierWave url
69
69
  #----------------------------------
70
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
71
+ if defined?(CarrierWave)
72
+ @pluck_all_cast_klass ||= klass
73
+ @pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, uploader| attributes.key?(key.to_s) }
74
+ @pluck_all_uploaders.each do |key, uploader|
74
75
  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
76
+ obj[key] = attributes[key_s = key.to_s]
77
+ @pluck_all_cast_need_columns.each{|s| obj[s] = attributes[s] } if @pluck_all_cast_need_columns
78
+ attributes[key_s] = obj.send(:_mounter, key).uploader #uploaders.first
78
79
  end
79
80
  end
80
81
  return attributes
@@ -1,3 +1,3 @@
1
1
  module PluckAll
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluck_all
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy