Tidy up pending TODOs after discussion with Mr. Gatoz (@wycats).
This commit is contained in:
parent
8dbee3aba6
commit
13df194c00
@ -35,7 +35,6 @@ module ActionView
|
||||
autoload :Helpers
|
||||
autoload :LookupContext
|
||||
autoload :PathSet
|
||||
autoload :Rendering
|
||||
autoload :Template
|
||||
autoload :TestCase
|
||||
|
||||
|
@ -131,7 +131,7 @@ module ActionView #:nodoc:
|
||||
#
|
||||
# More builder documentation can be found at http://builder.rubyforge.org.
|
||||
class Base
|
||||
include Helpers, Rendering, ::ERB::Util, Context
|
||||
include Helpers, ::ERB::Util, Context
|
||||
|
||||
# Specify the proc used to decorate input tags that refer to attributes with errors.
|
||||
cattr_accessor :field_error_proc
|
||||
@ -186,11 +186,6 @@ def initialize(context = nil, assigns_for_first_render = {}, controller = nil, f
|
||||
assign(assigns_for_first_render)
|
||||
self.helpers = Module.new unless self.class.helpers
|
||||
|
||||
# Context vars initialization
|
||||
@view_flow = OutputFlow.new
|
||||
@output_buffer = nil
|
||||
@virtual_path = nil
|
||||
|
||||
@_config = {}
|
||||
if @_controller = controller
|
||||
@_request = controller.request if controller.respond_to?(:request)
|
||||
@ -207,6 +202,8 @@ def initialize(context = nil, assigns_for_first_render = {}, controller = nil, f
|
||||
lookup_context.formats = formats if formats
|
||||
@view_renderer = ActionView::Renderer.new(lookup_context, controller)
|
||||
end
|
||||
|
||||
_prepare_context
|
||||
end
|
||||
|
||||
# TODO Is this needed anywhere? Maybe deprecate it?
|
||||
|
@ -10,71 +10,27 @@ module CompiledTemplates #:nodoc:
|
||||
#
|
||||
# In order to work with ActionController, a Context must just include this module.
|
||||
# The initialization of the variables used by the context (@output_buffer, @view_flow,
|
||||
# and @virtual_path) is responsibility of the object that includes this module.
|
||||
# and @virtual_path) is responsibility of the object that includes this module
|
||||
# (although you can call _prepare_context defined below).
|
||||
module Context
|
||||
include CompiledTemplates
|
||||
attr_accessor :output_buffer, :view_flow
|
||||
|
||||
# TODO Provide an easy method that initializes all variables?
|
||||
# Prepares the context by setting the appropriate instance variables.
|
||||
# :api: plugin
|
||||
def _prepare_context
|
||||
@view_flow = OutputFlow.new
|
||||
@output_buffer = nil
|
||||
@virtual_path = nil
|
||||
end
|
||||
|
||||
# Returns the contents that are yielded to a layout, given a name or a block.
|
||||
#
|
||||
# You can think of a layout as a method that is called with a block. If the user calls
|
||||
# <tt>yield :some_name</tt>, the block, by default, returns <tt>content_for(:some_name)</tt>.
|
||||
# If the user calls simply +yield+, the default block returns <tt>content_for(:layout)</tt>.
|
||||
#
|
||||
# The user can override this default by passing a block to the layout:
|
||||
#
|
||||
# # The template
|
||||
# <%= render :layout => "my_layout" do %>
|
||||
# Content
|
||||
# <% end %>
|
||||
#
|
||||
# # The layout
|
||||
# <html>
|
||||
# <%= yield %>
|
||||
# </html>
|
||||
#
|
||||
# In this case, instead of the default block, which would return <tt>content_for(:layout)</tt>,
|
||||
# this method returns the block that was passed in to <tt>render :layout</tt>, and the response
|
||||
# would be
|
||||
#
|
||||
# <html>
|
||||
# Content
|
||||
# </html>
|
||||
#
|
||||
# Finally, the block can take block arguments, which can be passed in by +yield+:
|
||||
#
|
||||
# # The template
|
||||
# <%= render :layout => "my_layout" do |customer| %>
|
||||
# Hello <%= customer.name %>
|
||||
# <% end %>
|
||||
#
|
||||
# # The layout
|
||||
# <html>
|
||||
# <%= yield Struct.new(:name).new("David") %>
|
||||
# </html>
|
||||
#
|
||||
# In this case, the layout would receive the block passed into <tt>render :layout</tt>,
|
||||
# and the struct specified would be passed into the block as an argument. The result
|
||||
# would be
|
||||
#
|
||||
# <html>
|
||||
# Hello David
|
||||
# </html>
|
||||
#
|
||||
def _layout_for(*args, &block)
|
||||
name = args.first
|
||||
|
||||
if name.is_a?(Symbol)
|
||||
view_flow.get(name).html_safe
|
||||
elsif block
|
||||
# TODO Import capture into AV::Context or
|
||||
# leave it as implicit dependency?
|
||||
capture(*args, &block)
|
||||
else
|
||||
view_flow.get(:layout).html_safe
|
||||
end
|
||||
# Encapsulates the interaction with the view flow so it
|
||||
# returns the correct buffer on yield. This is usually
|
||||
# overwriten by helpers to add more behavior.
|
||||
# :api: plugin
|
||||
def _layout_for(name=nil)
|
||||
name ||= :layout
|
||||
view_flow.get(name).html_safe
|
||||
end
|
||||
end
|
||||
end
|
@ -19,6 +19,7 @@ module Helpers #:nodoc:
|
||||
autoload :NumberHelper
|
||||
autoload :OutputSafetyHelper
|
||||
autoload :RecordTagHelper
|
||||
autoload :RenderingHelper
|
||||
autoload :SanitizeHelper
|
||||
autoload :SprocketsHelper
|
||||
autoload :TagHelper
|
||||
@ -48,6 +49,7 @@ module Helpers #:nodoc:
|
||||
include NumberHelper
|
||||
include OutputSafetyHelper
|
||||
include RecordTagHelper
|
||||
include RenderingHelper
|
||||
include SanitizeHelper
|
||||
include SprocketsHelper
|
||||
include TagHelper
|
||||
|
92
actionpack/lib/action_view/helpers/rendering_helper.rb
Normal file
92
actionpack/lib/action_view/helpers/rendering_helper.rb
Normal file
@ -0,0 +1,92 @@
|
||||
module ActionView
|
||||
module Helpers
|
||||
# = Action View Rendering
|
||||
#
|
||||
# Implements methods that allow rendering from a view context.
|
||||
# In order to use this module, all you need is to implement
|
||||
# view_renderer that returns an ActionView::Renderer object.
|
||||
module RenderingHelper
|
||||
# Returns the result of a render that's dictated by the options hash. The primary options are:
|
||||
#
|
||||
# * <tt>:partial</tt> - See ActionView::Partials.
|
||||
# * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
|
||||
# * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
|
||||
# * <tt>:text</tt> - Renders the text passed in out.
|
||||
#
|
||||
# If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
|
||||
# as the locals hash.
|
||||
def render(options = {}, locals = {}, &block)
|
||||
case options
|
||||
when Hash
|
||||
if block_given?
|
||||
view_renderer.render_partial(self, options.merge(:partial => options[:layout]), &block)
|
||||
elsif options.key?(:partial)
|
||||
view_renderer.render_partial(self, options)
|
||||
else
|
||||
view_renderer.render_template(self, options)
|
||||
end
|
||||
else
|
||||
view_renderer.render_partial(self, :partial => options, :locals => locals)
|
||||
end
|
||||
end
|
||||
|
||||
# Overwrites _layout_for in the context object so it supports the case a block is
|
||||
# passed to a partial. Returns the contents that are yielded to a layout, given a
|
||||
# name or a block.
|
||||
#
|
||||
# You can think of a layout as a method that is called with a block. If the user calls
|
||||
# <tt>yield :some_name</tt>, the block, by default, returns <tt>content_for(:some_name)</tt>.
|
||||
# If the user calls simply +yield+, the default block returns <tt>content_for(:layout)</tt>.
|
||||
#
|
||||
# The user can override this default by passing a block to the layout:
|
||||
#
|
||||
# # The template
|
||||
# <%= render :layout => "my_layout" do %>
|
||||
# Content
|
||||
# <% end %>
|
||||
#
|
||||
# # The layout
|
||||
# <html>
|
||||
# <%= yield %>
|
||||
# </html>
|
||||
#
|
||||
# In this case, instead of the default block, which would return <tt>content_for(:layout)</tt>,
|
||||
# this method returns the block that was passed in to <tt>render :layout</tt>, and the response
|
||||
# would be
|
||||
#
|
||||
# <html>
|
||||
# Content
|
||||
# </html>
|
||||
#
|
||||
# Finally, the block can take block arguments, which can be passed in by +yield+:
|
||||
#
|
||||
# # The template
|
||||
# <%= render :layout => "my_layout" do |customer| %>
|
||||
# Hello <%= customer.name %>
|
||||
# <% end %>
|
||||
#
|
||||
# # The layout
|
||||
# <html>
|
||||
# <%= yield Struct.new(:name).new("David") %>
|
||||
# </html>
|
||||
#
|
||||
# In this case, the layout would receive the block passed into <tt>render :layout</tt>,
|
||||
# and the struct specified would be passed into the block as an argument. The result
|
||||
# would be
|
||||
#
|
||||
# <html>
|
||||
# Hello David
|
||||
# </html>
|
||||
#
|
||||
def _layout_for(*args, &block)
|
||||
name = args.first
|
||||
|
||||
if block && !name.is_a?(Symbol)
|
||||
capture(*args, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,33 +0,0 @@
|
||||
module ActionView
|
||||
# = Action View Rendering
|
||||
#
|
||||
# Implements methods that allow rendering from a view context.
|
||||
# In order to use this module, all you need is to implement
|
||||
# view_renderer that returns an ActionView::Renderer object.
|
||||
module Rendering
|
||||
# Returns the result of a render that's dictated by the options hash. The primary options are:
|
||||
#
|
||||
# * <tt>:partial</tt> - See ActionView::Partials.
|
||||
# * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
|
||||
# * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
|
||||
# * <tt>:text</tt> - Renders the text passed in out.
|
||||
#
|
||||
# If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
|
||||
# as the locals hash.
|
||||
# def render(options = {}, locals = {}, &block)
|
||||
def render(options = {}, locals = {}, &block)
|
||||
case options
|
||||
when Hash
|
||||
if block_given?
|
||||
view_renderer.render_partial(self, options.merge(:partial => options[:layout]), &block)
|
||||
elsif options.key?(:partial)
|
||||
view_renderer.render_partial(self, options)
|
||||
else
|
||||
view_renderer.render_template(self, options)
|
||||
end
|
||||
else
|
||||
view_renderer.render_partial(self, :partial => options, :locals => locals)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,5 +1,8 @@
|
||||
require 'abstract_unit'
|
||||
|
||||
# This is testing the decoupling of view renderer and view context
|
||||
# by allowing the controller to be used as view context. This is
|
||||
# similar to the way sinatra renders templates.
|
||||
module RenderContext
|
||||
class BasicController < ActionController::Base
|
||||
self.view_paths = [ActionView::FixtureResolver.new(
|
||||
@ -7,15 +10,11 @@ class BasicController < ActionController::Base
|
||||
"layouts/basic.html.erb" => "?<%= yield %>?"
|
||||
)]
|
||||
|
||||
# Include ViewContext
|
||||
# 1) Include ActionView::Context to bring the required dependencies
|
||||
include ActionView::Context
|
||||
|
||||
# And initialize the required variables
|
||||
before_filter do
|
||||
@output_buffer = nil
|
||||
@virtual_path = nil
|
||||
@view_flow = ActionView::OutputFlow.new
|
||||
end
|
||||
# 2) Call _prepare_context that will do the required initialization
|
||||
before_filter :_prepare_context
|
||||
|
||||
def hello_world
|
||||
@value = "Hello"
|
||||
@ -29,6 +28,7 @@ def with_layout
|
||||
|
||||
protected
|
||||
|
||||
# 3) Set view_context to self
|
||||
def view_context
|
||||
self
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user