Permalink
Browse files

fix polymorphic association bug

  • Loading branch information...
hooopo committed Jul 25, 2012
1 parent 05ac3a7 commit 3db405b06f4cd1288bb41e03d0eddc0f2c3bdc3c
View
@@ -1,3 +1,7 @@
1.2.0
-----
* [clear cache after update_counters](https://github.com/csdn-dev/second_level_cache/commit/240dde81199124092e0e8ad0500c167ac146e301)
+
+1.2.1
+-----
+* [fix polymorphic association bug]
View
@@ -1,36 +1,36 @@
PATH
remote: .
specs:
- second_level_cache (1.1.1)
- activesupport (> 3.0)
+ second_level_cache (1.2.1)
+ activesupport (~> 3.2.0)
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.2.1)
- activesupport (= 3.2.1)
+ activemodel (3.2.6)
+ activesupport (= 3.2.6)
builder (~> 3.0.0)
- activerecord (3.2.1)
- activemodel (= 3.2.1)
- activesupport (= 3.2.1)
- arel (~> 3.0.0)
+ activerecord (3.2.6)
+ activemodel (= 3.2.6)
+ activesupport (= 3.2.6)
+ arel (~> 3.0.2)
tzinfo (~> 0.3.29)
- activesupport (3.2.1)
+ activesupport (3.2.6)
i18n (~> 0.6)
multi_json (~> 1.0)
- arel (3.0.0)
+ arel (3.0.2)
builder (3.0.0)
i18n (0.6.0)
- multi_json (1.0.4)
+ multi_json (1.3.6)
rake (0.9.2.2)
- sqlite3 (1.3.5)
- tzinfo (0.3.31)
+ sqlite3 (1.3.6)
+ tzinfo (0.3.33)
PLATFORMS
ruby
DEPENDENCIES
- activerecord (> 3.0)
+ activerecord (~> 3.2.0)
rake
second_level_cache!
sqlite3
View
@@ -1,2 +1,2 @@
-# encoding: utf-8
+# -*- encoding : utf-8 -*-
require File.expand_path("../lib/second_level_cache", __FILE__)
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'active_support/all'
require 'second_level_cache/config'
require 'second_level_cache/marshal'
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'second_level_cache/active_record/base'
require 'second_level_cache/active_record/finder_methods'
require 'second_level_cache/active_record/persistence'
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module SecondLevelCache
module ActiveRecord
module Base
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'second_level_cache/arel/wheres'
module SecondLevelCache
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module SecondLevelCache
module ActiveRecord
module Persistence
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module SecondLevelCache
module ActiveRecord
module Associations
@@ -10,8 +11,8 @@ module SingularAssociation
end
def find_target_with_second_level_cache
- return find_target_without_second_level_cache unless association_class.second_level_cache_enabled?
- cache_record = association_class.read_second_level_cache(second_level_cache_key)
+ return find_target_without_second_level_cache unless klass.second_level_cache_enabled?
+ cache_record = klass.read_second_level_cache(second_level_cache_key)
return cache_record.tap{|record| set_inverse_instance(record)} if cache_record
record = find_target_without_second_level_cache
record.write_second_level_cache
@@ -1,4 +1,4 @@
-# encoding: utf-8
+# -*- encoding : utf-8 -*-
module SecondLevelCache
module Arel
class Wheres
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module SecondLevelCache
module Config
extend self
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module Marshal
class << self
def load_with_constantize(value)
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# -*- encoding : utf-8 -*-
module SecondLevelCache
- VERSION = "1.2.0"
+ VERSION = "1.2.1"
end
@@ -15,9 +15,9 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = SecondLevelCache::VERSION
- gem.add_runtime_dependency "activesupport", ["> 3.0"]
+ gem.add_runtime_dependency "activesupport", ["~> 3.2.0"]
- gem.add_development_dependency "activerecord", ["> 3.0"]
+ gem.add_development_dependency "activerecord", ["~> 3.2.0"]
gem.add_development_dependency "sqlite3"
gem.add_development_dependency "rake"
end
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'active_record/test_helper'
class ActiveRecord::BaseTest < Test::Unit::TestCase
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'active_record/test_helper'
class ActiveRecord::FinderMethodsTest < Test::Unit::TestCase
@@ -1,11 +1,14 @@
+# -*- encoding : utf-8 -*-
ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
t.string :title
t.string :body
t.integer :user_id
+ t.integer :images_count, :default => 0
end
class Book < ActiveRecord::Base
acts_as_cached
belongs_to :user, :counter_cache => true
+ has_many :images, :as => :imagable
end
@@ -0,0 +1,13 @@
+# -*- encoding : utf-8 -*-
+ActiveRecord::Base.connection.create_table(:images, :force => true) do |t|
+ t.string :url
+ t.string :imagable_type
+ t.integer :imagable_id
+end
+
+class Image < ActiveRecord::Base
+ acts_as_cached
+
+ belongs_to :imagable, :polymorphic => true, :counter_cache => true
+end
+
@@ -1,11 +1,14 @@
+# -*- encoding : utf-8 -*-
ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
t.string :name
t.string :email
t.integer :books_count, :default => 0
+ t.integer :images_count, :default => 0
end
class User < ActiveRecord::Base
acts_as_cached
has_many :books
+ has_many :images, :as => :imagable
end
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'active_record/test_helper'
class ActiveRecord::PersistenceTest < Test::Unit::TestCase
@@ -0,0 +1,24 @@
+# -*- encoding : utf-8 -*-
+require 'active_record/test_helper'
+
+class ActiveRecord::PolymorphicAssociationTest < Test::Unit::TestCase
+ def setup
+ @user = User.create :name => 'csdn', :email => 'test@csdn.com'
+ end
+
+ def test_should_get_cache_when_use_polymorphic_association
+ image = @user.images.create
+
+ no_connection do
+ assert_equal @user, image.imagable
+ end
+ end
+
+ def test_should_write_polymorphic_association_cache
+ image = @user.images.create
+ @user.expire_second_level_cache
+ assert_nil User.read_second_level_cache(@user.id)
+ assert_equal @user, image.imagable
+ end
+end
+
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'active_record/test_helper'
class ActiveRecord::SecondLevelCacheTest < Test::Unit::TestCase
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'active_record/test_helper'
class ActiveRecord::SingularAssociationTest < Test::Unit::TestCase
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'test_helper'
require 'active_record'
require 'second_level_cache/active_record'
@@ -29,3 +30,4 @@ def teardown
require 'active_record/model/user'
require 'active_record/model/book'
+require 'active_record/model/image'
View
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'rubygems'
require 'bundler/setup'
require 'second_level_cache'

0 comments on commit 3db405b

Please sign in to comment.