aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2019-01-29 15:17:52 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2019-01-29 15:49:40 -0800
commite17fe52e0e0ef0842b6c409e1110a862c4e005bc (patch)
treefcc3617c202d873ffed4d99844fbd7d6f6712cd0 /actionview/lib
parent94d54fa4ab641a0ddeb173409cb41cc5becc02a9 (diff)
downloadrails-e17fe52e0e0ef0842b6c409e1110a862c4e005bc.tar.gz
rails-e17fe52e0e0ef0842b6c409e1110a862c4e005bc.tar.bz2
rails-e17fe52e0e0ef0842b6c409e1110a862c4e005bc.zip
Tighten up the AV::Base constructor
The AV::Base constructor was too complicated, and this commit tightens up the parameters it will take. At runtime, AV::Base is most commonly constructed here: https://github.com/rails/rails/blob/94d54fa4ab641a0ddeb173409cb41cc5becc02a9/actionview/lib/action_view/rendering.rb#L72-L74 This provides an AV::Renderer instance, a hash of assignments, and a controller instance. Since this is the common case for construction, we should remove logic from the constructor that handles other cases. This commit introduces special constructors for those other cases. Interestingly, most code paths that construct AV::Base "strangely" are tests.
Diffstat (limited to 'actionview/lib')
-rw-r--r--actionview/lib/action_view/base.rb51
-rw-r--r--actionview/lib/action_view/template/handlers/erb/erubi.rb2
2 files changed, 43 insertions, 10 deletions
diff --git a/actionview/lib/action_view/base.rb b/actionview/lib/action_view/base.rb
index 6cb342a0a9..fe9f3f9503 100644
--- a/actionview/lib/action_view/base.rb
+++ b/actionview/lib/action_view/base.rb
@@ -3,6 +3,7 @@
require "active_support/core_ext/module/attr_internal"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/ordered_options"
+require "active_support/deprecation"
require "action_view/log_subscriber"
require "action_view/helpers"
require "action_view/context"
@@ -187,7 +188,7 @@ module ActionView #:nodoc:
end
end
- attr_accessor :view_renderer
+ attr_reader :view_renderer
attr_internal :config, :assigns
delegate :lookup_context, to: :view_renderer
@@ -197,17 +198,49 @@ module ActionView #:nodoc:
@_assigns = new_assigns.each { |key, value| instance_variable_set("@#{key}", value) }
end
- def initialize(context = nil, assigns = {}, controller = nil, formats = nil) #:nodoc:
+ # :stopdoc:
+
+ def self.build_renderer(context, controller, formats)
+ lookup_context = context.is_a?(ActionView::LookupContext) ?
+ context : ActionView::LookupContext.new(context)
+ lookup_context.formats = formats if formats
+ lookup_context.prefixes = controller._prefixes if controller
+ ActionView::Renderer.new(lookup_context)
+ end
+
+ def self.empty
+ with_view_paths([])
+ end
+
+ def self.with_view_paths(view_paths, assigns = {}, controller = nil)
+ with_context ActionView::LookupContext.new(view_paths), assigns, controller
+ end
+
+ def self.with_context(context, assigns = {}, controller = nil)
+ new ActionView::Renderer.new(context), assigns, controller
+ end
+
+ NULL = Object.new
+
+ # :startdoc:
+
+ def initialize(renderer, assigns = {}, controller = nil, formats = NULL) #:nodoc:
@_config = ActiveSupport::InheritableOptions.new
- if context.is_a?(ActionView::Renderer)
- @view_renderer = context
+ unless formats == NULL
+ ActiveSupport::Deprecation.warn <<~eowarn
+ Passing formats to ActionView::Base.new is deprecated
+ eowarn
+ end
+
+ if renderer.is_a?(ActionView::Renderer)
+ @view_renderer = renderer
else
- lookup_context = context.is_a?(ActionView::LookupContext) ?
- context : ActionView::LookupContext.new(context)
- lookup_context.formats = formats if formats
- lookup_context.prefixes = controller._prefixes if controller
- @view_renderer = ActionView::Renderer.new(lookup_context)
+ ActiveSupport::Deprecation.warn <<~eowarn
+ ActionView::Base instances should be constructed with a view renderer,
+ assigments, and a controller.
+ eowarn
+ @view_renderer = self.class.build_renderer(renderer, controller, formats)
end
@cache_hit = {}
diff --git a/actionview/lib/action_view/template/handlers/erb/erubi.rb b/actionview/lib/action_view/template/handlers/erb/erubi.rb
index e155bae89d..15ca202024 100644
--- a/actionview/lib/action_view/template/handlers/erb/erubi.rb
+++ b/actionview/lib/action_view/template/handlers/erb/erubi.rb
@@ -26,7 +26,7 @@ module ActionView
view = Class.new(ActionView::Base) {
include action_view_erb_handler_context._routes.url_helpers
class_eval("define_method(:_template) { |local_assigns, output_buffer| #{src} }", @filename || "(erubi)", 0)
- }.new(action_view_erb_handler_context)
+ }.with_context(action_view_erb_handler_context)
view.run(:_template, {}, ActionView::OutputBuffer.new)
end