diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-29 17:53:10 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-29 17:53:10 -0200 |
commit | fec1028d088294c30f568e46db3bbd054a9330ff (patch) | |
tree | 93ee044f18873c3daa3acf86d338c6785b95e54a /activerecord | |
parent | 2c964ac59194c340660986423b4aa93de447c37b (diff) | |
parent | 0aad463cfbe9853fd3a7ab0f8e4e0a34715fd62c (diff) | |
download | rails-fec1028d088294c30f568e46db3bbd054a9330ff.tar.gz rails-fec1028d088294c30f568e46db3bbd054a9330ff.tar.bz2 rails-fec1028d088294c30f568e46db3bbd054a9330ff.zip |
Merge pull request #13201 from marcandre/find_in_batch_enumerator
`find_in_batches` now returns an `Enumerator`
Conflicts:
activerecord/CHANGELOG.md
activerecord/lib/active_record/relation/batches.rb
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/batches.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/batches_test.rb | 13 |
3 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 6226ce844f..fe0d7b2b35 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* `find_in_batches` now returns an `Enumerator` when called without a block, so that it + can be chained with other `Enumerable` methods. + + *Marc-André Lafortune* + * `enum` now raises on "dangerous" name conflicts Dangerous name conflicts includes instance or class method conflicts diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index e98b4712f5..dfcfef2ad2 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -64,6 +64,14 @@ module ActiveRecord # group.each { |person| person.party_all_night! } # end # + # If you do not provide a block to #find_in_batches, it will return an Enumerator + # for chaining with other methods: + # + # Person.find_in_batches.with_index do |group, batch| + # puts "Processing group ##{batch}" + # group.each(&:recover_from_last_night!) + # end + # # To be yielded each record one by one, use #find_each instead. # # ==== Options @@ -88,6 +96,7 @@ module ActiveRecord # the batch sizes. def find_in_batches(options = {}) options.assert_valid_keys(:start, :batch_size) + return to_enum(:find_in_batches, options) unless block_given? relation = self diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index ebb36e4940..1161b57514 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -172,4 +172,17 @@ class EachTest < ActiveRecord::TestCase end end end + + def test_find_in_batches_should_return_an_enumerator + enum = nil + assert_queries(0) do + enum = Post.find_in_batches(:batch_size => 1) + end + assert_queries(4) do + enum.first(4) do |batch| + assert_kind_of Array, batch + assert_kind_of Post, batch.first + end + end + end end |