Rendering xml shouldnt happen inside any layout. Added class proxying to RJS,...

Rendering xml shouldnt happen inside any layout. Added class proxying to RJS, so you can call page.field.clear("my_field") to generate Field.clear("my_field");. Added :content_type option to render, so you can change the content type on the fly. Do type/subtype reordering of Accept header preferences for xml types (aka make Firefox work with respond_to). CHANGED DEFAULT: The default content type for .rxml is now application/xml instead of type/xml, see http://www.xml.com/pub/a/2004/07/21/dive.html for reason

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3852 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 c5d5c4f7
*SVN* *SVN*
* Added :content_type option to render, so you can change the content type on the fly [DHH]. Example: render :action => "atom.rxml", :content_type => "application/atom+xml"
* CHANGED DEFAULT: The default content type for .rxml is now application/xml instead of type/xml, see http://www.xml.com/pub/a/2004/07/21/dive.html for reason [DHH]
* Added option to render action/template/file of a specific extension (and here by template type). This means you can have multiple templates with the same name but a different extension [DHH]. Example: * Added option to render action/template/file of a specific extension (and here by template type). This means you can have multiple templates with the same name but a different extension [DHH]. Example:
class WeblogController < ActionController::Base class WeblogController < ActionController::Base
......
...@@ -634,6 +634,10 @@ def render(options = nil, deprecated_status = nil, &block) #:doc: ...@@ -634,6 +634,10 @@ def render(options = nil, deprecated_status = nil, &block) #:doc:
end end
end end
if content_type = options[:content_type]
headers["Content-Type"] = content_type
end
if text = options[:text] if text = options[:text]
render_text(text, options[:status]) render_text(text, options[:status])
......
...@@ -245,7 +245,7 @@ def apply_layout?(template_with_options, options) ...@@ -245,7 +245,7 @@ def apply_layout?(template_with_options, options)
def candidate_for_layout?(options) def candidate_for_layout?(options)
(options.has_key?(:layout) && options[:layout] != false) || (options.has_key?(:layout) && options[:layout] != false) ||
options.values_at(:text, :file, :inline, :partial, :nothing).compact.empty? && options.values_at(:text, :xml, :file, :inline, :partial, :nothing).compact.empty? &&
!template_exempt_from_layout?(default_template_name(options[:action] || options[:template])) !template_exempt_from_layout?(default_template_name(options[:action] || options[:template]))
end end
......
module Mime module Mime
class Type class Type
def self.lookup(string) class << self
LOOKUP[string] def lookup(string)
end LOOKUP[string]
end
def self.parse(accept_header) def parse(accept_header)
accept_header.split(",").collect! do |mime_type| mime_types = accept_header.split(",").collect! do |mime_type|
Mime::Type.lookup(mime_type.split(";").first.strip) mime_type.split(";").first.strip
end
reorder_xml_types!(mime_types)
mime_types.collect! { |mime_type| Mime::Type.lookup(mime_type) }
end end
private
def reorder_xml_types!(mime_types)
mime_types.delete("text/xml") if mime_types.include?("application/xml")
if index_for_generic_xml = mime_types.index("application/xml")
specific_xml_types = mime_types[index_for_generic_xml..-1].grep(/application\/[a-z]*\+xml/)
for specific_xml_type in specific_xml_types.reverse
mime_types.insert(index_for_generic_xml, mime_types.delete(specific_xml_type))
end
end
end
end end
def initialize(string, symbol = nil, synonyms = []) def initialize(string, symbol = nil, synonyms = [])
...@@ -19,6 +37,10 @@ def to_s ...@@ -19,6 +37,10 @@ def to_s
@string @string
end end
def to_str
to_s
end
def to_sym def to_sym
@symbol || to_sym @symbol || to_sym
end end
...@@ -39,7 +61,7 @@ def ==(mime_type) ...@@ -39,7 +61,7 @@ def ==(mime_type)
ALL = Type.new "*/*", :all ALL = Type.new "*/*", :all
HTML = Type.new "text/html", :html, %w( application/xhtml+xml ) HTML = Type.new "text/html", :html, %w( application/xhtml+xml )
JS = Type.new "text/javascript", :js, %w( application/javascript application/x-javascript ) JS = Type.new "text/javascript", :js, %w( application/javascript application/x-javascript )
XML = Type.new "application/xml", :xml, %w( application/x-xml ) XML = Type.new "application/xml", :xml, %w( text/xml application/x-xml )
RSS = Type.new "application/rss+xml", :rss RSS = Type.new "application/rss+xml", :rss
ATOM = Type.new "application/atom+xml", :atom ATOM = Type.new "application/atom+xml", :atom
YAML = Type.new "application/x-yaml", :yaml YAML = Type.new "application/x-yaml", :yaml
......
...@@ -406,7 +406,7 @@ def create_template_source(extension, template, render_symbol, locals) ...@@ -406,7 +406,7 @@ def create_template_source(extension, template, render_symbol, locals)
body = case extension.to_sym body = case extension.to_sym
when :rxml when :rxml
"xml = Builder::XmlMarkup.new(:indent => 2)\n" + "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
"@controller.headers['Content-Type'] ||= 'text/xml'\n" + "@controller.headers['Content-Type'] ||= 'application/xml'\n" +
template template
when :rjs when :rjs
"@controller.headers['Content-Type'] ||= 'text/javascript'\n" + "@controller.headers['Content-Type'] ||= 'text/javascript'\n" +
......
...@@ -618,29 +618,33 @@ def drop_receiving(id, options = {}) ...@@ -618,29 +618,33 @@ def drop_receiving(id, options = {})
end end
private private
def page def page
self self
end end
def record(line) def record(line)
returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do
self << line self << line
end
end end
end
def render(*options_for_render) def render(*options_for_render)
Hash === options_for_render.first ? Hash === options_for_render.first ?
@context.render(*options_for_render) : @context.render(*options_for_render) :
options_for_render.first.to_s options_for_render.first.to_s
end end
def javascript_object_for(object) def javascript_object_for(object)
object.respond_to?(:to_json) ? object.to_json : object.inspect object.respond_to?(:to_json) ? object.to_json : object.inspect
end end
def arguments_for_call(arguments) def arguments_for_call(arguments)
arguments.map { |argument| javascript_object_for(argument) }.join ', ' arguments.map { |argument| javascript_object_for(argument) }.join ', '
end end
def method_missing(method, *arguments)
JavaScriptProxy.new(self, method.to_s.camelize)
end
end end
end end
...@@ -718,7 +722,7 @@ def method_missing(method, *arguments) ...@@ -718,7 +722,7 @@ def method_missing(method, *arguments)
if method.to_s =~ /(.*)=$/ if method.to_s =~ /(.*)=$/
assign($1, arguments.first) assign($1, arguments.first)
else else
call("#{method.to_s.first}#{method.to_s.classify[1..-1]}", *arguments) call("#{method.to_s.first}#{method.to_s.camelize[1..-1]}", *arguments)
end end
end end
......
...@@ -479,10 +479,12 @@ def setup ...@@ -479,10 +479,12 @@ def setup
@controller = ActionPackAssertionsController.new @controller = ActionPackAssertionsController.new
@request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
end end
def test_rendering_xml_sets_content_type def test_rendering_xml_sets_content_type
process :hello_xml_world process :hello_xml_world
assert_equal('text/xml', @controller.headers['Content-Type']) assert_equal('application/xml', @controller.headers['Content-Type'])
end end
def test_rendering_xml_respects_content_type def test_rendering_xml_respects_content_type
@response.headers['Content-Type'] = 'application/pdf' @response.headers['Content-Type'] = 'application/pdf'
process :hello_xml_world process :hello_xml_world
......
...@@ -380,4 +380,9 @@ def test_collection_proxy_with_zip ...@@ -380,4 +380,9 @@ def test_collection_proxy_with_zip
}); });
EOS EOS
end end
def test_class_proxy
@generator.form.focus('my_field')
assert_equal "Form.focus(\"my_field\");", @generator.to_s
end
end end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册