Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2748

Show
Ignore:
Timestamp:
10/26/05 13:20:02 (3 years ago)
Author:
david
Message:

Added :offset and :limit to the kinds of options that Base.constrain can use (closes #2466) [duane.johnson@gmail.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r2741 r2748  
    11*SVN* 
     2 
     3* Added :offset and :limit to the kinds of options that Base.constrain can use #2466 [duane.johnson@gmail.com] 
    24 
    35* Fixed handling of nil number columns on Oracle and cleaned up tests for Oracle in general #2555 [schoenm@earthlink.net] 
  • trunk/activerecord/lib/active_record/base.rb

    r2730 r2748  
    810810       
    811811      # Add constraints to all queries to the same model in the given block. 
    812       # Currently supported constraints are <tt>:conditions</tt> and <tt>:joins</tt>  
     812      # Currently supported constraints are <tt>:conditions</tt>, <tt>:joins</tt>, 
     813      # <tt>:offset</tt>, and <tt>:limit</tt>  
    813814      # 
    814815      #   Article.constrain(:conditions => "blog_id = 1") do 
     
    884885 
    885886        def add_limit!(sql, options) 
     887          options[:limit]  ||= scope_constraints[:limit]  if scope_constraints[:limit] 
     888          options[:offset] ||= scope_constraints[:offset] if scope_constraints[:offset] 
    886889          connection.add_limit_offset!(sql, options) 
    887890        end 
  • trunk/activerecord/test/base_test.rb

    r2741 r2748  
    10401040  end 
    10411041 
     1042  def test_constrain_conditions 
     1043    developers = Developer.constrain(:conditions => 'salary > 90000') do 
     1044      Developer.find(:all, :conditions => 'id < 5') 
     1045    end 
     1046    david = Developer.find(1) 
     1047    assert !developers.include?(david) # David's salary is less than 90,000 
     1048    assert_equal 3, developers.size 
     1049  end 
     1050   
     1051  def test_constrain_limit_offset 
     1052    developers = Developer.constrain(:limit => 3, :offset => 2) do 
     1053      Developer.find(:all, :order => 'id') 
     1054    end     
     1055    david = Developer.find(1) 
     1056    jamis = Developer.find(1) 
     1057    assert !developers.include?(david) # David has id 1 
     1058    assert !developers.include?(jamis) # Jamis has id 2 
     1059    assert_equal 3, developers.size 
     1060     
     1061    # Now test without constraints to make sure we get the whole thing 
     1062    developers = Developer.find(:all, :order => 'id') 
     1063    assert_equal 10, developers.size 
     1064  end 
     1065   
    10421066  # FIXME: this test ought to run, but it needs to run sandboxed so that it 
    10431067  # doesn't b0rk the current test environment by undefing everything.