Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Request#formats=(extensions) that lets you set multiple formats d…

…irectly in a prioritized order
  • Loading branch information...
commit 0d0d46222cb87032e76bad42ef812b7ee7367873 1 parent 6f7b651
David Heinemeier Hansson dhh authored
16 actionpack/CHANGELOG.md
View
@@ -1,5 +1,19 @@
## Rails 4.0.0 (unreleased) ##
+* Add Request#formats=(extensions) that lets you set multiple formats directly in a prioritized order *DHH*
+
+ Example of using this for custom iphone views with an HTML fallback:
+
+ class ApplicationController < ActionController::Base
+ before_filter :adjust_format_for_iphone_with_html_fallback
+
+ private
+ def adjust_format_for_iphone_with_html_fallback
+ request.formats = [ :iphone, :html ] if request.env["HTTP_USER_AGENT"][/iPhone/]
+ end
+ end
+
+
* Add Routing Concerns to declare common routes that can be reused inside
others resources and routes.
@@ -28,7 +42,7 @@
resources :posts, concerns: [:commentable, :image_attachable]
- *David Heinemeier Hansson + Rafael Mendonça França*
+ *DHH + Rafael Mendonça França*
* Add start_hour and end_hour options to the select_hour helper. *Evan Tann*
21 actionpack/lib/action_dispatch/http/mime_negotiation.rb
View
@@ -80,6 +80,27 @@ def format=(extension)
@env["action_dispatch.request.formats"] = [Mime::Type.lookup_by_extension(parameters[:format])]
end
+ # Sets the \formats by string extensions. This differs from #format= by allowing you
+ # to set multiple, ordered formats, which is useful when you want to have a fallback.
+ #
+ # In this example, the :iphone format will be used if it's available, otherwise it'll fallback
+ # to the :html format.
+ #
+ # class ApplicationController < ActionController::Base
+ # before_filter :adjust_format_for_iphone_with_html_fallback
+ #
+ # private
+ # def adjust_format_for_iphone_with_html_fallback
+ # request.formats = [ :iphone, :html ] if request.env["HTTP_USER_AGENT"][/iPhone/]
+ # end
+ # end
+ def formats=(extensions)
+ parameters[:format] = extensions.first.to_s
+ @env["action_dispatch.request.formats"] = extensions.collect do |extension|
+ Mime::Type.lookup_by_extension(extension)
+ end
+ end
+
# Receives an array of mimes and return the first user sent mime that
# matches the order array.
#
Please sign in to comment.
Something went wrong with that request. Please try again.