diff --git a/CHANGELOG.md b/CHANGELOG.md index b8dabec..3c9fc8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/Gemfile.lock b/Gemfile.lock index bae14e9..f4ab550 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/init.rb b/init.rb index 8236da5..719b846 100644 --- a/init.rb +++ b/init.rb @@ -1,2 +1,2 @@ -# encoding: utf-8 +# -*- encoding : utf-8 -*- require File.expand_path("../lib/second_level_cache", __FILE__) diff --git a/lib/second_level_cache.rb b/lib/second_level_cache.rb index fdd8288..d92630a 100644 --- a/lib/second_level_cache.rb +++ b/lib/second_level_cache.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'active_support/all' require 'second_level_cache/config' require 'second_level_cache/marshal' diff --git a/lib/second_level_cache/active_record.rb b/lib/second_level_cache/active_record.rb index 20ebb02..240857c 100644 --- a/lib/second_level_cache/active_record.rb +++ b/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/finder_methods' require 'second_level_cache/active_record/persistence' diff --git a/lib/second_level_cache/active_record/base.rb b/lib/second_level_cache/active_record/base.rb index 9b1d13f..edaf6c2 100644 --- a/lib/second_level_cache/active_record/base.rb +++ b/lib/second_level_cache/active_record/base.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- module SecondLevelCache module ActiveRecord module Base diff --git a/lib/second_level_cache/active_record/finder_methods.rb b/lib/second_level_cache/active_record/finder_methods.rb index c9fb1c2..2c832b1 100644 --- a/lib/second_level_cache/active_record/finder_methods.rb +++ b/lib/second_level_cache/active_record/finder_methods.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'second_level_cache/arel/wheres' module SecondLevelCache diff --git a/lib/second_level_cache/active_record/persistence.rb b/lib/second_level_cache/active_record/persistence.rb index 928ebe3..7db6e03 100644 --- a/lib/second_level_cache/active_record/persistence.rb +++ b/lib/second_level_cache/active_record/persistence.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- module SecondLevelCache module ActiveRecord module Persistence diff --git a/lib/second_level_cache/active_record/singular_association.rb b/lib/second_level_cache/active_record/singular_association.rb index 5867054..21a5762 100644 --- a/lib/second_level_cache/active_record/singular_association.rb +++ b/lib/second_level_cache/active_record/singular_association.rb @@ -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 diff --git a/lib/second_level_cache/arel/wheres.rb b/lib/second_level_cache/arel/wheres.rb index 0602525..f08887d 100644 --- a/lib/second_level_cache/arel/wheres.rb +++ b/lib/second_level_cache/arel/wheres.rb @@ -1,4 +1,4 @@ -# encoding: utf-8 +# -*- encoding : utf-8 -*- module SecondLevelCache module Arel class Wheres diff --git a/lib/second_level_cache/config.rb b/lib/second_level_cache/config.rb index 70b6877..aed25e4 100644 --- a/lib/second_level_cache/config.rb +++ b/lib/second_level_cache/config.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- module SecondLevelCache module Config extend self diff --git a/lib/second_level_cache/marshal.rb b/lib/second_level_cache/marshal.rb index 3cb8015..67ec1aa 100644 --- a/lib/second_level_cache/marshal.rb +++ b/lib/second_level_cache/marshal.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- module Marshal class << self def load_with_constantize(value) diff --git a/lib/second_level_cache/version.rb b/lib/second_level_cache/version.rb index c067b2a..d3b8d41 100644 --- a/lib/second_level_cache/version.rb +++ b/lib/second_level_cache/version.rb @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- encoding : utf-8 -*- module SecondLevelCache - VERSION = "1.2.0" + VERSION = "1.2.1" end diff --git a/second_level_cache.gemspec b/second_level_cache.gemspec index a507eb6..b04c0d1 100644 --- a/second_level_cache.gemspec +++ b/second_level_cache.gemspec @@ -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 diff --git a/test/active_record/base_test.rb b/test/active_record/base_test.rb index a4ad818..3c1c8be 100644 --- a/test/active_record/base_test.rb +++ b/test/active_record/base_test.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'active_record/test_helper' class ActiveRecord::BaseTest < Test::Unit::TestCase diff --git a/test/active_record/finder_methods_test.rb b/test/active_record/finder_methods_test.rb index b5afb6e..2de35c2 100644 --- a/test/active_record/finder_methods_test.rb +++ b/test/active_record/finder_methods_test.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'active_record/test_helper' class ActiveRecord::FinderMethodsTest < Test::Unit::TestCase diff --git a/test/active_record/model/book.rb b/test/active_record/model/book.rb index ef826c7..aa614f2 100644 --- a/test/active_record/model/book.rb +++ b/test/active_record/model/book.rb @@ -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 diff --git a/test/active_record/model/image.rb b/test/active_record/model/image.rb new file mode 100644 index 0000000..2653130 --- /dev/null +++ b/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 + diff --git a/test/active_record/model/user.rb b/test/active_record/model/user.rb index 4913e8c..7cff2af 100644 --- a/test/active_record/model/user.rb +++ b/test/active_record/model/user.rb @@ -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 diff --git a/test/active_record/persistence_test.rb b/test/active_record/persistence_test.rb index 9731928..f812775 100644 --- a/test/active_record/persistence_test.rb +++ b/test/active_record/persistence_test.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'active_record/test_helper' class ActiveRecord::PersistenceTest < Test::Unit::TestCase diff --git a/test/active_record/polymorphic_association_test.rb b/test/active_record/polymorphic_association_test.rb new file mode 100644 index 0000000..932e192 --- /dev/null +++ b/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 + diff --git a/test/active_record/second_level_cache_test.rb b/test/active_record/second_level_cache_test.rb index fe34807..1508c4f 100644 --- a/test/active_record/second_level_cache_test.rb +++ b/test/active_record/second_level_cache_test.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'active_record/test_helper' class ActiveRecord::SecondLevelCacheTest < Test::Unit::TestCase diff --git a/test/active_record/singular_association_test.rb b/test/active_record/singular_association_test.rb index 4bc84b9..f94ca53 100644 --- a/test/active_record/singular_association_test.rb +++ b/test/active_record/singular_association_test.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'active_record/test_helper' class ActiveRecord::SingularAssociationTest < Test::Unit::TestCase diff --git a/test/active_record/test_helper.rb b/test/active_record/test_helper.rb index af06d12..6079213 100644 --- a/test/active_record/test_helper.rb +++ b/test/active_record/test_helper.rb @@ -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' diff --git a/test/test_helper.rb b/test/test_helper.rb index 3f0b381..e4baadf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'rubygems' require 'bundler/setup' require 'second_level_cache'