From df85ab41c1ff6992dd462a0e63dac9dcdcee0348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 24 Feb 2010 22:06:24 +0100 Subject: Renamed LocalizedCache to DetailsCache. --- actionpack/lib/abstract_controller.rb | 2 +- .../lib/abstract_controller/details_cache.rb | 48 ++++++++++++++++++ .../lib/abstract_controller/localized_cache.rb | 49 ------------------- actionpack/lib/abstract_controller/rendering.rb | 10 ++-- .../lib/action_controller/metal/rendering.rb | 2 +- actionpack/lib/action_view/base.rb | 2 +- actionpack/test/abstract/details_cache_test.rb | 57 ++++++++++++++++++++++ actionpack/test/abstract/localized_cache_test.rb | 57 ---------------------- 8 files changed, 115 insertions(+), 112 deletions(-) create mode 100644 actionpack/lib/abstract_controller/details_cache.rb delete mode 100644 actionpack/lib/abstract_controller/localized_cache.rb create mode 100644 actionpack/test/abstract/details_cache_test.rb delete mode 100644 actionpack/test/abstract/localized_cache_test.rb (limited to 'actionpack') diff --git a/actionpack/lib/abstract_controller.rb b/actionpack/lib/abstract_controller.rb index 1e15ab090c..1944e42ef1 100644 --- a/actionpack/lib/abstract_controller.rb +++ b/actionpack/lib/abstract_controller.rb @@ -13,9 +13,9 @@ module AbstractController autoload :Callbacks autoload :Collector autoload :Compatibility + autoload :DetailsCache autoload :Helpers autoload :Layouts - autoload :LocalizedCache autoload :Logger autoload :Rendering autoload :Translation diff --git a/actionpack/lib/abstract_controller/details_cache.rb b/actionpack/lib/abstract_controller/details_cache.rb new file mode 100644 index 0000000000..5b87b41e7d --- /dev/null +++ b/actionpack/lib/abstract_controller/details_cache.rb @@ -0,0 +1,48 @@ +module AbstractController + class HashKey + @hash_keys = Hash.new {|h,k| h[k] = {} } + + def self.get(klass, details) + @hash_keys[klass][details] ||= new(klass, details) + end + + attr_reader :hash + alias_method :eql?, :equal? + + def initialize(klass, details) + @details, @hash = details, details.hash + end + + def inspect + "#" + end + end + + module DetailsCache + extend ActiveSupport::Concern + + module ClassMethods + def clear_template_caches! + ActionView::Partials::PartialRenderer::TEMPLATES.clear + template_cache.clear + super + end + + def template_cache + @template_cache ||= Hash.new {|h,k| h[k] = {} } + end + end + + def render_to_body(*args) + Thread.current[:format_locale_key] = HashKey.get(self.class, _details_defaults) + super + end + + private + + def with_template_cache(name, details) + self.class.template_cache[HashKey.get(self.class, details)][name] ||= super + end + + end +end diff --git a/actionpack/lib/abstract_controller/localized_cache.rb b/actionpack/lib/abstract_controller/localized_cache.rb deleted file mode 100644 index 5e3efa002c..0000000000 --- a/actionpack/lib/abstract_controller/localized_cache.rb +++ /dev/null @@ -1,49 +0,0 @@ -module AbstractController - class HashKey - @hash_keys = Hash.new {|h,k| h[k] = Hash.new {|sh,sk| sh[sk] = {} } } - - def self.get(klass, formats, locale) - @hash_keys[klass][formats][locale] ||= new(klass, formats, locale) - end - - attr_accessor :hash - def initialize(klass, formats, locale) - @formats, @locale = formats, locale - @hash = [formats, locale].hash - end - - alias_method :eql?, :equal? - - def inspect - "#" - end - end - - module LocalizedCache - extend ActiveSupport::Concern - - module ClassMethods - def clear_template_caches! - ActionView::Partials::PartialRenderer::TEMPLATES.clear - template_cache.clear - super - end - - def template_cache - @template_cache ||= Hash.new {|h,k| h[k] = {} } - end - end - - def render(*args) - Thread.current[:format_locale_key] = HashKey.get(self.class, formats, I18n.locale) - super - end - - private - - def with_template_cache(name) - self.class.template_cache[Thread.current[:format_locale_key]][name] ||= super - end - - end -end diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index f5c20e8013..14f51ae1bf 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -165,13 +165,17 @@ module AbstractController details = _normalize_details(options) - options[:_template] ||= with_template_cache(name) do + options[:_template] ||= with_template_cache(name, details) do find_template(name, details, options) end end + def _details_defaults + { :formats => formats, :locale => [I18n.locale] } + end + def _normalize_details(options) - details = { :formats => formats } + details = _details_defaults details[:formats] = Array(options[:format]) if options[:format] details[:locale] = Array(options[:locale]) if options[:locale] details @@ -185,7 +189,7 @@ module AbstractController view_paths.exists?(name, details, options[:_prefix], options[:_partial]) end - def with_template_cache(name) + def with_template_cache(name, details) yield end diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index b90c054e98..a026289ee5 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -4,7 +4,7 @@ module ActionController include ActionController::RackDelegation include AbstractController::Rendering - include AbstractController::LocalizedCache + include AbstractController::DetailsCache def process_action(*) self.formats = request.formats.map {|x| x.to_sym } diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 44e5870131..a61017ae11 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -196,7 +196,7 @@ module ActionView #:nodoc: # This is expensive, but we need to reset this when the format is updated, # which currently only happens Thread.current[:format_locale_key] = - AbstractController::HashKey.get(self.class, formats, I18n.locale) + AbstractController::HashKey.get(self.class, :formats => formats, :locale => [I18n.locale]) end end diff --git a/actionpack/test/abstract/details_cache_test.rb b/actionpack/test/abstract/details_cache_test.rb new file mode 100644 index 0000000000..ee746c1bb0 --- /dev/null +++ b/actionpack/test/abstract/details_cache_test.rb @@ -0,0 +1,57 @@ +require 'abstract_unit' + +module AbstractController + module Testing + + class CachedController < AbstractController::Base + include AbstractController::Rendering + include AbstractController::DetailsCache + + self.view_paths = [ActionView::FixtureResolver.new( + "default.erb" => "With Default", + "template.erb" => "With Template", + "some/file.erb" => "With File", + "template_name.erb" => "With Template Name" + )] + end + + class TestLocalizedCache < ActiveSupport::TestCase + + def setup + @controller = CachedController.new + CachedController.clear_template_caches! + end + + def test_templates_are_cached + @controller.render :template => "default.erb" + assert_equal "With Default", @controller.response_body + + cached = @controller.class.template_cache + assert_equal 1, cached.size + assert_kind_of ActionView::Template, cached.values.first["default.erb"] + end + + def test_cache_is_used + CachedController.new.render :template => "default.erb" + + @controller.expects(:find_template).never + @controller.render :template => "default.erb" + + assert_equal 1, @controller.class.template_cache.size + end + + def test_cache_changes_with_locale + CachedController.new.render :template => "default.erb" + + I18n.locale = :es + @controller.render :template => "default.erb" + + assert_equal 2, @controller.class.template_cache.size + ensure + I18n.locale = :en + end + + end + + end +end diff --git a/actionpack/test/abstract/localized_cache_test.rb b/actionpack/test/abstract/localized_cache_test.rb deleted file mode 100644 index 8b0b0fff03..0000000000 --- a/actionpack/test/abstract/localized_cache_test.rb +++ /dev/null @@ -1,57 +0,0 @@ -require 'abstract_unit' - -module AbstractController - module Testing - - class CachedController < AbstractController::Base - include AbstractController::Rendering - include AbstractController::LocalizedCache - - self.view_paths = [ActionView::FixtureResolver.new( - "default.erb" => "With Default", - "template.erb" => "With Template", - "some/file.erb" => "With File", - "template_name.erb" => "With Template Name" - )] - end - - class TestLocalizedCache < ActiveSupport::TestCase - - def setup - @controller = CachedController.new - CachedController.clear_template_caches! - end - - def test_templates_are_cached - @controller.render :template => "default.erb" - assert_equal "With Default", @controller.response_body - - cached = @controller.class.template_cache - assert_equal 1, cached.size - assert_kind_of ActionView::Template, cached.values.first["default.erb"] - end - - def test_cache_is_used - CachedController.new.render :template => "default.erb" - - @controller.expects(:find_template).never - @controller.render :template => "default.erb" - - assert_equal 1, @controller.class.template_cache.size - end - - def test_cache_changes_with_locale - CachedController.new.render :template => "default.erb" - - I18n.locale = :es - @controller.render :template => "default.erb" - - assert_equal 2, @controller.class.template_cache.size - ensure - I18n.locale = :en - end - - end - - end -end -- cgit v1.2.3