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

Ticket #9923: lazyattr.diff

File lazyattr.diff, 5.6 kB (added by RubyRedRick, 1 year ago)

Patch to add :lazy_attributes option to ActiveRecord::Base

  • test/associations/lazy_association_test.rb

    old new  
     1require 'abstract_unit' 
     2require 'fixtures/post' 
     3require 'fixtures/comment' 
     4require 'fixtures/author' 
     5require 'fixtures/category' 
     6require 'fixtures/categorization' 
     7require 'fixtures/company' 
     8require 'fixtures/topic' 
     9require 'fixtures/reply' 
     10 
     11class LazyAssociationTest < Test::Unit::TestCase 
     12  fixtures :authors, :mixins, :companies, :posts, :topics 
     13 
     14  def test_lazy_association_loading 
     15    authors = Author.find(:all, :include=> :posts, :conditions => ['posts.type = ?', "Post"]) 
     16    assert_not_equal(0 , authors.length) 
     17    authors.each { |author|  
     18      assert author.send(:instance_variables).include? "@posts" } 
     19    authors = Author.find(:all, :include=> :posts, :lazy_associations => true, :conditions => ['posts.type = ?', "Post"]) 
     20    assert_not_equal(0 , authors.length) 
     21    #    authors.each { |author| assert_queries {author.posts} } 
     22    authors.each { |author| 
     23       assert !(author.send(:instance_variables).include? "@posts") } 
     24  end  
     25end 
     26 
  • lib/active_record/associations.rb

    old new  
    11151115         
    11161116        def find_with_associations(options = {}) 
    11171117          catch :invalid_query do 
    1118             join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins]
     1118            join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins], options[:lazy_associations]
    11191119            rows = select_all_rows(options, join_dependency) 
    11201120            return join_dependency.instantiate(rows) 
    11211121          end 
     
    13691369        class JoinDependency # :nodoc: 
    13701370          attr_reader :joins, :reflections, :table_aliases 
    13711371 
    1372           def initialize(base, associations, joins
     1372          def initialize(base, associations, joins, lazy_associations = false
    13731373            @joins                 = [JoinBase.new(base, joins)] 
    13741374            @associations          = associations 
     1375            @lazy_associations = lazy_associations  
    13751376            @reflections           = [] 
    13761377            @base_records_hash     = {} 
    13771378            @base_records_in_order = [] 
     
    13941395              unless @base_records_hash[primary_id] 
    13951396                @base_records_in_order << (@base_records_hash[primary_id] = join_base.instantiate(row)) 
    13961397              end 
    1397               construct(@base_records_hash[primary_id], @associations, join_associations.dup, row) 
     1398              construct(@base_records_hash[primary_id], @associations, join_associations.dup, row)  unless @lazy_associations 
    13981399            end 
    13991400            return @base_records_in_order 
    14001401          end 
  • lib/active_record/base.rb

    old new  
    392392      # * <tt>:readonly</tt>: Mark the returned records read-only so they cannot be saved or updated. 
    393393      # * <tt>:lock</tt>: An SQL fragment like "FOR UPDATE" or "LOCK IN SHARE MODE". 
    394394      #   :lock => true gives connection's default exclusive lock, usually "FOR UPDATE". 
     395      # * <tt>:lazy_associations</tt> A boolean value, default is false.  Normally when the :include option is used to add associated  
     396      #    tables to the query, the cooresponding association collections will be created.  This is normally desired, but it may 
     397      #    result unnecessary processing and memory usage when the inclusion is solely to aid in selecting base records. 
     398      #    If lazy_associations is true then the tables for any associations will participate in the sql query so that their fields can 
     399      #    be used in the :conditions option, but the association collection will not be created by find. 
    395400      # 
    396401      # Examples for find by id: 
    397402      #   Person.find(1)       # returns the object for ID = 1 
     
    414419      #   Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50) 
    415420      #   Person.find(:all, :offset => 10, :limit => 10) 
    416421      #   Person.find(:all, :include => [ :account, :friends ]) 
     422      #   Person.find(:all, :include => :account, :lazy_associations => true, :conditions => ['accounts.last_accessed_time < ?', 1.year.ago]) 
    417423      #   Person.find(:all, :group => "category") 
     424      #    
     425      # Example for find_all: with :lazy_associations => true 
    418426      # 
     427      #   Person.find(:all, :include => :account, :lazy_associations => true, :conditions => ['accounts.last_accessed_time < ?', 1.year.ago])  
     428      # 
    419429      # Example for find with a lock. Imagine two concurrent transactions: 
    420430      # each will read person.visits == 2, add 1 to it, and save, resulting 
    421431      # in two saves of person.visits = 3.  By locking the row, the second 
     
    16581668          end 
    16591669        end 
    16601670 
    1661         VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset, 
     1671        VALID_FIND_OPTIONS = [ :conditions, :lazy_associations, :include, :joins, :limit, :offset, 
    16621672                               :order, :select, :readonly, :group, :from, :lock ] 
    16631673 
    16641674        def validate_find_options(options) #:nodoc: