Index: test/limit_test.rb =================================================================== --- test/limit_test.rb (revision 323) +++ test/limit_test.rb (working copy) @@ -11,33 +11,45 @@ @sql.first end - specify "first with argument" do + specify "first with limit" do conditions = { :conditions => "users.name = 'jon'", :limit => '5' } User.expects(:find).with(:all, conditions) @sql.first(5) + conditions = { :conditions => "users.name = 'jon'", :limit => '7' } + User.expects(:find).with(:all, conditions) + @sql.first(7) end + + specify "first with offset" do + conditions = { :conditions => "users.name = 'jon'", :limit => '5', :offset => '6' } + User.expects(:find).with(:all, conditions) + @sql.first(5,6) + conditions = { :conditions => "users.name = 'jon'", :limit => '7', :offset => '8' } + User.expects(:find).with(:all, conditions) + @sql.first(7,8) + end specify "[] with one element" do - conditions = { :conditions => "users.name = 'jon'", :limit => '10, 1' } - User.expects(:find).with(:all, conditions) + conditions = { :conditions => "users.name = 'jon'", :limit => '1', :offset => '10' } + User.expects(:find).with(:first, conditions) @sql[10] end specify "[] with two elements" do - conditions = { :conditions => "users.name = 'jon'", :limit => '10, 20' } + conditions = { :conditions => "users.name = 'jon'", :limit => '20', :offset => '10' } User.expects(:find).with(:all, conditions) @sql[10, 20] end specify "slice is an alias of []" do - conditions = { :conditions => "users.name = 'jon'", :limit => '10, 20' } + conditions = { :conditions => "users.name = 'jon'", :limit => '20', :offset => '10' } User.expects(:find).with(:all, conditions) @sql.slice(10, 20) end specify "[] with range" do - conditions = { :conditions => "users.name = 'jon'", :limit => '10, 10' } + conditions = { :conditions => "users.name = 'jon'", :limit => '12', :offset => '10' } User.expects(:find).with(:all, conditions) - @sql[10..20] + @sql[10..22] end end Index: lib/ambition/limit.rb =================================================================== --- lib/ambition/limit.rb (revision 323) +++ lib/ambition/limit.rb (working copy) @@ -1,35 +1,21 @@ module Ambition module Limit def first(limit = 1, offset = nil) - query_context.add LimitProcessor.new(limit, offset) - find(limit == 1 ? :first : :all, query_context.to_hash) + find(limit == 1 ? :first : :all, query_context.to_hash(limit, offset)) end def [](offset, limit = nil) - return first(offset, limit) if limit + return first(limit, offset) if limit if offset.is_a? Range limit = offset.end limit -= 1 if offset.exclude_end? - first(offset.first, limit - offset.first) + first(limit - offset.first, offset.first) else - first(offset, 1) + first(1, offset) end end alias_method :slice, :[] end - class LimitProcessor - def initialize(*args) - @args = args - end - - def key - :limit - end - - def to_s - @args.compact * ', ' - end - end end Index: lib/ambition/query.rb =================================================================== --- lib/ambition/query.rb (revision 323) +++ lib/ambition/query.rb (working copy) @@ -27,7 +27,7 @@ ret end - def to_hash + def to_hash(limit = nil, offset = nil) keyed = keyed_clauses hash = {} @@ -44,21 +44,26 @@ hash[:order] = order.join(', ') end - if limit = keyed[:limit] - hash[:limit] = limit.join(', ') + if limit + hash[:limit] = limit.to_s end + + if offset + hash[:offset] = offset.to_s + end hash end - def to_s + def to_s(limit = nil, offset = nil) hash = keyed_clauses sql = [] sql << "JOIN #{hash[:includes].join(', ')}" unless hash[:includes].blank? sql << "WHERE #{hash[:conditions].join(' AND ')}" unless hash[:conditions].blank? sql << "ORDER BY #{hash[:order].join(', ')}" unless hash[:order].blank? - sql << "LIMIT #{hash[:limit].join(', ')}" unless hash[:limit].blank? + sql << "LIMIT #{limit}" if limit + sql << "OFFSET #{offset}" if offset @@select % [ @table_name, sql.join(' ') ] end