Skip to content

Commit

Permalink
Use loaded collection, instead of read from cache, when use #size, #e…
Browse files Browse the repository at this point in the history
…mpty? and #any? from AssociationCollection. Added support for :uniq option.
  • Loading branch information
Luca Guidi committed Oct 21, 2008
1 parent 2441689 commit 5fd0579
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
* Use loaded collection, instead of read from cache, when use #size, #empty? and #any? from AssociationCollection. Added support for :uniq option.

* Reduced cache overhead using read instead of fetch for access to AssociationCollection. Enhanced Mocha expectactions.

# BEFORE
Expand Down
Expand Up @@ -113,8 +113,13 @@ def clear
# and you need to fetch that collection afterwards, it'll take one less SELECT query if you use length.
def size
if @reflection.options[:cached]
result = @owner.send(:cache_read, @reflection)
return result.to_ary.size if result
returning nil do
if @reflection.options[:uniq]
self.to_ary.uniq.size
else
self.to_ary.size
end
end
end

if @owner.new_record? || (loaded? && !@reflection.options[:uniq])
Expand Down
12 changes: 9 additions & 3 deletions test/active_record/associations/has_many_association_test.rb
Expand Up @@ -203,25 +203,31 @@ def test_should_update_cache_when_directly_assigning_a_new_collection
end

def test_should_use_cache_for_collection_size
cache.expects(:read).with("#{cache_key}/cached_posts").times(2).returns association_proxy
cache.expects(:read).with("#{cache_key}/cached_posts").returns association_proxy

assert_equal posts_by_author(:luca).size, authors(:luca).cached_posts.size
end

def test_should_use_cache_and_return_uniq_records_for_collection_size_on_uniq_option
cache.expects(:read).with("#{cache_key}/uniq_cached_posts").never # wuh?!

assert_equal posts_by_author(:luca).size, authors(:luca).uniq_cached_posts.size
end

def test_should_use_cache_for_collection_length
cache.expects(:read).with("#{cache_key}/cached_posts").returns association_proxy

assert_equal posts_by_author(:luca).length, authors(:luca).cached_posts.length
end

def test_should_use_cache_for_collection_empty
cache.expects(:read).with("#{cache_key}/cached_posts").times(2).returns association_proxy
cache.expects(:read).with("#{cache_key}/cached_posts").returns association_proxy

assert_equal posts_by_author(:luca).empty?, authors(:luca).cached_posts.empty?
end

def test_should_use_cache_for_collection_any
cache.expects(:read).with("#{cache_key}/cached_posts").times(2).returns association_proxy
cache.expects(:read).with("#{cache_key}/cached_posts").returns association_proxy

assert_equal posts_by_author(:luca).any?, authors(:luca).cached_posts.any?
end
Expand Down
1 change: 1 addition & 0 deletions test/models/author.rb
Expand Up @@ -2,6 +2,7 @@ class Author < ActiveRecord::Base
belongs_to :blog, :cached => true
has_many :posts
has_many :cached_posts, :cached => true, :class_name => 'Post'
has_many :uniq_cached_posts, :cached => true, :class_name => 'Post', :uniq => true
has_many :cached_dependent_posts, :cached => true, :class_name => 'Post', :dependent => :destroy
has_many :posts_with_comments, :class_name => 'Post', :include => :comments
has_many :cached_posts_with_comments, :class_name => 'Post', :include => :comments, :cached => true
Expand Down

0 comments on commit 5fd0579

Please sign in to comment.