Skip to content

Commit

Permalink
Add Mime::Type convenience methods to check the current mime type. [R…
Browse files Browse the repository at this point in the history
…ick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6152 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Feb 15, 2007
1 parent 5b7630e commit 5d54b8f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
9 changes: 9 additions & 0 deletions 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]
Expand Down
10 changes: 10 additions & 0 deletions actionpack/lib/action_controller/mime_type.rb
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions actionpack/lib/action_controller/session_management.rb
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions 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|
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion actionpack/test/template/form_tag_helper_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 5d54b8f

Please sign in to comment.