mirror of https://github.com/rails/rails
Improve AbstractController layouts coverage.
This commit is contained in:
parent
2d514e5352
commit
0cf16ddb88
|
@ -8,9 +8,7 @@ module AbstractController
|
|||
|
||||
included do
|
||||
attr_internal :formats
|
||||
|
||||
extlib_inheritable_accessor :_view_paths
|
||||
|
||||
self._view_paths ||= ActionView::PathSet.new
|
||||
end
|
||||
|
||||
|
@ -99,6 +97,7 @@ module AbstractController
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
# Take in a set of options and determine the template to render
|
||||
#
|
||||
# ==== Options
|
||||
|
@ -110,7 +109,7 @@ module AbstractController
|
|||
# _partial<TrueClass, FalseClass>:: Whether or not the file to look up is a partial
|
||||
def _determine_template(options)
|
||||
if options.key?(:text)
|
||||
options[:_template] = ActionView::TextTemplate.new(options[:text], formats.first)
|
||||
options[:_template] = ActionView::TextTemplate.new(options[:text], format_for_text)
|
||||
elsif options.key?(:inline)
|
||||
handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb")
|
||||
template = ActionView::Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {})
|
||||
|
@ -147,6 +146,10 @@ module AbstractController
|
|||
yield
|
||||
end
|
||||
|
||||
def format_for_text
|
||||
Mime[:text]
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def clear_template_caches!
|
||||
end
|
||||
|
|
|
@ -65,6 +65,10 @@ module ActionController
|
|||
controller_path
|
||||
end
|
||||
|
||||
def format_for_text
|
||||
formats.first
|
||||
end
|
||||
|
||||
def with_template_cache(name)
|
||||
self.class.template_cache[Thread.current[:format_locale_key]][name] ||= super
|
||||
end
|
||||
|
|
|
@ -16,11 +16,11 @@ module ActionView #:nodoc:
|
|||
end
|
||||
|
||||
def inspect
|
||||
'inline template'
|
||||
'text template'
|
||||
end
|
||||
|
||||
def render(*args)
|
||||
self
|
||||
to_s
|
||||
end
|
||||
|
||||
def mime_type
|
||||
|
|
|
@ -17,17 +17,6 @@ module AbstractControllerTests
|
|||
"layouts/omg.erb" => "OMGHI2U <%= yield %>",
|
||||
"layouts/with_false_layout.erb" => "False Layout <%= yield %>"
|
||||
)]
|
||||
|
||||
def self.controller_path
|
||||
@controller_path ||= self.name.sub(/Controller$/, '').underscore
|
||||
end
|
||||
|
||||
def controller_path() self.class.controller_path end
|
||||
|
||||
def render_to_body(options)
|
||||
options[:_layout] = _default_layout({})
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
class Blank < Base
|
||||
|
@ -44,6 +33,22 @@ module AbstractControllerTests
|
|||
def index
|
||||
render :_template => ActionView::TextTemplate.new("Hello string!")
|
||||
end
|
||||
|
||||
def overwrite_default
|
||||
render :_template => ActionView::TextTemplate.new("Hello string!"), :layout => :default
|
||||
end
|
||||
|
||||
def overwrite_false
|
||||
render :_template => ActionView::TextTemplate.new("Hello string!"), :layout => false
|
||||
end
|
||||
|
||||
def overwrite_string
|
||||
render :_template => ActionView::TextTemplate.new("Hello string!"), :layout => "omg"
|
||||
end
|
||||
|
||||
def overwrite_skip
|
||||
render :text => "Hello text!"
|
||||
end
|
||||
end
|
||||
|
||||
class WithStringChild < WithString
|
||||
|
@ -153,7 +158,31 @@ module AbstractControllerTests
|
|||
controller.process(:index)
|
||||
assert_equal "With String Hello string!", controller.response_body
|
||||
end
|
||||
|
||||
|
||||
test "when layout is overwriten by :default in render, render default layout" do
|
||||
controller = WithString.new
|
||||
controller.process(:overwrite_default)
|
||||
assert_equal "With String Hello string!", controller.response_body
|
||||
end
|
||||
|
||||
test "when layout is overwriten by string in render, render new layout" do
|
||||
controller = WithString.new
|
||||
controller.process(:overwrite_string)
|
||||
assert_equal "OMGHI2U Hello string!", controller.response_body
|
||||
end
|
||||
|
||||
test "when layout is overwriten by false in render, render no layout" do
|
||||
controller = WithString.new
|
||||
controller.process(:overwrite_false)
|
||||
assert_equal "Hello string!", controller.response_body
|
||||
end
|
||||
|
||||
test "when text is rendered, render no layout" do
|
||||
controller = WithString.new
|
||||
controller.process(:overwrite_skip)
|
||||
assert_equal "Hello text!", controller.response_body
|
||||
end
|
||||
|
||||
test "when layout is specified as a string, but the layout is missing, raise an exception" do
|
||||
assert_raises(ActionView::MissingTemplate) { WithMissingLayout.new.process(:index) }
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue