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
This commit is contained in:
Rick Olson
2007-02-15 16:25:46 +00:00
parent 5b7630e174
commit 5d54b8f07c
5 changed files with 40 additions and 3 deletions

View File

@@ -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]

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -132,7 +132,7 @@ class FormTagHelperTest < Test::Unit::TestCase
def test_submit_tag
assert_dom_equal(
%(<input name='commit' type='submit' value='Save' onclick="this.disabled=true;this.value='Saving...';alert('hello!');return (this.form.onsubmit ? this.form.onsubmit() : true)" />),
%(<input name='commit' type='submit' value='Save' onclick="this.disabled=true;this.value='Saving...';alert('hello!');return (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())" />),
submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')")
)
end