mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Always evaluate views against an ActionView::Base
Methods created by views should always be evaluated against an AV::Base instance. This way we can extract and refactor things in to classes.
This commit is contained in:
parent
ccfa01c36e
commit
c740ebdaf5
5 changed files with 18 additions and 9 deletions
|
@ -210,6 +210,13 @@ module ActionView #:nodoc:
|
|||
_prepare_context
|
||||
end
|
||||
|
||||
def run(method, locals, buffer, &block)
|
||||
_old_output_buffer = @output_buffer
|
||||
send(method, locals, buffer, &block)
|
||||
ensure
|
||||
@output_buffer = _old_output_buffer
|
||||
end
|
||||
|
||||
ActiveSupport.run_load_hooks(:action_view, self)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -158,7 +158,7 @@ module ActionView
|
|||
def render(view, locals, buffer = ActionView::OutputBuffer.new, &block)
|
||||
instrument_render_template do
|
||||
compile!(view)
|
||||
view.send(method_name, locals, buffer, &block)
|
||||
view.run(method_name, locals, buffer, &block)
|
||||
end
|
||||
rescue => e
|
||||
handle_render_error(view, e)
|
||||
|
@ -301,9 +301,9 @@ module ActionView
|
|||
# encoding of the code
|
||||
source = +<<-end_src
|
||||
def #{method_name}(local_assigns, output_buffer)
|
||||
_old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code}
|
||||
_old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};#{locals_code};#{code}
|
||||
ensure
|
||||
@virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer
|
||||
@virtual_path = _old_virtual_path
|
||||
end
|
||||
end_src
|
||||
|
||||
|
|
|
@ -22,10 +22,12 @@ module ActionView
|
|||
end
|
||||
|
||||
def evaluate(action_view_erb_handler_context)
|
||||
pr = eval("proc { #{@src} }", binding, @filename || "(erubi)")
|
||||
# Double assignment to eliminate -w warnings
|
||||
output_buffer = output_buffer = ActionView::OutputBuffer.new
|
||||
action_view_erb_handler_context.instance_eval(&pr)
|
||||
src = @src
|
||||
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)
|
||||
view.run(:_template, {}, ActionView::OutputBuffer.new)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -61,7 +61,7 @@ module RenderERBUtils
|
|||
ActionView::Template::Handlers::ERB,
|
||||
{})
|
||||
|
||||
template.render(self, {}).strip
|
||||
template.render(ActionView::Base.new, {}).strip
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ class TestERBTemplate < ActiveSupport::TestCase
|
|||
attr_accessor :formats
|
||||
end
|
||||
|
||||
class Context
|
||||
class Context < ActionView::Base
|
||||
def initialize
|
||||
@output_buffer = "original"
|
||||
@virtual_path = nil
|
||||
|
|
Loading…
Reference in a new issue