diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2019-01-23 14:19:50 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2019-02-06 16:52:15 -0800 |
commit | f9bea6304dfba902b1937b3bc29b1ebc2f67e55b (patch) | |
tree | 4859079e3b5c7055fb3b0b8b21bea08cd60de3f6 /actionview | |
parent | 9483cdee0a3ed9c686e338f079b0e369597b1211 (diff) | |
download | rails-f9bea6304dfba902b1937b3bc29b1ebc2f67e55b.tar.gz rails-f9bea6304dfba902b1937b3bc29b1ebc2f67e55b.tar.bz2 rails-f9bea6304dfba902b1937b3bc29b1ebc2f67e55b.zip |
Move templates to an anonymous subclass of AV::Base
Now we can throw away the subclass and the generated methods will get
GC'd too
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/lib/action_view.rb | 1 | ||||
-rw-r--r-- | actionview/lib/action_view/base.rb | 16 | ||||
-rw-r--r-- | actionview/lib/action_view/lookup_context.rb | 5 | ||||
-rw-r--r-- | actionview/lib/action_view/rendering.rb | 21 | ||||
-rw-r--r-- | actionview/test/abstract_unit.rb | 6 | ||||
-rw-r--r-- | actionview/test/activerecord/multifetch_cache_test.rb | 4 | ||||
-rw-r--r-- | actionview/test/template/compiled_templates_test.rb | 17 | ||||
-rw-r--r-- | actionview/test/template/log_subscriber_test.rb | 5 | ||||
-rw-r--r-- | actionview/test/template/render_test.rb | 21 | ||||
-rw-r--r-- | actionview/test/template/streaming_render_test.rb | 5 | ||||
-rw-r--r-- | actionview/test/template/template_test.rb | 4 | ||||
-rw-r--r-- | actionview/test/template/translation_helper_test.rb | 5 |
12 files changed, 75 insertions, 35 deletions
diff --git a/actionview/lib/action_view.rb b/actionview/lib/action_view.rb index 6ff2d70e35..8cb4648a67 100644 --- a/actionview/lib/action_view.rb +++ b/actionview/lib/action_view.rb @@ -35,7 +35,6 @@ module ActionView eager_autoload do autoload :Base autoload :Context - autoload :CompiledTemplates, "action_view/context" autoload :Digestor autoload :Helpers autoload :LookupContext diff --git a/actionview/lib/action_view/base.rb b/actionview/lib/action_view/base.rb index b143e13c96..1e58004fcf 100644 --- a/actionview/lib/action_view/base.rb +++ b/actionview/lib/action_view/base.rb @@ -11,10 +11,6 @@ require "action_view/template" require "action_view/lookup_context" module ActionView #:nodoc: - module CompiledTemplates #:nodoc: - # holds compiled template code - end - # = Action View Base # # Action View templates can be written in several ways. @@ -146,8 +142,6 @@ module ActionView #:nodoc: class Base include Helpers, ::ERB::Util, Context - include CompiledTemplates - # Specify the proc used to decorate input tags that refer to attributes with errors. cattr_accessor :field_error_proc, default: Proc.new { |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe } @@ -186,6 +180,14 @@ module ActionView #:nodoc: def xss_safe? #:nodoc: true end + + def with_empty_template_cache # :nodoc: + template_container = Module.new + Class.new(self) { + include template_container + define_method(:compiled_method_container) { template_container } + } + end end attr_reader :view_renderer @@ -260,7 +262,7 @@ module ActionView #:nodoc: end def compiled_method_container - CompiledTemplates + raise NotImplementedError end ActiveSupport.run_load_hooks(:action_view, self) diff --git a/actionview/lib/action_view/lookup_context.rb b/actionview/lib/action_view/lookup_context.rb index c2f6439e26..c3bb0a49fc 100644 --- a/actionview/lib/action_view/lookup_context.rb +++ b/actionview/lib/action_view/lookup_context.rb @@ -68,12 +68,17 @@ module ActionView end def self.clear + @view_context_class = nil @details_keys.clear end def self.digest_caches @details_keys.values end + + def self.view_context_class(klass) + @view_context_class ||= klass.with_empty_template_cache + end end # Add caching behavior on top of Details. diff --git a/actionview/lib/action_view/rendering.rb b/actionview/lib/action_view/rendering.rb index 205665a8c6..01caa82ec6 100644 --- a/actionview/lib/action_view/rendering.rb +++ b/actionview/lib/action_view/rendering.rb @@ -36,21 +36,12 @@ module ActionView module ClassMethods def view_context_class - @view_context_class ||= begin - supports_path = supports_path? - routes = respond_to?(:_routes) && _routes - helpers = respond_to?(:_helpers) && _helpers - - Class.new(ActionView::Base) do - if routes - include routes.url_helpers(supports_path) - include routes.mounted_helpers - end - - if helpers - include helpers - end - end + klass = ActionView::LookupContext::DetailsKey.view_context_class(ActionView::Base) + + @view_context_class ||= build_view_context_class(klass, supports_path?, _routes, _helpers) + + if klass.changed?(@view_context_class) + @view_context_class = build_view_context_class(klass, supports_path?, _routes, _helpers) end end end diff --git a/actionview/test/abstract_unit.rb b/actionview/test/abstract_unit.rb index acc2ed029b..b649b3c9dd 100644 --- a/actionview/test/abstract_unit.rb +++ b/actionview/test/abstract_unit.rb @@ -48,7 +48,8 @@ module RenderERBUtils @view ||= begin path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH) view_paths = ActionView::PathSet.new([path]) - ActionView::Base.with_view_paths(view_paths) + view = ActionView::Base.with_empty_template_cache + view.with_view_paths(view_paths) end end @@ -61,7 +62,8 @@ module RenderERBUtils ActionView::Template.handler_for_extension(:erb), {}) - template.render(ActionView::Base.empty, {}).strip + view = ActionView::Base.with_empty_template_cache + template.render(view.empty, {}).strip end end diff --git a/actionview/test/activerecord/multifetch_cache_test.rb b/actionview/test/activerecord/multifetch_cache_test.rb index 229b4e56d0..f56168bda5 100644 --- a/actionview/test/activerecord/multifetch_cache_test.rb +++ b/actionview/test/activerecord/multifetch_cache_test.rb @@ -10,8 +10,10 @@ class MultifetchCacheTest < ActiveRecordTestCase def setup view_paths = ActionController::Base.view_paths + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) - @view = Class.new(ActionView::Base) do + @view = Class.new(ActionView::Base.with_empty_template_cache) do def view_cache_dependencies [] end diff --git a/actionview/test/template/compiled_templates_test.rb b/actionview/test/template/compiled_templates_test.rb index ded4786e62..3d4561b55f 100644 --- a/actionview/test/template/compiled_templates_test.rb +++ b/actionview/test/template/compiled_templates_test.rb @@ -3,7 +3,18 @@ require "abstract_unit" class CompiledTemplatesTest < ActiveSupport::TestCase - teardown do + attr_reader :view_class + + def setup + super + view_paths = ActionController::Base.view_paths + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) + @view_class = ActionView::Base.with_empty_template_cache + end + + def teardown + super ActionView::LookupContext::DetailsKey.clear end @@ -72,13 +83,13 @@ class CompiledTemplatesTest < ActiveSupport::TestCase def render_with_cache(*args) view_paths = ActionController::Base.view_paths - ActionView::Base.with_view_paths(view_paths, {}).render(*args) + view_class.with_view_paths(view_paths, {}).render(*args) end def render_without_cache(*args) path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH) view_paths = ActionView::PathSet.new([path]) - ActionView::Base.with_view_paths(view_paths, {}).render(*args) + view_class.with_view_paths(view_paths, {}).render(*args) end def modify_template(template, content) diff --git a/actionview/test/template/log_subscriber_test.rb b/actionview/test/template/log_subscriber_test.rb index 9fcf80bb24..7f98207e3d 100644 --- a/actionview/test/template/log_subscriber_test.rb +++ b/actionview/test/template/log_subscriber_test.rb @@ -12,9 +12,12 @@ class AVLogSubscriberTest < ActiveSupport::TestCase super view_paths = ActionController::Base.view_paths + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) + lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"]) renderer = ActionView::Renderer.new(lookup_context) - @view = ActionView::Base.new(renderer, {}) + @view = ActionView::Base.with_empty_template_cache.new(renderer, {}) ActionView::LogSubscriber.attach_to :action_view diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb index f89d087c34..372b85a4a5 100644 --- a/actionview/test/template/render_test.rb +++ b/actionview/test/template/render_test.rb @@ -9,7 +9,8 @@ end module RenderTestCases def setup_view(paths) @assigns = { secret: "in the sauce" } - @view = Class.new(ActionView::Base) do + + @view = Class.new(ActionView::Base.with_empty_template_cache) do def view_cache_dependencies; []; end def combined_fragment_cache_key(key) @@ -17,7 +18,16 @@ module RenderTestCases end end.with_view_paths(paths, @assigns) - @controller_view = TestController.new.view_context + controller = TestController.new + view = @view + + @controller_view = Class.new(controller.view_context_class) do + include view.compiled_method_container + + define_method(:compiled_method_container) do + view.compiled_method_container + end + end.new(controller.view_renderer, controller.view_assigns, controller) # Reload and register danish language for testing I18n.backend.store_translations "da", {} @@ -629,6 +639,8 @@ class CachedViewRenderTest < ActiveSupport::TestCase # Ensure view path cache is primed def setup view_paths = ActionController::Base.view_paths + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) assert_equal ActionView::OptimizedFileSystemResolver, view_paths.first.class setup_view(view_paths) end @@ -645,6 +657,9 @@ class LazyViewRenderTest < ActiveSupport::TestCase # Test the same thing as above, but make sure the view path # is not eager loaded def setup + view_paths = ActionController::Base.view_paths + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH) view_paths = ActionView::PathSet.new([path]) assert_equal ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH), view_paths.first @@ -704,6 +719,8 @@ class CachedCollectionViewRenderTest < ActiveSupport::TestCase setup do view_paths = ActionController::Base.view_paths assert_equal ActionView::OptimizedFileSystemResolver, view_paths.first.class + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) ActionView::PartialRenderer.collection_cache = ActiveSupport::Cache::MemoryStore.new diff --git a/actionview/test/template/streaming_render_test.rb b/actionview/test/template/streaming_render_test.rb index dda2095013..4567ee31b4 100644 --- a/actionview/test/template/streaming_render_test.rb +++ b/actionview/test/template/streaming_render_test.rb @@ -8,8 +8,11 @@ end class SetupFiberedBase < ActiveSupport::TestCase def setup view_paths = ActionController::Base.view_paths + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) + @assigns = { secret: "in the sauce", name: nil } - @view = ActionView::Base.with_view_paths(view_paths, @assigns) + @view = ActionView::Base.with_empty_template_cache.with_view_paths(view_paths, @assigns) @controller_view = TestController.new.view_context end diff --git a/actionview/test/template/template_test.rb b/actionview/test/template/template_test.rb index 8257d97b7c..8af6739e45 100644 --- a/actionview/test/template/template_test.rb +++ b/actionview/test/template/template_test.rb @@ -20,6 +20,7 @@ class TestERBTemplate < ActiveSupport::TestCase class Context < ActionView::Base def initialize + super @output_buffer = "original" @virtual_path = nil end @@ -63,7 +64,8 @@ class TestERBTemplate < ActiveSupport::TestCase end def setup - @context = Context.new + @context = Context.with_empty_template_cache.new + super end def test_basic_template diff --git a/actionview/test/template/translation_helper_test.rb b/actionview/test/template/translation_helper_test.rb index d04e68e182..7094d4c7fc 100644 --- a/actionview/test/template/translation_helper_test.rb +++ b/actionview/test/template/translation_helper_test.rb @@ -36,7 +36,10 @@ class TranslationHelperTest < ActiveSupport::TestCase } } ) - @view = ::ActionView::Base.with_view_paths(ActionController::Base.view_paths, {}) + view_paths = ActionController::Base.view_paths + view_paths.each(&:clear_cache) + ActionView::LookupContext.fallbacks.each(&:clear_cache) + @view = ::ActionView::Base.with_empty_template_cache.with_view_paths(view_paths, {}) end teardown do |