Permalink
Browse files

Properly implemented limit.

  • Loading branch information...
1 parent ee212a1 commit a01f52a40be286876c1acbba66c072d0cc030c5b @dblock committed Mar 27, 2012
Showing with 11 additions and 2 deletions.
  1. +2 −2 lib/garb/model.rb
  2. +9 −0 test/unit/garb/model_test.rb
View
@@ -48,11 +48,11 @@ def all(profile, options = {}, &block)
limit = options.delete(:limit)
total = 0
options = {}
- options[:limit] = limit if limit
while ((rs = results(profile, options)) && rs.any?)
rs.each do |r|
- total += 1
yield r
+ total += 1
+ return self if limit and total >= limit
end
options[:offset] = total
end
@@ -141,6 +141,15 @@ class ModelTest < MiniTest::Unit::TestCase
assert_equal ['result', 'result'], results
end
+ should "be able to fetch multiple pages of results with a limit" do
+ results = []
+ @test_model.all(@profile, :limit => 1) do |result|
+ results << result
+ end
+ assert_equal ['result'], results
+ end
+
+
end
# should "return results as an array of the class it belongs to, if that class is an ActiveRecord descendant"

0 comments on commit a01f52a

Please sign in to comment.