fix model cache for rails 3

Change-Id: I642731a7bb26580bf236fd275fd8ce30565ef2eb
Reviewed-on: https://gerrit.instructure.com/29553
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Jon Willesen <jonw@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2014-02-03 16:06:42 -07:00
parent a43c3e39e8
commit 0ddad6ef86
2 changed files with 16 additions and 13 deletions

View File

@ -102,10 +102,7 @@ module ModelCache
return {} if records.empty?
keys[records.first.class.name].inject({}) do |h, k|
h[k] = records.inject({}) do |h2, v|
h2[v.send(k)] = v
h2
end
h[k] = records.index_by(&k)
h
end
end
@ -118,10 +115,12 @@ module ModelCache
orig_method = "super"
alias_method = nil
if (options[:type] == :instance ? klass.instance_methods : klass.methods).map(&:to_s).include?(method.to_s)
orig_method = "#{method}_without_cache(*args)"
alias_method = "alias_method_chain #{method.inspect}, :cache"
method = "#{method}_with_cache"
if CANVAS_RAILS2
if (options[:type] == :instance ? klass.instance_methods : klass.methods).map(&:to_s).include?(method.to_s)
orig_method = "#{method}_without_cache(*args)"
alias_method = "alias_method_chain #{method.inspect}, :cache"
method = "#{method}_with_cache"
end
end
key_value = options[:key_method] || "args.first"

View File

@ -19,7 +19,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
require 'lib/model_cache'
describe ModelCache do
before do
before(:all) do
class TestModelCacheUser < ActiveRecord::Base
self.table_name = :users # reuse exiting tables so AR doesn't asplode
include ModelCache
@ -37,18 +37,22 @@ describe ModelCache do
belongs_to :test_model_cache_user_copy, :class_name => 'TestModelCacheUser', :foreign_key => :user_id
end
end
before do
user_with_pseudonym(:name => 'qwerty')
@user = TestModelCacheUser.where(:id => @user).first
@pseudonym = TestModelCachePseudonym.where(:id => @pseudonym).first
end
after do
after(:all) do
ModelCache.keys.delete('TestModelCacheUser')
ModelCache.keys.delete('TestModelCachePseudonym')
subclasses = ActiveRecord::Base.send(:class_variable_get, :@@subclasses)[ActiveRecord::Base]
subclasses.delete(TestModelCacheUser)
subclasses.delete(TestModelCachePseudonym)
if CANVAS_RAILS2
subclasses = ActiveRecord::Base.send(:class_variable_get, :@@subclasses)[ActiveRecord::Base]
subclasses.delete(TestModelCacheUser)
subclasses.delete(TestModelCachePseudonym)
end
Object.send(:remove_const, :TestModelCacheUser)
Object.send(:remove_const, :TestModelCachePseudonym)
end