public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
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
dhh (author)
Wed Oct 26 06:20:02 -0700 2005
commit  0b92d38c0083c2077d0533014678ed017026fac1
tree    5127ff1b6bf11f9954932dbcec3c2b3bba6835ca
parent  07c494ae24b897bfa1d46f741b9ac14d3b480bc2
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Added :offset and :limit to the kinds of options that Base.constrain can use #2466 [duane.johnson@gmail.com]
0
+
0
 * Fixed handling of nil number columns on Oracle and cleaned up tests for Oracle in general #2555 [schoenm@earthlink.net]
0
 
0
 * Added quoted_true and quoted_false methods to db2_adapter and cleaned up tests for DB2 #2493 [maik schmidt]
...
809
810
811
812
 
 
813
814
815
...
883
884
885
 
 
886
887
888
...
809
810
811
 
812
813
814
815
816
...
884
885
886
887
888
889
890
891
0
@@ -809,7 +809,8 @@
0
       end
0
       
0
       # Add constraints to all queries to the same model in the given block.
0
- # Currently supported constraints are <tt>:conditions</tt> and <tt>:joins</tt>
0
+ # Currently supported constraints are <tt>:conditions</tt>, <tt>:joins</tt>,
0
+ # <tt>:offset</tt>, and <tt>:limit</tt>
0
       #
0
       # Article.constrain(:conditions => "blog_id = 1") do
0
       # Article.find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1
0
@@ -883,6 +884,8 @@
0
         end
0
 
0
         def add_limit!(sql, options)
0
+ options[:limit] ||= scope_constraints[:limit] if scope_constraints[:limit]
0
+ options[:offset] ||= scope_constraints[:offset] if scope_constraints[:offset]
0
           connection.add_limit_offset!(sql, options)
0
         end
0
         
...
1039
1040
1041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1042
1043
1044
...
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
0
@@ -1039,6 +1039,30 @@
0
     assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar} baz') }
0
   end
0
 
0
+ def test_constrain_conditions
0
+ developers = Developer.constrain(:conditions => 'salary > 90000') do
0
+ Developer.find(:all, :conditions => 'id < 5')
0
+ end
0
+ david = Developer.find(1)
0
+ assert !developers.include?(david) # David's salary is less than 90,000
0
+ assert_equal 3, developers.size
0
+ end
0
+
0
+ def test_constrain_limit_offset
0
+ developers = Developer.constrain(:limit => 3, :offset => 2) do
0
+ Developer.find(:all, :order => 'id')
0
+ end
0
+ david = Developer.find(1)
0
+ jamis = Developer.find(1)
0
+ assert !developers.include?(david) # David has id 1
0
+ assert !developers.include?(jamis) # Jamis has id 2
0
+ assert_equal 3, developers.size
0
+
0
+ # Now test without constraints to make sure we get the whole thing
0
+ developers = Developer.find(:all, :order => 'id')
0
+ assert_equal 10, developers.size
0
+ end
0
+
0
   # FIXME: this test ought to run, but it needs to run sandboxed so that it
0
   # doesn't b0rk the current test environment by undefing everything.
0
   #

Comments

    No one has commented yet.