Permalink
Browse files

Added that render :json will automatically call .to_json unless its b…

…eing passed a string [DHH] Added Mime::Type.register_alias for dealing with different formats using the same mime type [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7520 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent e86d1cd commit 1373991dd8feacb0c09a6115a271777810807668 @dhh dhh committed Sep 20, 2007
Showing with 34 additions and 2 deletions.
  1. +25 −0 actionpack/CHANGELOG
  2. +1 −0 actionpack/lib/action_controller/base.rb
  3. +8 −2 actionpack/lib/action_controller/mime_type.rb
View
@@ -1,5 +1,30 @@
*SVN*
+* Added Mime::Type.register_alias for dealing with different formats using the same mime type [DHH]. Example:
+
+ class PostsController < ApplicationController
+ before_filter :adjust_format_for_iphone
+
+ def index
+ @posts = Post.find(:all)
+
+ respond_to do |format|
+ format.html # => renders index.html.erb and uses "text/html" as the content type
+ format.iphone # => renders index.iphone.erb and uses "text/html" as the content type
+ end
+ end
+
+
+ private
+ def adjust_format_for_iphone
+ if request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/iPhone/]
+ request.format = :iphone
+ end
+ end
+ end
+
+* Added that render :json will automatically call .to_json unless it's being passed a string [DHH].
+
* Autolink behaves well with emails embedded in URLs. #7313 [Jeremy McAnally, tarmo]
* Fixed that default layouts did not take the format into account #9564 [lifofifo]
@@ -826,6 +826,7 @@ def render(options = nil, &block) #:doc:
render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status])
elsif json = options[:json]
+ json = json.to_json unless json.is_a?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
response.content_type = Mime::JSON
render_for_text(json, options[:status])
@@ -52,12 +52,18 @@ def lookup_by_extension(extension)
EXTENSION_LOOKUP[extension]
end
- def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [])
+ # Registers an alias that's not usd on mime type lookup, but can be referenced directly. Especially useful for
+ # rendering different HTML versions depending on the user agent, like an iPhone.
+ def register_alias(string, symbol, extension_synonyms = [])
+ register(string, symbol, [], extension_synonyms, true)
+ end
+
+ def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms)
SET << Mime.send(:const_get, symbol.to_s.upcase)
- ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last }
+ ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup
([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
end

0 comments on commit 1373991

Please sign in to comment.