diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-31 07:08:27 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-31 07:08:27 -0700 |
commit | 6a6909dc2940ac0a6d3536fdac96bdce55a18023 (patch) | |
tree | cb8c582ce21a3a58110bd6430cd74a43204e33d4 /actionpack | |
parent | 65697098811af50a0191a4fce3289b24335f96f9 (diff) | |
parent | a8560fa361958b33d76e4468eb5c07d82a20196e (diff) | |
download | rails-6a6909dc2940ac0a6d3536fdac96bdce55a18023.tar.gz rails-6a6909dc2940ac0a6d3536fdac96bdce55a18023.tar.bz2 rails-6a6909dc2940ac0a6d3536fdac96bdce55a18023.zip |
Merge pull request #8084 from acapilleri/format_never_nil
if format is unknown NullMimeTypeObject is returned
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG.md | 5 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/http/mime_type.rb | 13 | ||||
-rw-r--r-- | actionpack/test/dispatch/request_test.rb | 12 |
3 files changed, 28 insertions, 2 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 48ba1518e0..cc042b5ac3 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Added `NullMimeTypeObject` class. This allows to use html?, xml?, json?..etc when + the `format` of `request` is unknown. + + *Angelo Capilleri* + * `date_select` helper accepts `with_css_classes: true` to add css classes similar with type of generated select tags. diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index 3d560518e1..5ee6f2056e 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -153,7 +153,7 @@ module Mime end def lookup_by_extension(extension) - EXTENSION_LOOKUP[extension.to_s] + EXTENSION_LOOKUP[extension.to_s] || NullMimeTypeObject.new end # Registers an alias that's not used on mime type lookup, but can be referenced directly. Especially useful for @@ -301,6 +301,17 @@ module Mime method.to_s.ends_with? '?' end end + + class NullMimeTypeObject + private + def method_missing(method, *args) + if method.to_s.ends_with? '?' + false + else + super + end + end + end end require 'action_dispatch/http/mime_types' diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index e2964f9071..b3466faffb 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -590,7 +590,17 @@ class RequestTest < ActiveSupport::TestCase request = stub_request request.expects(:parameters).at_least_once.returns({ :format => :unknown }) - assert request.formats.empty? + assert_instance_of Mime::NullMimeTypeObject , request.format + end + + + test "format is not nil with unknown format" do + request = stub_request + request.expects(:parameters).at_least_once.returns({ format: :hello }) + assert_equal request.format.html?, false + assert_equal request.format.xml?, false + assert_equal request.format.json?, false + assert !request.format.html? end test "formats with xhr request" do |