diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 718376d14c7e8..6a38dd5b65006 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,14 @@ *SVN* +* Add Mime::Type convenience methods to check the current mime type. [Rick] + + request.format.html? # => true if Mime::HTML + request.format.jpg? # => true if Mime::JPG + + # ActionController sample usage: + # the session will be disabled for non html/ajax requests + session :off, :if => Proc.new { |req| !(req.format.html? || req.format.js?) } + * Performance: patch cgi/session to require digest/md5 once rather than per #create_new_id. [Stefan Kaes] * Add a :url_based_filename => true option to ActionController::Streaming::send_file, which allows URL-based filenames. [Thomas Fuchs] diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb index 79da49e789b97..17b3d861ec47d 100644 --- a/actionpack/lib/action_controller/mime_type.rb +++ b/actionpack/lib/action_controller/mime_type.rb @@ -139,6 +139,16 @@ def ===(list) def ==(mime_type) (@synonyms + [ self ]).any? { |synonym| synonym.to_s == mime_type.to_s } if mime_type end + + private + def method_missing(method, *args) + if method.to_s =~ /(\w+)\?$/ + mime_type = $1.downcase.to_sym + mime_type == @symbol || (mime_type == :html && @symbol == :all) + else + super + end + end end end diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb index 60b0cd5f94e40..24f1651b0083c 100644 --- a/actionpack/lib/action_controller/session_management.rb +++ b/actionpack/lib/action_controller/session_management.rb @@ -61,6 +61,10 @@ def session_options # session :off, :only => :foo, # :if => Proc.new { |req| req.parameters[:ws] } # + # # the session will be disabled for non html/ajax requests + # session :off, + # :if => Proc.new { |req| !(req.format.html? || req.format.js?) } + # # All session options described for ActionController::Base.process_cgi # are valid arguments. def session(*args) diff --git a/actionpack/test/controller/mime_type_test.rb b/actionpack/test/controller/mime_type_test.rb index 65acbbf36e64a..0755f9d62d4c8 100644 --- a/actionpack/test/controller/mime_type_test.rb +++ b/actionpack/test/controller/mime_type_test.rb @@ -1,8 +1,8 @@ require File.dirname(__FILE__) + '/../abstract_unit' class MimeTypeTest < Test::Unit::TestCase - Mime::PNG = Mime::Type.new("image/png") - Mime::PLAIN = Mime::Type.new("text/plain") + Mime::Type.register "image/png", :png + Mime::Type.register "text/plain", :plain def test_parse_single Mime::LOOKUP.keys.each do |mime_type| @@ -30,4 +30,18 @@ def test_custom_type end Mime.send :remove_const, :GIF end + + def test_type_convenience_methods + types = [:html, :xml, :png, :plain, :yaml] + types.each do |type| + mime = Mime.const_get(type.to_s.upcase) + assert mime.send("#{type}?"), "Mime::#{type.to_s.upcase} is not #{type}?" + (types - [type]).each { |t| assert !mime.send("#{t}?"), "Mime::#{t.to_s.upcase} is #{t}?" } + end + end + + def test_mime_all_is_html + assert Mime::ALL.all?, "Mime::ALL is not all?" + assert Mime::ALL.html?, "Mime::ALL is not html?" + end end \ No newline at end of file diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index e7fd3b082383c..cc5ab1e7f4282 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -132,7 +132,7 @@ def test_stringify_symbol_keys def test_submit_tag assert_dom_equal( - %(), + %(), submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')") ) end