aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-05-31 18:52:24 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-05-31 18:52:24 +0000
commitf9cd92f4ee49c689f25dbe7da008fb298a5feb4f (patch)
tree1498f6f638177ff53f99fb85cb89dc3c01c884e9
parente7fe1c47baf89522617b67e96b701ee916adcb88 (diff)
downloadrails-f9cd92f4ee49c689f25dbe7da008fb298a5feb4f.tar.gz
rails-f9cd92f4ee49c689f25dbe7da008fb298a5feb4f.tar.bz2
rails-f9cd92f4ee49c689f25dbe7da008fb298a5feb4f.zip
Added interrogation of params[:format] to determine Accept type. If :format is specified and matches a declared extension, like "rss" or "xml", that mime type will be put in front of the accept handler. This means you can link to the same action from different extensions and use that fact to determine output [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4384 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG34
-rw-r--r--actionpack/lib/action_controller/mime_responds.rb7
-rw-r--r--actionpack/lib/action_controller/mime_type.rb17
-rw-r--r--actionpack/test/controller/mime_responds_test.rb14
4 files changed, 71 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 81c660086c..0bfbb978b6 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,39 @@
*SVN*
+* Added interrogation of params[:format] to determine Accept type. If :format is specified and matches a declared extension, like "rss" or "xml", that mime type will be put in front of the accept handler. This means you can link to the same action from different extensions and use that fact to determine output [DHH]. Example:
+
+ class WeblogController < ActionController::Base
+ def index
+ @posts = Post.find :all
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @posts.to_xml }
+ format.rss { render :action => "feed.rxml" }
+ end
+ end
+ end
+
+ # returns HTML when requested by a browser, since the browser
+ # has the HTML mimetype at the top of its priority list
+ Accept: text/html
+ GET /weblog
+
+ # returns the XML
+ Accept: application/xml
+ GET /weblog
+
+ # returns the HTML
+ Accept: application/xml
+ GET /weblog.html
+
+ # returns the XML
+ Accept: text/html
+ GET /weblog.xml
+
+ All this relies on the fact that you have a route that includes .:format.
+
+
* Expanded :method option in FormTagHelper#form_tag, FormHelper#form_for, PrototypeHelper#remote_form_for, PrototypeHelper#remote_form_tag, and PrototypeHelper#link_to_remote to allow for verbs other than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post [DHH]
* Added :method option to UrlHelper#link_to, which allows for using other verbs than GET for the link. This replaces the :post option, which is now deprecated. Example: link_to "Destroy", person_url(:id => person), :method => :delete [DHH]
diff --git a/actionpack/lib/action_controller/mime_responds.rb b/actionpack/lib/action_controller/mime_responds.rb
index ff4837896c..e1e6790381 100644
--- a/actionpack/lib/action_controller/mime_responds.rb
+++ b/actionpack/lib/action_controller/mime_responds.rb
@@ -110,7 +110,12 @@ module ActionController #:nodoc:
def initialize(block_binding)
@block_binding = block_binding
- @mime_type_priority = eval("request.accepts", block_binding)
+ @mime_type_priority = eval(
+ "(params[:format] && Mime::EXTENSION_LOOKUP[params[:format]]) ? " +
+ "[ Mime::EXTENSION_LOOKUP[params[:format]] ] : request.accepts",
+ block_binding
+ )
+
@order = []
@responses = {}
end
diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb
index 30cea88a44..ff65e18e44 100644
--- a/actionpack/lib/action_controller/mime_type.rb
+++ b/actionpack/lib/action_controller/mime_type.rb
@@ -119,6 +119,7 @@ module Mime
ATOM = Type.new "application/atom+xml", :atom
YAML = Type.new "application/x-yaml", :yaml, %w( text/yaml )
+
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) }
LOOKUP["*/*"] = ALL
@@ -139,4 +140,20 @@ module Mime
LOOKUP["application/rss+xml"] = RSS
LOOKUP["application/atom+xml"] = ATOM
+
+
+ EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) }
+
+ EXTENSION_LOOKUP["html"] = HTML
+ EXTENSION_LOOKUP["xhtml"] = HTML
+
+ EXTENSION_LOOKUP["xml"] = XML
+
+ EXTENSION_LOOKUP["js"] = JS
+
+ EXTENSION_LOOKUP["yml"] = YAML
+ EXTENSION_LOOKUP["yaml"] = YAML
+
+ EXTENSION_LOOKUP["rss"] = RSS
+ EXTENSION_LOOKUP["atom"] = ATOM
end \ No newline at end of file
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index e696a41cb5..c3763bc566 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -254,4 +254,18 @@ class MimeControllerTest < Test::Unit::TestCase
xhr :get, :using_defaults
assert_equal '$("body").visualEffect("highlight");', @response.body
end
+
+ def test_forced_format
+ get :html_xml_or_rss
+ assert_equal "HTML", @response.body
+
+ get :html_xml_or_rss, :format => "html"
+ assert_equal "HTML", @response.body
+
+ get :html_xml_or_rss, :format => "xml"
+ assert_equal "XML", @response.body
+
+ get :html_xml_or_rss, :format => "rss"
+ assert_equal "RSS", @response.body
+ end
end