ar-find-in-batches-with-order 0.0.1 → 0.0.2

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: 9a925a70757e5aba077a829c8390721aafb758e6
4
- data.tar.gz: 6662ce7e8fb81f22f807be5e5b3c7fbd42759258
3
+ metadata.gz: 84f11eb8cbb3debcff137e68266fa7668bb938ae
4
+ data.tar.gz: ee5ee55a31b708b9117c39a65cd076da33693a20
5
5
  SHA512:
6
- metadata.gz: 4ba8c62855896049ebf287bf1b5435e40bf11570561360e20ce1bd5af543c216a7ae817b31a7d67339ebd21b1c496922887a8b381183510e2df7b72d7b5a3d2a
7
- data.tar.gz: 68704bce9b02c094c168ae4320fc575f7091056c0f462d8061bdae449e7921d8a444250caf9cd6f876bd4acc3439579075623536b2f70bda7289431c44bb8d3e
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 youo 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.
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(table[property_key].lteq(start)).to_a : relation.where(table[property_key].gteq(start)).to_a) : relation.to_a
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
- while records.any?
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(table[property_key].lteq(start)).to_a : relation.where(table[property_key].gteq(start)).to_a) : (direction == :desc ? relation.where(table[property_key].lt(start)).to_a : relation.where(table[property_key].gt(start)).to_a)
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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module FindInBatchesWithOrder
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
- end
5
+ 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.1
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: 2014-03-18 00:00:00.000000000 Z
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.2.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: