提交 ff063d70 编写于 作者: T Tobias Lütke

respond_to .html now always renders #{action_name}.rhtml so that registered...

respond_to .html now always renders #{action_name}.rhtml so that registered custom template handlers do not override it in priority. 
Custom mime types require a block and throw proper error now.


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4860 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 3704088e
*SVN* *SVN*
* respond_to .html now always renders #{action_name}.rhtml so that registered custom template handlers do not override it in priority. Custom mime types require a block and throw proper error now. [Tobias Luetke]
* Deprecation: test deprecated instance vars in partials. [Jeremy Kemper] * Deprecation: test deprecated instance vars in partials. [Jeremy Kemper]
* Add UrlWriter to allow writing urls from Mailers and scripts. [Nicholas Seckar] * Add UrlWriter to allow writing urls from Mailers and scripts. [Nicholas Seckar]
......
...@@ -27,6 +27,8 @@ class UnknownAction < ActionControllerError #:nodoc: ...@@ -27,6 +27,8 @@ class UnknownAction < ActionControllerError #:nodoc:
end end
class MissingFile < ActionControllerError #:nodoc: class MissingFile < ActionControllerError #:nodoc:
end end
class RenderError < ActionControllerError #:nodoc:
end
class SessionOverflowError < ActionControllerError #:nodoc: class SessionOverflowError < ActionControllerError #:nodoc:
DEFAULT_MESSAGE = 'Your session data is larger than the data column in which it is to be stored. You must increase the size of your data column if you intend to store large data.' DEFAULT_MESSAGE = 'Your session data is larger than the data column in which it is to be stored. You must increase the size of your data column if you intend to store large data.'
......
...@@ -108,11 +108,9 @@ def respond_to(*types, &block) ...@@ -108,11 +108,9 @@ def respond_to(*types, &block)
end end
class Responder #:nodoc: class Responder #:nodoc:
DEFAULT_BLOCKS = { DEFAULT_BLOCKS = [:html, :js, :xml].inject({}) do |blocks, ext|
:html => 'Proc.new { render }', blocks.update ext => %(Proc.new { render :action => "\#{action_name}.r#{ext}" })
:js => 'Proc.new { render :action => "#{action_name}.rjs" }', end
:xml => 'Proc.new { render :action => "#{action_name}.rxml" }'
}
def initialize(block_binding) def initialize(block_binding)
@block_binding = block_binding @block_binding = block_binding
...@@ -134,7 +132,11 @@ def custom(mime_type, &block) ...@@ -134,7 +132,11 @@ def custom(mime_type, &block)
if block_given? if block_given?
@responses[mime_type] = block @responses[mime_type] = block
else else
@responses[mime_type] = eval(DEFAULT_BLOCKS[mime_type.to_sym], @block_binding) if source = DEFAULT_BLOCKS[mime_type.to_sym]
@responses[mime_type] = eval(source, @block_binding)
else
raise ActionController::RenderError, "Expected a block but none was given for custom mime handler #{mime_type}"
end
end end
end end
......
...@@ -34,7 +34,7 @@ def lookup(string) ...@@ -34,7 +34,7 @@ def lookup(string)
def register(string, symbol, synonyms = []) def register(string, symbol, synonyms = [])
Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, synonyms) Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, synonyms)
SET << Mime.send(:const_get, symbol.to_s.upcase) SET << Mime.send(:const_get, symbol.to_s.upcase)
LOOKUP[string] = EXTENSION_LOOKUP[symbol.to_s] = SET.last LOOKUP[string] = EXTENSION_LOOKUP[symbol.to_s] = SET.last
end end
def parse(accept_header) def parse(accept_header)
......
...@@ -72,6 +72,18 @@ def custom_constant_handling ...@@ -72,6 +72,18 @@ def custom_constant_handling
Mime.send :remove_const, :MOBILE Mime.send :remove_const, :MOBILE
end end
def custom_constant_handling_without_block
Mime::Type.register("text/x-mobile", :mobile)
respond_to do |type|
type.html { render :text => "HTML" }
type.mobile
end
Mime.send :remove_const, :MOBILE
end
def handle_any def handle_any
respond_to do |type| respond_to do |type|
...@@ -271,6 +283,13 @@ def test_custom_constant ...@@ -271,6 +283,13 @@ def test_custom_constant
assert_equal "Mobile", @response.body assert_equal "Mobile", @response.body
end end
def custom_constant_handling_without_block
assert_raised(ActionController::RenderError) do
get :custom_constant_handling, :format => "mobile"
end
end
def test_forced_format def test_forced_format
get :html_xml_or_rss get :html_xml_or_rss
assert_equal "HTML", @response.body assert_equal "HTML", @response.body
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册