Skip to content

Instantly share code, notes, and snippets.

@elandesign
Created November 8, 2011 09:15
Show Gist options
  • Save elandesign/1347343 to your computer and use it in GitHub Desktop.
Save elandesign/1347343 to your computer and use it in GitHub Desktop.
Add find_each and find_in_batches functionality to ActiveResource
# 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