Create AbstractController::Rendering interface
This interface should be use when implementing renderers.
This commit is contained in:
parent
c90971644a
commit
8e3413d410
@ -13,6 +13,7 @@ module AbstractController
|
|||||||
autoload :DoubleRenderError, "abstract_controller/rendering.rb"
|
autoload :DoubleRenderError, "abstract_controller/rendering.rb"
|
||||||
autoload :Helpers
|
autoload :Helpers
|
||||||
autoload :Logger
|
autoload :Logger
|
||||||
|
autoload :Rendering
|
||||||
autoload :Translation
|
autoload :Translation
|
||||||
autoload :AssetPaths
|
autoload :AssetPaths
|
||||||
autoload :UrlFor
|
autoload :UrlFor
|
||||||
|
@ -6,4 +6,54 @@ def initialize(message = nil)
|
|||||||
super(message || DEFAULT_MESSAGE)
|
super(message || DEFAULT_MESSAGE)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Rendering
|
||||||
|
# Raw rendering of a template to a string.
|
||||||
|
#
|
||||||
|
# It is similar to render, except that it does not
|
||||||
|
# set the response_body and it should be guaranteed
|
||||||
|
# to always return a string.
|
||||||
|
#
|
||||||
|
# If a component extends the semantics of response_body
|
||||||
|
# (as Action Controller extends it to be anything that
|
||||||
|
# responds to the method each), this method needs to be
|
||||||
|
# overridden in order to still return a string.
|
||||||
|
# :api: plugin
|
||||||
|
def render_to_string(*args, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Raw rendering of a template.
|
||||||
|
# :api: plugin
|
||||||
|
def render_to_body(options = {})
|
||||||
|
end
|
||||||
|
|
||||||
|
def render(*args, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
# This method should return a hash with assigns.
|
||||||
|
# You can overwrite this configuration per controller.
|
||||||
|
# :api: public
|
||||||
|
def view_assigns
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
|
# Normalize args by converting render "foo" to render :action => "foo" and
|
||||||
|
# render "foo/bar" to render :file => "foo/bar".
|
||||||
|
# :api: plugin
|
||||||
|
def _normalize_args(action=nil, options={})
|
||||||
|
options
|
||||||
|
end
|
||||||
|
|
||||||
|
# Normalize options.
|
||||||
|
# :api: plugin
|
||||||
|
def _normalize_options(options)
|
||||||
|
options
|
||||||
|
end
|
||||||
|
|
||||||
|
# Process extra options.
|
||||||
|
# :api: plugin
|
||||||
|
def _process_options(options)
|
||||||
|
options
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -161,7 +161,11 @@ module ActionController
|
|||||||
# render action: "overthere" # won't be called if monkeys is nil
|
# render action: "overthere" # won't be called if monkeys is nil
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
class Base < Metal
|
metal = Class.new(Metal) do
|
||||||
|
include AbstractController::Rendering
|
||||||
|
end
|
||||||
|
|
||||||
|
class Base < metal
|
||||||
abstract!
|
abstract!
|
||||||
|
|
||||||
# We document the request and response methods here because albeit they are
|
# We document the request and response methods here because albeit they are
|
||||||
|
@ -29,6 +29,7 @@ class TestBasic < ActiveSupport::TestCase
|
|||||||
# Test Render mixin
|
# Test Render mixin
|
||||||
# ====
|
# ====
|
||||||
class RenderingController < AbstractController::Base
|
class RenderingController < AbstractController::Base
|
||||||
|
include AbstractController::Rendering
|
||||||
include ActionView::Rendering
|
include ActionView::Rendering
|
||||||
|
|
||||||
def _prefixes
|
def _prefixes
|
||||||
|
@ -6,8 +6,9 @@ module AbstractController
|
|||||||
module Testing
|
module Testing
|
||||||
|
|
||||||
class ControllerWithHelpers < AbstractController::Base
|
class ControllerWithHelpers < AbstractController::Base
|
||||||
include ActionView::Rendering
|
|
||||||
include AbstractController::Helpers
|
include AbstractController::Helpers
|
||||||
|
include AbstractController::Rendering
|
||||||
|
include ActionView::Rendering
|
||||||
|
|
||||||
def with_module
|
def with_module
|
||||||
render :inline => "Module <%= included_method %>"
|
render :inline => "Module <%= included_method %>"
|
||||||
|
@ -5,6 +5,7 @@ module Layouts
|
|||||||
|
|
||||||
# Base controller for these tests
|
# Base controller for these tests
|
||||||
class Base < AbstractController::Base
|
class Base < AbstractController::Base
|
||||||
|
include AbstractController::Rendering
|
||||||
include ActionView::Rendering
|
include ActionView::Rendering
|
||||||
include ActionView::Layouts
|
include ActionView::Layouts
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ module AbstractController
|
|||||||
module Testing
|
module Testing
|
||||||
|
|
||||||
class ControllerRenderer < AbstractController::Base
|
class ControllerRenderer < AbstractController::Base
|
||||||
|
include AbstractController::Rendering
|
||||||
include ActionView::Rendering
|
include ActionView::Rendering
|
||||||
|
|
||||||
def _prefixes
|
def _prefixes
|
||||||
|
@ -271,7 +271,6 @@ def assert_header(name, value)
|
|||||||
module ActionController
|
module ActionController
|
||||||
class Base
|
class Base
|
||||||
include ActionController::Testing
|
include ActionController::Testing
|
||||||
include ActionView::Layouts
|
|
||||||
# This stub emulates the Railtie including the URL helpers from a Rails application
|
# This stub emulates the Railtie including the URL helpers from a Rails application
|
||||||
include SharedTestRoutes.url_helpers
|
include SharedTestRoutes.url_helpers
|
||||||
include SharedTestRoutes.mounted_helpers
|
include SharedTestRoutes.mounted_helpers
|
||||||
@ -291,6 +290,7 @@ class TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
class ::ApplicationController < ActionController::Base
|
class ::ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -337,6 +337,7 @@ def test_using_resource_for_put_with_html_rerender_on_failure
|
|||||||
errors = { :name => :invalid }
|
errors = { :name => :invalid }
|
||||||
Customer.any_instance.stubs(:errors).returns(errors)
|
Customer.any_instance.stubs(:errors).returns(errors)
|
||||||
put :using_resource
|
put :using_resource
|
||||||
|
|
||||||
assert_equal "text/html", @response.content_type
|
assert_equal "text/html", @response.content_type
|
||||||
assert_equal 200, @response.status
|
assert_equal 200, @response.status
|
||||||
assert_equal "Edit world!\n", @response.body
|
assert_equal "Edit world!\n", @response.body
|
||||||
|
@ -755,6 +755,8 @@ def determine_layout
|
|||||||
end
|
end
|
||||||
|
|
||||||
class MetalTestController < ActionController::Metal
|
class MetalTestController < ActionController::Metal
|
||||||
|
include AbstractController::Rendering
|
||||||
|
include ActionView::Rendering
|
||||||
include ActionController::Rendering
|
include ActionController::Rendering
|
||||||
|
|
||||||
def accessing_logger_in_template
|
def accessing_logger_in_template
|
||||||
|
@ -83,21 +83,11 @@ def view_renderer
|
|||||||
# Normalize arguments, options and then delegates render_to_body and
|
# Normalize arguments, options and then delegates render_to_body and
|
||||||
# sticks the result in self.response_body.
|
# sticks the result in self.response_body.
|
||||||
def render(*args, &block)
|
def render(*args, &block)
|
||||||
|
super
|
||||||
options = _normalize_render(*args, &block)
|
options = _normalize_render(*args, &block)
|
||||||
self.response_body = render_to_body(options)
|
self.response_body = render_to_body(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Raw rendering of a template to a string.
|
|
||||||
#
|
|
||||||
# It is similar to render, except that it does not
|
|
||||||
# set the response_body and it should be guaranteed
|
|
||||||
# to always return a string.
|
|
||||||
#
|
|
||||||
# If a component extends the semantics of response_body
|
|
||||||
# (as Action Controller extends it to be anything that
|
|
||||||
# responds to the method each), this method needs to be
|
|
||||||
# overridden in order to still return a string.
|
|
||||||
# :api: plugin
|
|
||||||
def render_to_string(*args, &block)
|
def render_to_string(*args, &block)
|
||||||
options = _normalize_render(*args, &block)
|
options = _normalize_render(*args, &block)
|
||||||
render_to_body(options)
|
render_to_body(options)
|
||||||
@ -126,7 +116,7 @@ def _render_template(options) #:nodoc:
|
|||||||
# You can overwrite this configuration per controller.
|
# You can overwrite this configuration per controller.
|
||||||
# :api: public
|
# :api: public
|
||||||
def view_assigns
|
def view_assigns
|
||||||
hash = {}
|
hash = super
|
||||||
variables = instance_variables
|
variables = instance_variables
|
||||||
variables -= protected_instance_variables
|
variables -= protected_instance_variables
|
||||||
variables -= DEFAULT_PROTECTED_INSTANCE_VARIABLES
|
variables -= DEFAULT_PROTECTED_INSTANCE_VARIABLES
|
||||||
@ -148,6 +138,7 @@ def _normalize_render(*args, &block)
|
|||||||
# render "foo/bar" to render :file => "foo/bar".
|
# render "foo/bar" to render :file => "foo/bar".
|
||||||
# :api: plugin
|
# :api: plugin
|
||||||
def _normalize_args(action=nil, options={})
|
def _normalize_args(action=nil, options={})
|
||||||
|
options = super(action, options)
|
||||||
case action
|
case action
|
||||||
when NilClass
|
when NilClass
|
||||||
when Hash
|
when Hash
|
||||||
@ -166,6 +157,7 @@ def _normalize_args(action=nil, options={})
|
|||||||
# Normalize options.
|
# Normalize options.
|
||||||
# :api: plugin
|
# :api: plugin
|
||||||
def _normalize_options(options)
|
def _normalize_options(options)
|
||||||
|
options = super(options)
|
||||||
if options[:partial] == true
|
if options[:partial] == true
|
||||||
options[:partial] = action_name
|
options[:partial] = action_name
|
||||||
end
|
end
|
||||||
@ -177,10 +169,5 @@ def _normalize_options(options)
|
|||||||
options[:template] ||= (options[:action] || action_name).to_s
|
options[:template] ||= (options[:action] || action_name).to_s
|
||||||
options
|
options
|
||||||
end
|
end
|
||||||
|
|
||||||
# Process extra options.
|
|
||||||
# :api: plugin
|
|
||||||
def _process_options(options)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user