Skip to content

Commit

Permalink
stop calling deprecated methods
Browse files Browse the repository at this point in the history
We should be asking the mime type method for the mime objects rather
than via const lookup
  • Loading branch information
tenderlove committed Sep 21, 2015
1 parent efc6dd5 commit e4ba720
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 93 deletions.
6 changes: 3 additions & 3 deletions actionpack/lib/abstract_controller/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.generate_method_for_mime(mime)
const = sym.upcase
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{sym}(*args, &block) # def html(*args, &block)
custom(Mime::#{const}, *args, &block) # custom(Mime::HTML, *args, &block)
custom(Mime::Type[:#{const}], *args, &block) # custom(Mime::Type[:HTML], *args, &block)
end # end
RUBY
end
Expand All @@ -25,15 +25,15 @@ def #{sym}(*args, &block) # def html(*args, &block)
def method_missing(symbol, &block)
const_name = symbol.upcase

unless Mime.const_defined?(const_name)
unless Mime::Type.registered?(const_name)
raise NoMethodError, "To respond to a custom format, register it as a MIME type first: " \
"http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads. " \
"If you meant to respond to a variant like :tablet or :phone, not a custom format, " \
"be sure to nest your variant response within a format response: " \
"format.html { |html| html.tablet { ... } }"
end

mime_constant = Mime.const_get(const_name)
mime_constant = Mime::Type[const_name]

if Mime::SET.include?(mime_constant)
AbstractController::Collector.generate_method_for_mime(mime_constant)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/abstract_controller/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def render_to_body(options = {})
# Returns Content-Type of rendered content
# :api: public
def rendered_format
Mime::TEXT
Mime::Type[:TEXT]
end

DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %i(
Expand Down
6 changes: 3 additions & 3 deletions actionpack/lib/action_controller/metal/mime_responds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ def initialize(mimes, variant = nil)
@responses = {}
@variant = variant

mimes.each { |mime| @responses["Mime::#{mime.upcase}".constantize] = nil }
mimes.each { |mime| @responses[Mime::Type[mime.to_sym.upcase]] = nil }
end

def any(*args, &block)
if args.any?
args.each { |type| send(type, &block) }
else
custom(Mime::ALL, &block)
custom(Mime::Type[:ALL], &block)
end
end
alias :all :any
Expand All @@ -251,7 +251,7 @@ def custom(mime_type, &block)
end

def response
response = @responses.fetch(format, @responses[Mime::ALL])
response = @responses.fetch(format, @responses[Mime::Type[:ALL]])
if response.is_a?(VariantCollector) # `format.html.phone` - variant inline syntax
response.variant
elsif response.nil? || response.arity == 0 # `format.html` - just a format, call its block
Expand Down
14 changes: 7 additions & 7 deletions actionpack/lib/action_controller/metal/renderers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def self._render_with_renderer_method_name(key)
# ActionController::Renderers.add :csv do |obj, options|
# filename = options[:filename] || 'data'
# str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
# send_data str, type: Mime::CSV,
# send_data str, type: Mime::Type[:CSV],
# disposition: "attachment; filename=#{filename}.csv"
# end
#
# Note that we used Mime::CSV for the csv mime type as it comes with Rails.
# Note that we used Mime::Type[:CSV] for the csv mime type as it comes with Rails.
# For a custom renderer, you'll need to register a mime type with
# <tt>Mime::Type.register</tt>.
#
Expand Down Expand Up @@ -116,24 +116,24 @@ module All
json = json.to_json(options) unless json.kind_of?(String)

if options[:callback].present?
if content_type.nil? || content_type == Mime::JSON
self.content_type = Mime::JS
if content_type.nil? || content_type == Mime::Type[:JSON]
self.content_type = Mime::Type[:JS]
end

"/**/#{options[:callback]}(#{json})"
else
self.content_type ||= Mime::JSON
self.content_type ||= Mime::Type[:JSON]
json
end
end

add :js do |js, options|
self.content_type ||= Mime::JS
self.content_type ||= Mime::Type[:JS]
js.respond_to?(:to_js) ? js.to_js(options) : js
end

add :xml do |xml, options|
self.content_type ||= Mime::XML
self.content_type ||= Mime::Type[:XML]
xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml
end
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _render_in_priorities(options)
end

def _set_html_content_type
self.content_type = Mime::HTML.to_s
self.content_type = Mime::Type[:HTML].to_s
end

def _set_rendered_content_type(format)
Expand Down
6 changes: 3 additions & 3 deletions actionpack/lib/action_controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initialize(env, session)
self.session = session
self.session_options = TestSession::DEFAULT_OPTIONS
@custom_param_parsers = {
Mime::XML => lambda { |raw_post| Hash.from_xml(raw_post)['hash'] }
Mime::Type[:XML] => lambda { |raw_post| Hash.from_xml(raw_post)['hash'] }
}
end

Expand Down Expand Up @@ -402,7 +402,7 @@ def xml_http_request(*args)
MSG

@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
@request.env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
@request.env['HTTP_ACCEPT'] ||= [Mime::Type[:JS], Mime::Type[:HTML], Mime::Type[:XML], 'text/xml', Mime::Type[:ALL]].join(', ')
__send__(*args).tap do
@request.env.delete 'HTTP_X_REQUESTED_WITH'
@request.env.delete 'HTTP_ACCEPT'
Expand Down Expand Up @@ -505,7 +505,7 @@ def process(action, *args)
if xhr
@request.set_header 'HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'
@request.fetch_header('HTTP_ACCEPT') do |k|
@request.set_header k, [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
@request.set_header k, [Mime::Type[:JS], Mime::Type[:HTML], Mime::Type[:XML], 'text/xml', Mime::Type[:ALL]].join(', ')
end
end

Expand Down
8 changes: 4 additions & 4 deletions actionpack/lib/action_dispatch/http/mime_negotiation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def formats
elsif use_accept_header && valid_accept_header
accepts
elsif xhr?
[Mime::JS]
[Mime::Type[:JS]]
else
[Mime::HTML]
[Mime::Type[:HTML]]
end
set_header k, v
end
Expand Down Expand Up @@ -134,14 +134,14 @@ def formats=(extensions)
#
def negotiate_mime(order)
formats.each do |priority|
if priority == Mime::ALL
if priority == Mime::Type[:ALL]
return order.first
elsif order.include?(priority)
return priority
end
end

order.include?(Mime::ALL) ? format : nil
order.include?(Mime::Type[:ALL]) ? format : nil
end

protected
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_dispatch/http/mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def const_defined?(sym, inherit = true)
ActiveSupport::Deprecation.warn <<-eow
Accessing mime types via constants is deprecated. Please change:
`Mime::#{sym}`
`Mime.const_defined?(#{sym})`
to:
`Mime::Type[:#{sym}]`
`Mime::Type.registered?(:#{sym})`
eow
true
else
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def assign_default_content_type_and_charset!
return if content_type

ct = parse_content_type
set_content_type(ct.mime_type || Mime::HTML.to_s,
set_content_type(ct.mime_type || Mime::Type[:HTML].to_s,
ct.charset || self.class.default_charset)
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/testing/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Assertions
include Rails::Dom::Testing::Assertions

def html_document
@html_document ||= if @response.content_type === Mime::XML
@html_document ||= if @response.content_type === Mime::Type[:XML]
Nokogiri::XML::Document.parse(@response.body)
else
Nokogiri::HTML::Document.parse(@response.body)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/testing/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false)
if xhr
headers ||= {}
headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
headers['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
headers['HTTP_ACCEPT'] ||= [Mime::Type[:JS], Mime::Type[:HTML], Mime::Type[:XML], 'text/xml', Mime::Type[:ALL]].join(', ')
end

# this modifies the passed request_env directly
Expand Down
6 changes: 3 additions & 3 deletions actionpack/test/abstract/collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class TestCollector < ActiveSupport::TestCase
collector.html
collector.text(:foo)
collector.js(:bar) { :baz }
assert_equal [Mime::HTML, [], nil], collector.responses[0]
assert_equal [Mime::TEXT, [:foo], nil], collector.responses[1]
assert_equal [Mime::JS, [:bar]], collector.responses[2][0,2]
assert_equal [Mime::Type[:HTML], [], nil], collector.responses[0]
assert_equal [Mime::Type[:TEXT], [:foo], nil], collector.responses[1]
assert_equal [Mime::Type[:JS], [:bar]], collector.responses[2][0,2]
assert_equal :baz, collector.responses[2][2].call
end
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/action_pack_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def render_url
end

def render_text_with_custom_content_type
render body: "Hello!", content_type: Mime::RSS
render body: "Hello!", content_type: Mime::Type[:RSS]
end

def session_stuffing
Expand Down
44 changes: 22 additions & 22 deletions actionpack/test/controller/content_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class OldContentTypeController < ActionController::Base
# :ported:
def render_content_type_from_body
response.content_type = Mime::RSS
response.content_type = Mime::Type[:RSS]
render body: "hello world!"
end

Expand All @@ -14,7 +14,7 @@ def render_defaults

# :ported:
def render_content_type_from_render
render body: "hello world!", :content_type => Mime::RSS
render body: "hello world!", :content_type => Mime::Type[:RSS]
end

# :ported:
Expand All @@ -36,7 +36,7 @@ def render_default_for_builder
end

def render_change_for_builder
response.content_type = Mime::HTML
response.content_type = Mime::Type[:HTML]
render :action => "render_default_for_builder"
end

Expand All @@ -45,7 +45,7 @@ def render_default_content_types_for_respond_to
format.html { render body: "hello world!" }
format.xml { render action: "render_default_content_types_for_respond_to" }
format.js { render body: "hello world!" }
format.rss { render body: "hello world!", content_type: Mime::XML }
format.rss { render body: "hello world!", content_type: Mime::Type[:XML] }
end
end
end
Expand All @@ -64,68 +64,68 @@ def setup
def test_render_defaults
get :render_defaults
assert_equal "utf-8", @response.charset
assert_equal Mime::TEXT, @response.content_type
assert_equal Mime::Type[:TEXT], @response.content_type
end

def test_render_changed_charset_default
with_default_charset "utf-16" do
get :render_defaults
assert_equal "utf-16", @response.charset
assert_equal Mime::TEXT, @response.content_type
assert_equal Mime::Type[:TEXT], @response.content_type
end
end

# :ported:
def test_content_type_from_body
get :render_content_type_from_body
assert_equal Mime::RSS, @response.content_type
assert_equal Mime::Type[:RSS], @response.content_type
assert_equal "utf-8", @response.charset
end

# :ported:
def test_content_type_from_render
get :render_content_type_from_render
assert_equal Mime::RSS, @response.content_type
assert_equal Mime::Type[:RSS], @response.content_type
assert_equal "utf-8", @response.charset
end

# :ported:
def test_charset_from_body
get :render_charset_from_body
assert_equal Mime::TEXT, @response.content_type
assert_equal Mime::Type[:TEXT], @response.content_type
assert_equal "utf-16", @response.charset
end

# :ported:
def test_nil_charset_from_body
get :render_nil_charset_from_body
assert_equal Mime::TEXT, @response.content_type
assert_equal Mime::Type[:TEXT], @response.content_type
assert_equal "utf-8", @response.charset, @response.headers.inspect
end

def test_nil_default_for_erb
with_default_charset nil do
get :render_default_for_erb
assert_equal Mime::HTML, @response.content_type
assert_equal Mime::Type[:HTML], @response.content_type
assert_nil @response.charset, @response.headers.inspect
end
end

def test_default_for_erb
get :render_default_for_erb
assert_equal Mime::HTML, @response.content_type
assert_equal Mime::Type[:HTML], @response.content_type
assert_equal "utf-8", @response.charset
end

def test_default_for_builder
get :render_default_for_builder
assert_equal Mime::XML, @response.content_type
assert_equal Mime::Type[:XML], @response.content_type
assert_equal "utf-8", @response.charset
end

def test_change_for_builder
get :render_change_for_builder
assert_equal Mime::HTML, @response.content_type
assert_equal Mime::Type[:HTML], @response.content_type
assert_equal "utf-8", @response.charset
end

Expand All @@ -144,24 +144,24 @@ class AcceptBasedContentTypeTest < ActionController::TestCase
tests OldContentTypeController

def test_render_default_content_types_for_respond_to
@request.accept = Mime::HTML.to_s
@request.accept = Mime::Type[:HTML].to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::HTML, @response.content_type
assert_equal Mime::Type[:HTML], @response.content_type

@request.accept = Mime::JS.to_s
@request.accept = Mime::Type[:JS].to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::JS, @response.content_type
assert_equal Mime::Type[:JS], @response.content_type
end

def test_render_default_content_types_for_respond_to_with_template
@request.accept = Mime::XML.to_s
@request.accept = Mime::Type[:XML].to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::XML, @response.content_type
assert_equal Mime::Type[:XML], @response.content_type
end

def test_render_default_content_types_for_respond_to_with_overwrite
@request.accept = Mime::RSS.to_s
@request.accept = Mime::Type[:RSS].to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::XML, @response.content_type
assert_equal Mime::Type[:XML], @response.content_type
end
end
4 changes: 2 additions & 2 deletions actionpack/test/controller/new_base/content_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def index
end

def set_on_response_obj
response.content_type = Mime::RSS
response.content_type = Mime::Type[:RSS]
render body: "Hello world!"
end

def set_on_render
render body: "Hello world!", content_type: Mime::RSS
render body: "Hello world!", content_type: Mime::Type[:RSS]
end
end

Expand Down
Loading

0 comments on commit e4ba720

Please sign in to comment.