From 5d54b8f07cef29e932e164e23713a71274c78e49 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 15 Feb 2007 16:25:46 +0000 Subject: Add Mime::Type convenience methods to check the current mime type. [Rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6152 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 9 +++++++++ actionpack/lib/action_controller/mime_type.rb | 10 ++++++++++ actionpack/lib/action_controller/session_management.rb | 4 ++++ actionpack/test/controller/mime_type_test.rb | 18 ++++++++++++++++-- actionpack/test/template/form_tag_helper_test.rb | 2 +- 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 718376d14c..6a38dd5b65 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 79da49e789..17b3d861ec 100644 --- a/actionpack/lib/action_controller/mime_type.rb +++ b/actionpack/lib/action_controller/mime_type.rb @@ -139,6 +139,16 @@ module Mime 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 60b0cd5f94..24f1651b00 100644 --- a/actionpack/lib/action_controller/session_management.rb +++ b/actionpack/lib/action_controller/session_management.rb @@ -61,6 +61,10 @@ module ActionController #:nodoc: # 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 65acbbf36e..0755f9d62d 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 @@ class MimeTypeTest < Test::Unit::TestCase 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 e7fd3b0823..cc5ab1e7f4 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -132,7 +132,7 @@ class FormTagHelperTest < Test::Unit::TestCase def test_submit_tag assert_dom_equal( - %(), + %(), submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')") ) end -- cgit v1.2.3