Permalink
Browse files

there was a reason for those cattr_accessors

  • Loading branch information...
sd committed May 2, 2011
1 parent b3e1392 commit 02874729df9699d445a7bea2a740ae2dcad5e713
Showing with 72 additions and 75 deletions.
  1. +51 −54 lib/embedded_actions/caches_embedded.rb
  2. +21 −21 test/test_caches_embedded.rb
@@ -2,6 +2,13 @@ module ActionController
module CachesEmbedded
def self.included(base) # :nodoc:
base.send :include, InstanceMethods
+ base.send :cattr_accessor, :cached_embedded
+ base.send :cattr_accessor, :cached_embedded_options
+ base.send :cattr_accessor, :cached_embedded_engine_options
+ base.cached_embedded = {}
+ base.cached_embedded_options = {}
+ base.cached_embedded_engine_options = {}
+
base.extend(ClassMethods)
base.class_eval do
@@ -10,43 +17,33 @@ def self.included(base) # :nodoc:
end
module ClassMethods
- def cached_embedded
- @cached_embedded ||= {}
- end
-
- def cached_embedded_options
- @cached_embedded_options ||= {}
- end
-
def caches_embedded(*actions)
return unless perform_caching
options = actions.pop if actions.last.kind_of?(Hash)
-
+
actions.each do |action|
action_key = "#{controller_path}/#{action}".to_sym
self.cached_embedded[action_key] = true
- self.cached_embedded_options[action_key] = options if options
+ if options
+ options = options.dup
+ self.cached_embedded_options[action_key] ||= {}
+ self.cached_embedded_options[action_key][:compress] = options.delete(:compress)
+ self.cached_embedded_options[action_key][:options_for_name] = options.delete(:options_for_name)
+ self.cached_embedded_engine_options[action_key] = options
+ end
end
end
end
module InstanceMethods
- def cached_embedded
- self.class.cached_embedded
- end
-
- def cached_embedded_options
- self.class.cached_embedded_options
- end
-
def cache_embedded?(options)
cache_this_instance = options[:params] && options[:params].delete(:caching) # the rest of the request processing code doesn't have to know about this option
return false unless self.perform_caching
controller_class = embedded_class(options)
while controller_class
- if controller_class.cached_embedded["#{controller_class.controller_path}/#{options[:action]}".to_sym]
+ if self.cached_embedded["#{controller_class.controller_path}/#{options[:action]}".to_sym]
return true unless cache_this_instance == false
end
controller_class = controller_class.superclass
@@ -60,57 +57,57 @@ def expire_embedded(options)
expire_fragment(options)
end
- def embedded_cache_name_for_options(options)
- cache_name_for_options = options.dup
-
- options_for_cache_engine = self.cached_embedded_options["#{embedded_class(options).controller_path}/#{options[:action]}".to_sym]
- options_for_cache_engine ||= self.cached_embedded_options["#{embedded_class(options).superclass.controller_path}/#{options[:action]}".to_sym] if embedded_class(options).superclass.respond_to? :controller_path
+ def embedded_cache_name_for_options(options, options_for_name = nil)
+ if options_for_name.nil?
+ options_for_caching = self.cached_embedded_options["#{embedded_class(options).controller_path}/#{options[:action]}".to_sym]
+ options_for_caching ||= self.cached_embedded_options["#{embedded_class(options).superclass.controller_path}/#{options[:action]}".to_sym] if embedded_class(options).superclass.respond_to? :controller_path
- if options_for_cache_engine and options_for_cache_engine[:options_for_name]
- case options_for_cache_engine[:options_for_name]
- when Hash
- extra_options_for_name = options_for_cache_engine[:options_for_name]
- when Proc
- case options_for_cache_engine[:options_for_name].arity
- when 1
- extra_options_for_name = options_for_cache_engine[:options_for_name].call(self)
- else
- extra_options_for_name = options_for_cache_engine[:options_for_name].call(self, options)
- end
+ options_for_name = options_for_caching && options_for_caching[:options_for_name]
+ end
+
+ case options_for_name
+ when Hash
+ extra_options_for_name = options_for_name
+ when Proc
+ case options_for_name.arity
+ when 1
+ extra_options_for_name = options_for_name.call(self)
+ else
+ extra_options_for_name = options_for_name.call(self, options)
end
- options_for_cache_engine = options_for_cache_engine.dup
- options_for_cache_engine.delete(:options_for_name)
else
extra_options_for_name = nil
end
- cache_name_for_options = cache_name_for_options.merge(extra_options_for_name) if extra_options_for_name
+ options = options.merge(extra_options_for_name) if extra_options_for_name
- cache_name_for_options
+ options
end
def embed_action_as_string_with_caching(options)
options = options.dup
force_refresh = options[:params] && options[:params].delete(:refresh_cache)
+
return embed_action_as_string_without_caching(options) unless self.cache_embedded?(options)
- options_for_cache_engine = self.cached_embedded_options["#{embedded_class(options).controller_path}/#{options[:action]}".to_sym]
- options_for_cache_engine ||= self.cached_embedded_options["#{embedded_class(options).superclass.controller_path}/#{options[:action]}".to_sym] if embedded_class(options).superclass.respond_to? :controller_path
+ options_for_caching = self.cached_embedded_options["#{embedded_class(options).controller_path}/#{options[:action]}".to_sym]
+ options_for_caching ||= self.cached_embedded_options["#{embedded_class(options).superclass.controller_path}/#{options[:action]}".to_sym] if embedded_class(options).superclass.respond_to? :controller_path
- cache_name = embedded_cache_name_for_options(options)
-
- if options_for_cache_engine and options_for_cache_engine[:compress]
- compress_cached = options_for_cache_engine[:compress]
- else
- compress_cached = false
- end
+ options_for_cache_engine = self.cached_embedded_engine_options["#{embedded_class(options).controller_path}/#{options[:action]}".to_sym]
+ options_for_cache_engine ||= self.cached_embedded_engine_options["#{embedded_class(options).superclass.controller_path}/#{options[:action]}".to_sym] if embedded_class(options).superclass.respond_to? :controller_path
+# debugger
+ cache_name = embedded_cache_name_for_options(options, options_for_caching && options_for_caching[:options_for_name])
if force_refresh
cached = nil
else
- cached = send(:read_fragment, cache_name)
- if cached and compress_cached
- cached = Zlib::Inflate.inflate(cached) rescue nil
+ cached = nil
+ if options_for_caching && options_for_caching[:compress]
+ cached = Zlib::Inflate.inflate(send(:read_fragment, cache_name.merge("_embedded_compression" => true))) rescue nil
+ end
+
+ unless cached
+ cached = send(:read_fragment, cache_name)
end
end
@@ -119,14 +116,14 @@ def embed_action_as_string_with_caching(options)
if (cached.exception_rescued rescue false) # rescue NoMethodError
RAILS_DEFAULT_LOGGER.debug "Embedded action was not cached because it resulted in an error"
else
- if compress_cached
- send(:write_fragment, cache_name, Zlib::Deflate.deflate(cached), options_for_cache_engine)
+ if options_for_caching && options_for_caching[:compress]
+ send(:write_fragment, cache_name.merge("_embedded_compression" => true), Zlib::Deflate.deflate(cached), options_for_cache_engine)
else
send(:write_fragment, cache_name, cached, options_for_cache_engine)
end
end
end
-
+
cached
end
end
@@ -83,7 +83,7 @@ def inline_erb_action
render :inline => params[:erb]
end
- # def rescue_action(e) raise e end
+ def rescue_action(e) raise e end
end
class InheritingController < CachesEmbeddedTestController
@@ -124,7 +124,7 @@ def test_embedded_caching
CachesEmbeddedTestController.test_value = 1
get :embedded_actions
assert_equal " regular value is 1\n cached value is 1\n", @response.body
-
+
CachesEmbeddedTestController.test_value = 2
get :embedded_actions
assert_equal " regular value is 2\n cached value is 1\n", @response.body
@@ -142,7 +142,7 @@ def test_should_not_return_from_cache_if_params_are_different
get :cached_action, :id => 3
assert_equal "test (id=3)", @response.body
end
-
+
def test_ensure_caching_only_is_enabled_where_it_should_be
TestNoCachingController.test_value = 1
get :call_uncached_controller
@@ -152,7 +152,7 @@ def test_ensure_caching_only_is_enabled_where_it_should_be
get :call_uncached_controller
assert_equal "This should never cache. value: 2", @response.body
end
-
+
def test_should_cache_properly_with_namespaced_controllers
Admin::NamespacedController.test_value = 1
get :call_namespaced_action
@@ -166,14 +166,14 @@ def test_should_cache_properly_with_namespaced_controllers
get :call_namespaced_action
assert_equal "Namespace cache test. value: 2", @response.body
end
-
+
def test_embedded_caching_overrides
# This page uses explicit overrides to reverse which embedded actions are cached
CachesEmbeddedTestController.test_value = 1
get :embedded_overrides
assert_equal " regular value is 1\n cached value is 1\n", @response.body, 'First call should not have been cached'
-
+
CachesEmbeddedTestController.test_value = 2
get :embedded_overrides
assert_equal " regular value is 1\n cached value is 2\n", @response.body, "Second call should reflect cached value"
@@ -182,21 +182,21 @@ def test_embedded_caching_overrides
get :embedded_overrides
assert_equal " regular value is 2\n cached value is 2\n", @response.body, "Expiration should have forced a refreshed value"
end
-
+
def test_embedded_caching_refresh
# This page uses explicit overrides to force refreshing the cache
CachesEmbeddedTestController.test_value = 1
get :forced_refresh
assert_equal " regular value is 1\n cached value is 1\n", @response.body, "First call should not have been cached"
-
+
CachesEmbeddedTestController.test_value = 2
get :forced_refresh
assert_equal " regular value is 2\n cached value is 1\n", @response.body, "Second call should reflect the cached value"
get :forced_refresh, :refresh => true
assert_equal " regular value is 2\n cached value is 2\n", @response.body, "Call with refresh should reflect the new valu"
-
+
CachesEmbeddedTestController.test_value = 3
get :forced_refresh
assert_equal " regular value is 3\n cached value is 2\n", @response.body, "Another call without refresh should reflect the cached value"
@@ -220,17 +220,17 @@ def test_caches_embedded_across_inheritance_tree
end
def test_caches_embedded_with_custom_options_for_cache_name
- CachesEmbeddedTestController.test_value = 1
- get :embedded_variable_actions
- assert_equal " regular value is 1\n cached value is 1\n", @response.body, 'First call should not have been cached'
-
- CachesEmbeddedTestController.test_value = 2
- get :embedded_variable_actions
- assert_equal " regular value is 2\n cached value is 1\n", @response.body, "Second call should reflect cached value"
-
- CachesEmbeddedTestController.another_value = 2
- get :embedded_variable_actions
- assert_equal " regular value is 2\n cached value is 2\n", @response.body, "Third call should reflect updated value since variable in cache name changed"
- end
+ CachesEmbeddedTestController.test_value = 1
+ get :embedded_variable_actions
+ assert_equal " regular value is 1\n cached value is 1\n", @response.body, 'First call should not have been cached'
+
+ CachesEmbeddedTestController.test_value = 2
+ get :embedded_variable_actions
+ assert_equal " regular value is 2\n cached value is 1\n", @response.body, "Second call should reflect cached value"
+
+ CachesEmbeddedTestController.another_value = 2
+ get :embedded_variable_actions
+ assert_equal " regular value is 2\n cached value is 2\n", @response.body, "Third call should reflect updated value since variable in cache name changed"
+ end
end

0 comments on commit 0287472

Please sign in to comment.