From 6f8159c4217dc8433a2027ced0c61e7ce94551d3 Mon Sep 17 00:00:00 2001 From: kennyj Date: Wed, 18 Jan 2012 01:53:09 +0900 Subject: Deprecate RAILS_CACHE constant. --- railties/guides/source/configuring.textile | 4 +-- railties/lib/rails.rb | 7 ++++- railties/lib/rails/application/bootstrap.rb | 8 +++--- railties/lib/rails/deprecation.rb | 39 ++++++++++++++++++++++++++++ railties/test/application/middleware_test.rb | 4 +-- 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 railties/lib/rails/deprecation.rb (limited to 'railties') diff --git a/railties/guides/source/configuring.textile b/railties/guides/source/configuring.textile index 3153cac2f9..fb8031b16d 100644 --- a/railties/guides/source/configuring.textile +++ b/railties/guides/source/configuring.textile @@ -533,7 +533,7 @@ Serves as a placeholder so that +:load_environment_config+ can be defined to run *+initialize_logger+* Initializes the logger (an +ActiveSupport::BufferedLogger+ object) for the application and makes it accessible at +Rails.logger+, provided that no initializer inserted before this point has defined +Rails.logger+. -*+initialize_cache+* If +RAILS_CACHE+ isn't set yet, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +RAILS_CACHE+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack. +*+initialize_cache+* If +Rails.cache+ isn't set yet, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +Rails.cache+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack. *+set_clear_dependencies_hook+* Provides a hook for +active_record.set_dispatch_hooks+ to use, which will run before this initializer. This initializer -- which runs only if +cache_classes+ is set to +false+ -- uses +ActionDispatch::Callbacks.after+ to remove the constants which have been referenced during the request from the object space so that they will be reloaded during the following request. @@ -571,7 +571,7 @@ The error occurred while evaluating nil.each *+action_controller.logger+* Sets +ActionController::Base.logger+ -- if it's not already set -- to +Rails.logger+. -*+action_controller.initialize_framework_caches+* Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +RAILS_CACHE+. +*+action_controller.initialize_framework_caches+* Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +Rails.cache+. *+action_controller.set_configs+* Sets up Action Controller by using the settings in +config.action_controller+ by +send+'ing the method names as setters to +ActionController::Base+ and passing the values through. diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 658756ad51..77a09507a8 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -8,6 +8,7 @@ require 'active_support/core_ext/array/extract_options' require 'rails/application' require 'rails/version' +require 'rails/deprecation' require 'active_support/railtie' require 'action_dispatch/railtie' @@ -77,7 +78,11 @@ module Rails end def cache - RAILS_CACHE + @@cache ||= nil + end + + def cache=(cache) + @@cache = cache end # Returns all rails groups for loading based on: diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index f96a7d1772..d55ec982ec 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -50,11 +50,11 @@ module Rails # Initialize cache early in the stack so railties can make use of it. initializer :initialize_cache, :group => :all do - unless defined?(RAILS_CACHE) - silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) } + unless Rails.cache + Rails.cache = ActiveSupport::Cache.lookup_store(config.cache_store) - if RAILS_CACHE.respond_to?(:middleware) - config.middleware.insert_before("Rack::Runtime", RAILS_CACHE.middleware) + if Rails.cache.respond_to?(:middleware) + config.middleware.insert_before("Rack::Runtime", Rails.cache.middleware) end end end diff --git a/railties/lib/rails/deprecation.rb b/railties/lib/rails/deprecation.rb new file mode 100644 index 0000000000..71adcd61f4 --- /dev/null +++ b/railties/lib/rails/deprecation.rb @@ -0,0 +1,39 @@ +require "active_support/string_inquirer" +require "active_support/basic_object" + +module Rails + module Initializer + def self.run(&block) + klass = Class.new(Rails::Application) + klass.instance_exec(klass.config, &block) + klass.initialize! + end + end + + class DeprecatedConstant < ActiveSupport::BasicObject + def self.deprecate(old, new) + constant = self.new(old, new) + eval "::#{old} = constant" + end + + def initialize(old, new) + @old, @new = old, new + @target = ::Kernel.eval "proc { #{@new} }" + @warned = false + end + + def method_missing(meth, *args, &block) + ::ActiveSupport::Deprecation.warn("#{@old} is deprecated. Please use #{@new}") unless @warned + @warned = true + + target = @target.call + if target.respond_to?(meth) + target.send(meth, *args, &block) + else + super + end + end + end + + DeprecatedConstant.deprecate("RAILS_CACHE", "::Rails.cache") +end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index 2f1df1fa59..d0a550d2f0 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -132,13 +132,13 @@ module ApplicationTests assert_equal "Rack::Config", middleware.second end - test "RAILS_CACHE does not respond to middleware" do + test "Rails.cache does not respond to middleware" do add_to_config "config.cache_store = :memory_store" boot! assert_equal "Rack::Runtime", middleware.third end - test "RAILS_CACHE does respond to middleware" do + test "Rails.cache does respond to middleware" do boot! assert_equal "Rack::Runtime", middleware.fourth end -- cgit v1.2.3