ar-find-in-batches-with-order 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +1 -1
- data/lib/ar-find-in-batches-with-order.rb +10 -9
- data/lib/ar-find-in-batches-with-order/version.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84f11eb8cbb3debcff137e68266fa7668bb938ae
|
4
|
+
data.tar.gz: ee5ee55a31b708b9117c39a65cd076da33693a20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a0e60008eb6e22060fee13095de11a85a0493ef5966caf5e0ced6f85493c132bd5398443b801efbb40e198557f300474ef2c072ab397e14b55061ba4c3f5247
|
7
|
+
data.tar.gz: a295e13733faae2843fb10b8dbdadea74f506793c6fe4436af7d432f9e560a60cbe468e6ad84672a594c9867ce44784085bb63a8399a73231db656b70d7be2b9
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Allows you to use find_each and find_each_in_batches with custom ordering.
|
4
4
|
|
5
|
-
This is useful if your domain knowledge allows
|
5
|
+
This is useful if your domain knowledge allows you to make assumptions about the order of your records. In the vanilla find_each/find_each_in_batches implementation, Rails disables custom ordering to ensure consistency in case ordering changes between batchings.
|
6
6
|
|
7
7
|
However, in many cases you know that this would never happen. For example, in acitivity feeds, you might want to batch-find activitites sorted by newest items. You know that new items cannot disrupt the ordering once batching started.
|
8
8
|
|
@@ -12,26 +12,27 @@ module ActiveRecord
|
|
12
12
|
batch_size = options.delete(:batch_size) || 1000
|
13
13
|
|
14
14
|
# try to deduct the property_key, but safer to specificy directly
|
15
|
-
property_key = options.delete(:property_key) || arel.orders.first.try(:value).try(:name)
|
15
|
+
property_key = options.delete(:property_key) || arel.orders.first.try(:value).try(:name) || arel.orders.first.try(:split,' ').try(:first)
|
16
|
+
sanitized_key = ActiveRecord::Base.connection.quote_column_name(property_key)
|
16
17
|
relation = relation.limit(batch_size)
|
17
18
|
|
18
19
|
# in strictmode, we return records with same values as the last record of the last batch
|
19
20
|
strict_mode = options.delete(:strict_mode) || true
|
20
21
|
|
21
22
|
|
22
|
-
records = start ? (direction == :desc ? relation.where(
|
23
|
+
records = start ? (direction == :desc ? relation.where("#{sanitized_key} <= ?", start).to_a : relation.where("#{sanitized_key} >= ?", start).to_a) : relation.to_a
|
23
24
|
|
24
|
-
|
25
|
+
while records.any?
|
25
26
|
records_size = records.size
|
26
|
-
|
27
|
+
|
27
28
|
yield records
|
28
29
|
|
29
30
|
|
30
31
|
break if records_size < batch_size
|
31
|
-
|
32
|
+
|
32
33
|
start = records.last.try(property_key)
|
33
34
|
|
34
|
-
records = strict_mode ? (direction == :desc ? relation.where(
|
35
|
+
records = strict_mode ? (direction == :desc ? relation.where("#{sanitized_key} <= ?", start).to_a : relation.where("#{sanitized_key} >= ?", start).to_a) : (direction == :desc ? relation.where("#{sanitized_key} < ?", start).to_a : relation.where("#{sanitized_key} > ?", start).to_a)
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
@@ -40,10 +41,10 @@ module ActiveRecord
|
|
40
41
|
last_record = nil
|
41
42
|
find_in_batches_with_order(options) do |records|
|
42
43
|
|
43
|
-
records.each do |record|
|
44
|
+
records.each do |record|
|
44
45
|
# we need to find the last record of the previous batch
|
45
46
|
next if last_record and (record != last_record)
|
46
|
-
if last_record
|
47
|
+
if last_record
|
47
48
|
last_record = nil
|
48
49
|
next
|
49
50
|
end
|
@@ -59,4 +60,4 @@ module ActiveRecord
|
|
59
60
|
class Relation
|
60
61
|
include FindInBatchesWithOrder
|
61
62
|
end
|
62
|
-
end
|
63
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-find-in-batches-with-order
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nam Chu Hoai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,8 +73,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
version: '0'
|
74
74
|
requirements: []
|
75
75
|
rubyforge_project:
|
76
|
-
rubygems_version: 2.
|
76
|
+
rubygems_version: 2.6.7
|
77
77
|
signing_key:
|
78
78
|
specification_version: 4
|
79
79
|
summary: Allow find_in_batches with custom order property
|
80
80
|
test_files: []
|
81
|
+
has_rdoc:
|