Permalink
Browse files

Add Mime::Type convenience methods to check the current mime type. [R…

…ick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6152 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 5b7630e commit 5d54b8f07cef29e932e164e23713a71274c78e49 @technoweenie technoweenie committed Feb 15, 2007
View
@@ -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]
@@ -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
@@ -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)
@@ -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
@@ -132,7 +132,7 @@ def test_stringify_symbol_keys
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

0 comments on commit 5d54b8f

Please sign in to comment.