Skip to content

Commit

Permalink
fix polymorphic association bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hooopo committed Jul 25, 2012
1 parent 05ac3a7 commit 3db405b
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
1.2.0 1.2.0
----- -----
* [clear cache after update_counters](https://github.com/csdn-dev/second_level_cache/commit/240dde81199124092e0e8ad0500c167ac146e301) * [clear cache after update_counters](https://github.com/csdn-dev/second_level_cache/commit/240dde81199124092e0e8ad0500c167ac146e301)

1.2.1
-----
* [fix polymorphic association bug]
28 changes: 14 additions & 14 deletions Gemfile.lock
@@ -1,36 +1,36 @@
PATH PATH
remote: . remote: .
specs: specs:
second_level_cache (1.1.1) second_level_cache (1.2.1)
activesupport (> 3.0) activesupport (~> 3.2.0)


GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
activemodel (3.2.1) activemodel (3.2.6)
activesupport (= 3.2.1) activesupport (= 3.2.6)
builder (~> 3.0.0) builder (~> 3.0.0)
activerecord (3.2.1) activerecord (3.2.6)
activemodel (= 3.2.1) activemodel (= 3.2.6)
activesupport (= 3.2.1) activesupport (= 3.2.6)
arel (~> 3.0.0) arel (~> 3.0.2)
tzinfo (~> 0.3.29) tzinfo (~> 0.3.29)
activesupport (3.2.1) activesupport (3.2.6)
i18n (~> 0.6) i18n (~> 0.6)
multi_json (~> 1.0) multi_json (~> 1.0)
arel (3.0.0) arel (3.0.2)
builder (3.0.0) builder (3.0.0)
i18n (0.6.0) i18n (0.6.0)
multi_json (1.0.4) multi_json (1.3.6)
rake (0.9.2.2) rake (0.9.2.2)
sqlite3 (1.3.5) sqlite3 (1.3.6)
tzinfo (0.3.31) tzinfo (0.3.33)


PLATFORMS PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
activerecord (> 3.0) activerecord (~> 3.2.0)
rake rake
second_level_cache! second_level_cache!
sqlite3 sqlite3
2 changes: 1 addition & 1 deletion init.rb
@@ -1,2 +1,2 @@
# encoding: utf-8 # -*- encoding : utf-8 -*-
require File.expand_path("../lib/second_level_cache", __FILE__) require File.expand_path("../lib/second_level_cache", __FILE__)
1 change: 1 addition & 0 deletions lib/second_level_cache.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'active_support/all' require 'active_support/all'
require 'second_level_cache/config' require 'second_level_cache/config'
require 'second_level_cache/marshal' require 'second_level_cache/marshal'
Expand Down
1 change: 1 addition & 0 deletions lib/second_level_cache/active_record.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'second_level_cache/active_record/base' require 'second_level_cache/active_record/base'
require 'second_level_cache/active_record/finder_methods' require 'second_level_cache/active_record/finder_methods'
require 'second_level_cache/active_record/persistence' require 'second_level_cache/active_record/persistence'
Expand Down
1 change: 1 addition & 0 deletions lib/second_level_cache/active_record/base.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module SecondLevelCache module SecondLevelCache
module ActiveRecord module ActiveRecord
module Base module Base
Expand Down
1 change: 1 addition & 0 deletions lib/second_level_cache/active_record/finder_methods.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'second_level_cache/arel/wheres' require 'second_level_cache/arel/wheres'


module SecondLevelCache module SecondLevelCache
Expand Down
1 change: 1 addition & 0 deletions lib/second_level_cache/active_record/persistence.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module SecondLevelCache module SecondLevelCache
module ActiveRecord module ActiveRecord
module Persistence module Persistence
Expand Down
5 changes: 3 additions & 2 deletions lib/second_level_cache/active_record/singular_association.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module SecondLevelCache module SecondLevelCache
module ActiveRecord module ActiveRecord
module Associations module Associations
Expand All @@ -10,8 +11,8 @@ module SingularAssociation
end end


def find_target_with_second_level_cache def find_target_with_second_level_cache
return find_target_without_second_level_cache unless association_class.second_level_cache_enabled? return find_target_without_second_level_cache unless klass.second_level_cache_enabled?
cache_record = association_class.read_second_level_cache(second_level_cache_key) cache_record = klass.read_second_level_cache(second_level_cache_key)
return cache_record.tap{|record| set_inverse_instance(record)} if cache_record return cache_record.tap{|record| set_inverse_instance(record)} if cache_record
record = find_target_without_second_level_cache record = find_target_without_second_level_cache
record.write_second_level_cache record.write_second_level_cache
Expand Down
2 changes: 1 addition & 1 deletion lib/second_level_cache/arel/wheres.rb
@@ -1,4 +1,4 @@
# encoding: utf-8 # -*- encoding : utf-8 -*-
module SecondLevelCache module SecondLevelCache
module Arel module Arel
class Wheres class Wheres
Expand Down
1 change: 1 addition & 0 deletions lib/second_level_cache/config.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module SecondLevelCache module SecondLevelCache
module Config module Config
extend self extend self
Expand Down
1 change: 1 addition & 0 deletions lib/second_level_cache/marshal.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
module Marshal module Marshal
class << self class << self
def load_with_constantize(value) def load_with_constantize(value)
Expand Down
4 changes: 2 additions & 2 deletions lib/second_level_cache/version.rb
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding : utf-8 -*-
module SecondLevelCache module SecondLevelCache
VERSION = "1.2.0" VERSION = "1.2.1"
end end
4 changes: 2 additions & 2 deletions second_level_cache.gemspec
Expand Up @@ -15,9 +15,9 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"] gem.require_paths = ["lib"]
gem.version = SecondLevelCache::VERSION 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 "sqlite3"
gem.add_development_dependency "rake" gem.add_development_dependency "rake"
end end
1 change: 1 addition & 0 deletions test/active_record/base_test.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'active_record/test_helper' require 'active_record/test_helper'


class ActiveRecord::BaseTest < Test::Unit::TestCase class ActiveRecord::BaseTest < Test::Unit::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/active_record/finder_methods_test.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'active_record/test_helper' require 'active_record/test_helper'


class ActiveRecord::FinderMethodsTest < Test::Unit::TestCase class ActiveRecord::FinderMethodsTest < Test::Unit::TestCase
Expand Down
3 changes: 3 additions & 0 deletions test/active_record/model/book.rb
@@ -1,11 +1,14 @@
# -*- encoding : utf-8 -*-
ActiveRecord::Base.connection.create_table(:books, :force => true) do |t| ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
t.string :title t.string :title
t.string :body t.string :body
t.integer :user_id t.integer :user_id
t.integer :images_count, :default => 0
end end


class Book < ActiveRecord::Base class Book < ActiveRecord::Base
acts_as_cached acts_as_cached


belongs_to :user, :counter_cache => true belongs_to :user, :counter_cache => true
has_many :images, :as => :imagable
end end
13 changes: 13 additions & 0 deletions test/active_record/model/image.rb
@@ -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

3 changes: 3 additions & 0 deletions test/active_record/model/user.rb
@@ -1,11 +1,14 @@
# -*- encoding : utf-8 -*-
ActiveRecord::Base.connection.create_table(:users, :force => true) do |t| ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
t.string :name t.string :name
t.string :email t.string :email
t.integer :books_count, :default => 0 t.integer :books_count, :default => 0
t.integer :images_count, :default => 0
end end


class User < ActiveRecord::Base class User < ActiveRecord::Base
acts_as_cached acts_as_cached


has_many :books has_many :books
has_many :images, :as => :imagable
end end
1 change: 1 addition & 0 deletions test/active_record/persistence_test.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'active_record/test_helper' require 'active_record/test_helper'


class ActiveRecord::PersistenceTest < Test::Unit::TestCase class ActiveRecord::PersistenceTest < Test::Unit::TestCase
Expand Down
24 changes: 24 additions & 0 deletions test/active_record/polymorphic_association_test.rb
@@ -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 change: 1 addition & 0 deletions test/active_record/second_level_cache_test.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'active_record/test_helper' require 'active_record/test_helper'


class ActiveRecord::SecondLevelCacheTest < Test::Unit::TestCase class ActiveRecord::SecondLevelCacheTest < Test::Unit::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/active_record/singular_association_test.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'active_record/test_helper' require 'active_record/test_helper'


class ActiveRecord::SingularAssociationTest < Test::Unit::TestCase class ActiveRecord::SingularAssociationTest < Test::Unit::TestCase
Expand Down
2 changes: 2 additions & 0 deletions test/active_record/test_helper.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'test_helper' require 'test_helper'
require 'active_record' require 'active_record'
require 'second_level_cache/active_record' require 'second_level_cache/active_record'
Expand Down Expand Up @@ -29,3 +30,4 @@ def teardown


require 'active_record/model/user' require 'active_record/model/user'
require 'active_record/model/book' require 'active_record/model/book'
require 'active_record/model/image'
1 change: 1 addition & 0 deletions test/test_helper.rb
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'rubygems' require 'rubygems'
require 'bundler/setup' require 'bundler/setup'
require 'second_level_cache' require 'second_level_cache'
Expand Down

0 comments on commit 3db405b

Please sign in to comment.