diff --git a/lib/sinatra/indifferent_hash.rb b/lib/sinatra/indifferent_hash.rb index e5cf6cb33c..635b71b7d2 100644 --- a/lib/sinatra/indifferent_hash.rb +++ b/lib/sinatra/indifferent_hash.rb @@ -1,21 +1,23 @@ # frozen_string_literal: true -if !$LOAD_PATH.grep(%r{gems/activesupport}).empty? && $LOADED_FEATURES.grep(%r{active_support/core_ext/hash}).empty? - puts <<-EOF +if !Hash.method_defined?(:slice) && !$LOAD_PATH.grep(%r{gems/activesupport}).empty? + if ENV['APP_ENV'] == 'test' && File.basename(File.dirname(ENV['BUNDLE_GEMFILE'])) == 'sinatra' + # Some extensions get loaded during internal testing (e.g. by RABL and our + # RABL test) that we have no control over, but we need them to load + # *before* IndifferentHash, so we'll do it preemptively here. + # + # Newer Rubies have these methods built-in, so the extensions are no-ops. + require 'active_support/core_ext/hash/conversions' + require 'active_support/core_ext/hash/slice' + require 'active_support/core_ext/hash/keys' + elsif ENV['SINATRA_ACTIVESUPPORT_WARNING'] != 'false' + $stderr.puts <<-EOF WARNING: If you plan to load any of ActiveSupport's core extensions to Hash, be sure to do so *before* loading Sinatra::Application or Sinatra::Base. If not, you may disregard this warning. - EOF -end -if ENV['APP_ENV'] == 'test' && !Hash.method_defined?(:slice) - # Some extensions get loaded during testing (e.g. by RABL and our RABL test) - # that we have no control over, but we need it to load *before* - # IndifferentHash, so we'll do it preemptively here. - # - # Newer Rubies have these methods built-in, so the extensions are no-ops. - require 'active_support/core_ext/hash/conversions' - require 'active_support/core_ext/hash/slice' - require 'active_support/core_ext/hash/keys' +Set SINATRA_ACTIVESUPPORT_WARNING=false in the environment to hide this warning. + EOF + end end module Sinatra