Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added :offset and :limit to the kinds of options that Base.constrain …

…can use (closes #2466) [duane.johnson@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2748 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 0b92d38c0083c2077d0533014678ed017026fac1 1 parent 07c494a
@dhh dhh authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added :offset and :limit to the kinds of options that Base.constrain can use #2466 [duane.johnson@gmail.com]
+
* Fixed handling of nil number columns on Oracle and cleaned up tests for Oracle in general #2555 [schoenm@earthlink.net]
* Added quoted_true and quoted_false methods to db2_adapter and cleaned up tests for DB2 #2493 [maik schmidt]
View
5 activerecord/lib/active_record/base.rb
@@ -809,7 +809,8 @@ def silence
end
# Add constraints to all queries to the same model in the given block.
- # Currently supported constraints are <tt>:conditions</tt> and <tt>:joins</tt>
+ # Currently supported constraints are <tt>:conditions</tt>, <tt>:joins</tt>,
+ # <tt>:offset</tt>, and <tt>:limit</tt>
#
# Article.constrain(:conditions => "blog_id = 1") do
# Article.find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1
@@ -883,6 +884,8 @@ def construct_finder_sql(options)
end
def add_limit!(sql, options)
+ options[:limit] ||= scope_constraints[:limit] if scope_constraints[:limit]
+ options[:offset] ||= scope_constraints[:offset] if scope_constraints[:offset]
connection.add_limit_offset!(sql, options)
end
View
24 activerecord/test/base_test.rb
@@ -1039,6 +1039,30 @@ def test_interpolate_sql
assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar} baz') }
end
+ def test_constrain_conditions
+ developers = Developer.constrain(:conditions => 'salary > 90000') do
+ Developer.find(:all, :conditions => 'id < 5')
+ end
+ david = Developer.find(1)
+ assert !developers.include?(david) # David's salary is less than 90,000
+ assert_equal 3, developers.size
+ end
+
+ def test_constrain_limit_offset
+ developers = Developer.constrain(:limit => 3, :offset => 2) do
+ Developer.find(:all, :order => 'id')
+ end
+ david = Developer.find(1)
+ jamis = Developer.find(1)
+ assert !developers.include?(david) # David has id 1
+ assert !developers.include?(jamis) # Jamis has id 2
+ assert_equal 3, developers.size
+
+ # Now test without constraints to make sure we get the whole thing
+ developers = Developer.find(:all, :order => 'id')
+ assert_equal 10, developers.size
+ end
+
# FIXME: this test ought to run, but it needs to run sandboxed so that it
# doesn't b0rk the current test environment by undefing everything.
#
Please sign in to comment.
Something went wrong with that request. Please try again.