Permalink
Browse files

Added `offset` option for #find_all

  • Loading branch information...
1 parent c27be4e commit a665787261a7cf9b78ac1301c8e414a567684b29 @fredwu fredwu committed Jul 7, 2012
@@ -25,8 +25,8 @@ def find_first(options = {})
# @see OrmAdapter::Base#find_all
def find_all(options = {})
- conditions, order, limit = extract_conditions!(options)
- klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).limit(limit).all
+ conditions, order, limit, offset = extract_conditions!(options)
+ klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).limit(limit).offset(offset).all
end
# @see OrmAdapter::Base#create!
@@ -30,9 +30,10 @@ def find_first(options = {})
# @see OrmAdapter::Base#find_all
def find_all(options = {})
- conditions, order, limit = extract_conditions!(options)
+ conditions, order, limit, offset = extract_conditions!(options)
opts = { :conditions => conditions, :order => order_clause(order) }
opts = opts.merge({ :limit => limit }) unless limit.nil?
+ opts = opts.merge({ :offset => offset }) unless offset.nil?
klass.all opts
end
@@ -31,9 +31,10 @@ def find_first(conditions = {})
# @see OrmAdapter::Base#find_all
def find_all(conditions = {})
- conditions, order, limit = extract_conditions!(conditions)
+ conditions, order, limit, offset = extract_conditions!(conditions)
conditions = conditions.merge(:sort => order) unless order.nil?
conditions = conditions.merge(:limit => limit) unless limit.nil?
+ conditions = conditions.merge(:offset => offset) unless limit.nil? || offset.nil?
klass.all(conditions_to_fields(conditions))
end
@@ -30,8 +30,8 @@ def find_first(options = {})
# @see OrmAdapter::Base#find_all
def find_all(options = {})
- conditions, order, limit = extract_conditions!(options)
- klass.where(conditions_to_fields(conditions)).order_by(order).limit(limit)
+ conditions, order, limit, offset = extract_conditions!(options)
+ klass.where(conditions_to_fields(conditions)).order_by(order).limit(limit).offset(offset)
end
# @see OrmAdapter::Base#create!
View
@@ -89,13 +89,15 @@ def wrap_key(key)
end
# given an options hash,
- # with optional :conditions, :order and :limit keys,
- # returns conditions, normalized order and limit
+ # with optional :conditions, :order, :limit and :offset keys,
+ # returns conditions, normalized order, limit and offset
def extract_conditions!(options = {})
- order = normalize_order(options.delete(:order))
- limit = options.delete(:limit)
+ order = normalize_order(options.delete(:order))
+ limit = options.delete(:limit)
+ offset = options.delete(:offset)
conditions = options.delete(:conditions) || options
- [conditions, order, limit]
+
+ [conditions, order, limit, offset]
end
# given an order argument, returns an array of pairs, with each pair containing the attribute, and :asc or :desc
@@ -7,29 +7,38 @@
let(:conditions) { {:foo => 'bar'} }
let(:order) { [[:foo, :asc]] }
let(:limit) { 1 }
+ let(:offset) { 2 }
- it "(<conditions>) should return [<conditions>, [], nil]" do
- subject.send(:extract_conditions!, conditions).should == [conditions, [], nil]
+ it "(<conditions>)" do
+ subject.send(:extract_conditions!, conditions).should == [conditions, [], nil, nil]
end
- it "(:conditions => <conditions>) should return [<conditions>, [], nil]" do
- subject.send(:extract_conditions!, :conditions => conditions).should == [conditions, [], nil]
+ it "(:conditions => <conditions>)" do
+ subject.send(:extract_conditions!, :conditions => conditions).should == [conditions, [], nil, nil]
end
- it "(:order => <order>) should return [{}, <order>, nil]" do
- subject.send(:extract_conditions!, :order => order).should == [{}, order, nil]
+ it "(:order => <order>)" do
+ subject.send(:extract_conditions!, :order => order).should == [{}, order, nil, nil]
end
- it "(:limit => <limit>) should return [{}, [], <limit>]" do
- subject.send(:extract_conditions!, :limit => limit).should == [{}, [], limit]
+ it "(:limit => <limit>)" do
+ subject.send(:extract_conditions!, :limit => limit).should == [{}, [], limit, nil]
end
- it "(:conditions => <conditions>, :order => <order>) should return [<conditions>, <order>, nil]" do
- subject.send(:extract_conditions!, :conditions => conditions, :order => order).should == [conditions, order, nil]
+ it "(:offset => <offset>)" do
+ subject.send(:extract_conditions!, :offset => offset).should == [{}, [], nil, offset]
end
- it "(:conditions => <conditions>, :limit => <limit>) should return [<conditions>, [], <limit>]" do
- subject.send(:extract_conditions!, :conditions => conditions, :limit => limit).should == [conditions, [], limit]
+ it "(:conditions => <conditions>, :order => <order>)" do
+ subject.send(:extract_conditions!, :conditions => conditions, :order => order).should == [conditions, order, nil, nil]
+ end
+
+ it "(:conditions => <conditions>, :limit => <limit>)" do
+ subject.send(:extract_conditions!, :conditions => conditions, :limit => limit).should == [conditions, [], limit, nil]
+ end
+
+ it "(:conditions => <conditions>, :offset => <offset>)" do
+ subject.send(:extract_conditions!, :conditions => conditions, :offset => offset).should == [conditions, [], nil, offset]
end
describe "#valid_object?" do
@@ -183,6 +183,17 @@ def reload_model(model)
user_adapter.find_all(:limit => 2).should == [user1, user2]
end
end
+
+ describe "(:offset => <offset number>) with limit (as DataMapper doesn't allow offset on its own)" do
+ it "should return an offset set of matching models" do
+ user1 = create_model(user_class, :name => "Fred", :rating => 1)
+ user2 = create_model(user_class, :name => "Fred", :rating => 2)
+ user3 = create_model(user_class, :name => "Betty", :rating => 1)
+ user_adapter.find_all(:limit => 3, :offset => 0).should == [user1, user2, user3]
+ user_adapter.find_all(:limit => 3, :offset => 1).should == [user2, user3]
+ user_adapter.find_all(:limit => 1, :offset => 1).should == [user2]
+ end
+ end
end
describe "#create!(attributes)" do

0 comments on commit a665787

Please sign in to comment.