pluck_each 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pluck_each/version.rb +1 -1
- data/lib/pluck_each.rb +10 -5
- data/spec/pluck_each_spec.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a808f819a95611a329a5a724924b41ff62e6bbb7
|
4
|
+
data.tar.gz: d2c9cc635521e5ffd1fbc5e8f09c9dcf23d3a2c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c72e5d7bef5021a2d9563fea25c94f9abbdd78f7c6ec2b497134a3d66816df7e902fe8b7efe9ae4817819ad379bbcf44cc4b16d0ba9c54bdac7e2031967b38f
|
7
|
+
data.tar.gz: d332a5c55fa5a6d969172d908e13f50e1f5c810d95cd64a146ebdf326201aeb406b180e5627cfa624fa722598e6393638271434befe8dee501521b71373ee451
|
data/lib/pluck_each/version.rb
CHANGED
data/lib/pluck_each.rb
CHANGED
@@ -20,6 +20,7 @@ module ActiveRecord
|
|
20
20
|
|
21
21
|
# Ensure the primary key is selected so we can use it as an offset
|
22
22
|
id_in_columns_requested = string_column_names.include?(primary_key)
|
23
|
+
id_only_field_requested = id_in_columns_requested && string_column_names.size == 1
|
23
24
|
string_column_names.unshift(primary_key) unless id_in_columns_requested
|
24
25
|
id_position_in_response = string_column_names.index(primary_key)
|
25
26
|
|
@@ -33,12 +34,16 @@ module ActiveRecord
|
|
33
34
|
batch = batch_relation.pluck(*string_column_names)
|
34
35
|
break if batch.empty?
|
35
36
|
|
36
|
-
primary_key_offset = batch.last
|
37
|
+
primary_key_offset = batch.last
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
if !id_only_field_requested
|
40
|
+
primary_key_offset = primary_key_offset.at(id_position_in_response)
|
41
|
+
|
42
|
+
unless id_in_columns_requested
|
43
|
+
batch.collect! do |record|
|
44
|
+
record.delete_at(id_position_in_response)
|
45
|
+
record
|
46
|
+
end
|
42
47
|
end
|
43
48
|
end
|
44
49
|
|
data/spec/pluck_each_spec.rb
CHANGED
@@ -30,6 +30,16 @@ describe PluckEach do
|
|
30
30
|
User.create(:first_name => '5', :last_name => '5')
|
31
31
|
end
|
32
32
|
|
33
|
+
it 'plucks :id when only field requested' do
|
34
|
+
values = []
|
35
|
+
User.all.pluck_each(:id) do |id|
|
36
|
+
values << id
|
37
|
+
end
|
38
|
+
|
39
|
+
values.sort!
|
40
|
+
values.must_equal User.all.pluck(:id).sort
|
41
|
+
end
|
42
|
+
|
33
43
|
it 'plucks only the fields requested' do
|
34
44
|
values = []
|
35
45
|
User.all.pluck_each(:first_name) do |first_name|
|