Created
November 8, 2011 09:15
-
-
Save elandesign/1347343 to your computer and use it in GitHub Desktop.
Add find_each and find_in_batches functionality to ActiveResource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Drop this into config/initializers | |
# The resource being consumed should honour the :page parameter (works great with will_paginate) | |
module ActiveResource | |
module Batches | |
module ClassMethods | |
def find_each(options = {}) | |
find_in_batches(options) do |batch| | |
batch.each do |entry| | |
yield entry | |
end | |
end | |
end | |
def find_in_batches(options = {}) | |
finished = false | |
page = 1 | |
while not finished do | |
begin | |
batch = find(:all, :params => options.merge(:page => page)) | |
if batch.empty? | |
finished = true | |
else | |
yield batch | |
end | |
page += 1 | |
end | |
end | |
end | |
end | |
end | |
end | |
ActiveResource::Base.extend(ActiveResource::Batches::ClassMethods) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment