Skip to content

Commit

Permalink
Added the :xml render option
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehuda Katz + Carl Lerche committed May 21, 2009
1 parent 2daac47 commit ad1c90d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion actionpack/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Rake::TestTask.new(:test_new_base_on_old_tests) do |t|
t.test_files = %w(
addresses_render base benchmark caching capture content_type dispatcher
flash mime_responds record_identifier redirect
render render_json
render render_json render_xml
send_file request_forgery_protection rescue url_rewriter verification webservice
).map { |name| "test/controller/#{name}_test.rb" }
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/new_base/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Base < Http
include ActionController::UrlFor
include ActionController::Redirector
include ActionController::Renderer
include ActionController::RenderOptions
include ActionController::Renderers::Json
include ActionController::Renderers::Xml
include ActionController::Layouts
include ActionController::ConditionalGet

Expand Down
30 changes: 24 additions & 6 deletions actionpack/lib/action_controller/new_base/render_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ def render_to_body(options)
end
end

module Renderers
module Json
module RenderOption
extend ActiveSupport::DependencyModule

included do
extend ActiveSupport::DependencyModule

depends_on RenderOptions
included do
_renderers << :json

def self.register_renderer(name)
included { _renderers << name }
end
end
end

module Renderers
module Json
include RenderOption
register_renderer :json

def _render_json(json, options)
json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
Expand All @@ -35,5 +43,15 @@ def _render_json(json, options)
self.response_body = json
end
end

module Xml
include RenderOption
register_renderer :xml

def _render_xml(xml, options)
response.content_type ||= Mime::XML
self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml : xml
end
end
end
end
3 changes: 2 additions & 1 deletion actionpack/lib/action_controller/new_base/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ def _render_partial(partial, options)
end

def _process_options(options)
status, content_type = options.values_at(:status, :content_type)
status, content_type, location = options.values_at(:status, :content_type, :location)
response.status = status.to_i if status
response.content_type = content_type if content_type
response.headers["Location"] = url_for(location) if location
end
end
end

0 comments on commit ad1c90d

Please sign in to comment.