Skip to content

Commit

Permalink
Merge pull request rails#8084 from acapilleri/format_never_nil
Browse files Browse the repository at this point in the history
if format is unknown NullMimeTypeObject is returned
  • Loading branch information
rafaelfranca committed Oct 31, 2012
2 parents 6569709 + a8560fa commit 6a6909d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions 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.

Expand Down
13 changes: 12 additions & 1 deletion actionpack/lib/action_dispatch/http/mime_type.rb
Expand Up @@ -153,7 +153,7 @@ def lookup(string)
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
Expand Down Expand Up @@ -301,6 +301,17 @@ def respond_to_missing?(method, include_private = false) #:nodoc:
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'
12 changes: 11 additions & 1 deletion actionpack/test/dispatch/request_test.rb
Expand Up @@ -590,7 +590,17 @@ def url_for(options = {})

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
Expand Down

0 comments on commit 6a6909d

Please sign in to comment.