Skip to content

Commit

Permalink
Merge pull request #6990 from mirceapricop/bug/mime-type-method-name
Browse files Browse the repository at this point in the history
Fix: Mime type names conflict with Object methods
  • Loading branch information
José Valim committed Jul 6, 2012
2 parents 717aa92 + 021f3d2 commit cb46e7a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions actionpack/lib/abstract_controller/collector.rb
Expand Up @@ -16,6 +16,10 @@ def #{sym}(*args, &block) # def html(*args, &block)
generate_method_for_mime(mime) generate_method_for_mime(mime)
end end


Mime::Type.register_callback do |mime|
generate_method_for_mime(mime) unless self.instance_methods.include?(mime.to_sym)
end

protected protected


def method_missing(symbol, &block) def method_missing(symbol, &block)
Expand Down
13 changes: 12 additions & 1 deletion actionpack/lib/action_dispatch/http/mime_type.rb
Expand Up @@ -58,6 +58,8 @@ class Type
cattr_reader :browser_generated_types cattr_reader :browser_generated_types
attr_reader :symbol attr_reader :symbol


@register_callbacks = []

# A simple helper class used in parsing the accept header # A simple helper class used in parsing the accept header
class AcceptItem #:nodoc: class AcceptItem #:nodoc:
attr_accessor :order, :name, :q attr_accessor :order, :name, :q
Expand Down Expand Up @@ -89,6 +91,10 @@ class << self
TRAILING_STAR_REGEXP = /(text|application)\/\*/ TRAILING_STAR_REGEXP = /(text|application)\/\*/
PARAMETER_SEPARATOR_REGEXP = /;\s*\w+="?\w+"?/ PARAMETER_SEPARATOR_REGEXP = /;\s*\w+="?\w+"?/


def register_callback(&block)
@register_callbacks << block
end

def lookup(string) def lookup(string)
LOOKUP[string] LOOKUP[string]
end end
Expand All @@ -106,10 +112,15 @@ def register_alias(string, symbol, extension_synonyms = [])
def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false) def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
Mime.const_set(symbol.upcase, Type.new(string, symbol, mime_type_synonyms)) Mime.const_set(symbol.upcase, Type.new(string, symbol, mime_type_synonyms))


SET << Mime.const_get(symbol.upcase) new_mime = Mime.const_get(symbol.upcase)
SET << new_mime


([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup ([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup
([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = SET.last } ([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = SET.last }

@register_callbacks.each do |callback|
callback.call(new_mime)
end
end end


def parse(accept_header) def parse(accept_header)
Expand Down
4 changes: 3 additions & 1 deletion actionpack/test/controller/mime_responds_test.rb
Expand Up @@ -505,7 +505,7 @@ def test_invalid_format
end end


class RespondWithController < ActionController::Base class RespondWithController < ActionController::Base
respond_to :html, :json respond_to :html, :json, :touch
respond_to :xml, :except => :using_resource_with_block respond_to :xml, :except => :using_resource_with_block
respond_to :js, :only => [ :using_resource_with_block, :using_resource, 'using_hash_resource' ] respond_to :js, :only => [ :using_resource_with_block, :using_resource, 'using_hash_resource' ]


Expand Down Expand Up @@ -623,12 +623,14 @@ def setup
super super
@request.host = "www.example.com" @request.host = "www.example.com"
Mime::Type.register_alias('text/html', :iphone) Mime::Type.register_alias('text/html', :iphone)
Mime::Type.register_alias('text/html', :touch)
Mime::Type.register('text/x-mobile', :mobile) Mime::Type.register('text/x-mobile', :mobile)
end end


def teardown def teardown
super super
Mime::Type.unregister(:iphone) Mime::Type.unregister(:iphone)
Mime::Type.unregister(:touch)
Mime::Type.unregister(:mobile) Mime::Type.unregister(:mobile)
end end


Expand Down
14 changes: 14 additions & 0 deletions actionpack/test/dispatch/mime_type_test.rb
Expand Up @@ -118,6 +118,20 @@ class MimeTypeTest < ActiveSupport::TestCase
end end
end end


test "register callbacks" do
begin
registered_mimes = []
Mime::Type.register_callback do |mime|
registered_mimes << mime
end

Mime::Type.register("text/foo", :foo)
assert_equal registered_mimes, [Mime::FOO]
ensure
Mime::Type.unregister(:FOO)
end
end

test "custom type with extension aliases" do test "custom type with extension aliases" do
begin begin
Mime::Type.register "text/foobar", :foobar, [], [:foo, "bar"] Mime::Type.register "text/foobar", :foobar, [], [:foo, "bar"]
Expand Down

0 comments on commit cb46e7a

Please sign in to comment.