aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_controller/railtie.rb2
-rw-r--r--actionpack/lib/action_dispatch/http/rack_cache.rb5
-rw-r--r--actionpack/lib/sprockets/railtie.rb2
-rw-r--r--activerecord/lib/active_record/counter_cache.rb3
-rw-r--r--activerecord/test/cases/counter_cache_test.rb20
-rw-r--r--activerecord/test/cases/migration/logger_test.rb5
-rw-r--r--activerecord/test/fixtures/dog_lovers.yml4
-rw-r--r--activerecord/test/fixtures/dogs.yml3
-rw-r--r--activerecord/test/models/dog.rb4
-rw-r--r--activerecord/test/models/dog_lover.rb4
-rw-r--r--activerecord/test/schema/schema.rb10
-rw-r--r--activesupport/lib/active_support/callbacks.rb42
-rw-r--r--railties/guides/source/configuring.textile4
-rw-r--r--railties/lib/rails.rb7
-rw-r--r--railties/lib/rails/application/bootstrap.rb8
-rw-r--r--railties/lib/rails/deprecation.rb39
-rw-r--r--railties/test/application/middleware_test.rb4
17 files changed, 113 insertions, 53 deletions
diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb
index fb810c41b1..a288e69649 100644
--- a/actionpack/lib/action_controller/railtie.rb
+++ b/actionpack/lib/action_controller/railtie.rb
@@ -14,7 +14,7 @@ module ActionController
end
initializer "action_controller.initialize_framework_caches" do
- ActiveSupport.on_load(:action_controller) { self.cache_store ||= RAILS_CACHE }
+ ActiveSupport.on_load(:action_controller) { self.cache_store ||= Rails.cache }
end
initializer "action_controller.assets_config", :group => :all do |app|
diff --git a/actionpack/lib/action_dispatch/http/rack_cache.rb b/actionpack/lib/action_dispatch/http/rack_cache.rb
index cc8edee300..003ae4029d 100644
--- a/actionpack/lib/action_dispatch/http/rack_cache.rb
+++ b/actionpack/lib/action_dispatch/http/rack_cache.rb
@@ -8,8 +8,7 @@ module ActionDispatch
new
end
- # TODO: Finally deal with the RAILS_CACHE global
- def initialize(store = RAILS_CACHE)
+ def initialize(store = Rails.cache)
@store = store
end
@@ -33,7 +32,7 @@ module ActionDispatch
new
end
- def initialize(store = RAILS_CACHE)
+ def initialize(store = Rails.cache)
@store = store
end
diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb
index a7eb03acaf..9f4151e4b3 100644
--- a/actionpack/lib/sprockets/railtie.rb
+++ b/actionpack/lib/sprockets/railtie.rb
@@ -21,7 +21,7 @@ module Sprockets
require 'sprockets'
app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
- env.logger = ::Rails.logger
+ env.logger = config.assets.logger || ::Rails.logger
env.version = ::Rails.env + "-#{config.assets.version}"
if config.assets.cache_store != false
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index 031918712a..c9c46b8d4f 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -25,9 +25,10 @@ module ActiveRecord
self.name
end
+ foreign_key = has_many_association.foreign_key.to_s
child_class = has_many_association.klass
belongs_to = child_class.reflect_on_all_associations(:belongs_to)
- reflection = belongs_to.find { |e| e.class_name == expected_name }
+ reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key }
counter_name = reflection.counter_cache_column
stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({
diff --git a/activerecord/test/cases/counter_cache_test.rb b/activerecord/test/cases/counter_cache_test.rb
index 3ed96a3ec8..cd3d19e783 100644
--- a/activerecord/test/cases/counter_cache_test.rb
+++ b/activerecord/test/cases/counter_cache_test.rb
@@ -6,9 +6,11 @@ require 'models/engine'
require 'models/reply'
require 'models/category'
require 'models/categorization'
+require 'models/dog'
+require 'models/dog_lover'
class CounterCacheTest < ActiveRecord::TestCase
- fixtures :topics, :categories, :categorizations, :cars
+ fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers
class ::SpecialTopic < ::Topic
has_many :special_replies, :foreign_key => 'parent_id'
@@ -61,7 +63,7 @@ class CounterCacheTest < ActiveRecord::TestCase
end
end
- test "reset counter should with belongs_to which has class_name" do
+ test "reset counter with belongs_to which has class_name" do
car = cars(:honda)
assert_nothing_raised do
Car.reset_counters(car.id, :engines)
@@ -71,6 +73,20 @@ class CounterCacheTest < ActiveRecord::TestCase
end
end
+ test "reset the right counter if two have the same class_name" do
+ david = dog_lovers(:david)
+
+ DogLover.increment_counter(:bred_dogs_count, david.id)
+ DogLover.increment_counter(:trained_dogs_count, david.id)
+
+ assert_difference 'david.reload.bred_dogs_count', -1 do
+ DogLover.reset_counters(david.id, :bred_dogs)
+ end
+ assert_difference 'david.reload.trained_dogs_count', -1 do
+ DogLover.reset_counters(david.id, :trained_dogs)
+ end
+ end
+
test "update counter with initial null value" do
category = categories(:general)
assert_equal 2, category.categorizations.count
diff --git a/activerecord/test/cases/migration/logger_test.rb b/activerecord/test/cases/migration/logger_test.rb
index 8466562daf..ee0c20747e 100644
--- a/activerecord/test/cases/migration/logger_test.rb
+++ b/activerecord/test/cases/migration/logger_test.rb
@@ -3,13 +3,16 @@ require "cases/helper"
module ActiveRecord
class Migration
class LoggerTest < ActiveRecord::TestCase
+ # mysql can't roll back ddl changes
+ self.use_transactional_fixtures = false
+
Migration = Struct.new(:name, :version) do
def migrate direction
# do nothing
end
end
- def initialize(*args)
+ def setup
super
ActiveRecord::SchemaMigration.create_table
ActiveRecord::SchemaMigration.delete_all
diff --git a/activerecord/test/fixtures/dog_lovers.yml b/activerecord/test/fixtures/dog_lovers.yml
new file mode 100644
index 0000000000..d3e5e4a1aa
--- /dev/null
+++ b/activerecord/test/fixtures/dog_lovers.yml
@@ -0,0 +1,4 @@
+david:
+ id: 1
+ bred_dogs_count: 0
+ trained_dogs_count: 1
diff --git a/activerecord/test/fixtures/dogs.yml b/activerecord/test/fixtures/dogs.yml
new file mode 100644
index 0000000000..16d19be2c5
--- /dev/null
+++ b/activerecord/test/fixtures/dogs.yml
@@ -0,0 +1,3 @@
+sophie:
+ id: 1
+ trainer_id: 1
diff --git a/activerecord/test/models/dog.rb b/activerecord/test/models/dog.rb
new file mode 100644
index 0000000000..72b7d33a86
--- /dev/null
+++ b/activerecord/test/models/dog.rb
@@ -0,0 +1,4 @@
+class Dog < ActiveRecord::Base
+ belongs_to :breeder, :class_name => "DogLover", :counter_cache => :bred_dogs_count
+ belongs_to :trainer, :class_name => "DogLover", :counter_cache => :trained_dogs_count
+end
diff --git a/activerecord/test/models/dog_lover.rb b/activerecord/test/models/dog_lover.rb
new file mode 100644
index 0000000000..a33dc575c5
--- /dev/null
+++ b/activerecord/test/models/dog_lover.rb
@@ -0,0 +1,4 @@
+class DogLover < ActiveRecord::Base
+ has_many :trained_dogs, :class_name => "Dog", :foreign_key => :trainer_id
+ has_many :bred_dogs, :class_name => "Dog", :foreign_key => :breeder_id
+end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 11378c12e5..e2cd7ce9e4 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -213,6 +213,16 @@ ActiveRecord::Schema.define do
t.integer :access_level, :default => 1
end
+ create_table :dog_lovers, :force => true do |t|
+ t.integer :trained_dogs_count, :default => 0
+ t.integer :bred_dogs_count, :default => 0
+ end
+
+ create_table :dogs, :force => true do |t|
+ t.integer :trainer_id
+ t.integer :breeder_id
+ end
+
create_table :edges, :force => true, :id => false do |t|
t.column :source_id, :integer, :null => false
t.column :sink_id, :integer, :null => false
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index c20de7cd78..d4a9b93ff2 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -91,29 +91,23 @@ module ActiveSupport
class Callback #:nodoc:#
@@_callback_sequence = 0
- attr_accessor :chain, :filter, :kind, :options, :per_key, :klass, :raw_filter
+ attr_accessor :chain, :filter, :kind, :options, :klass, :raw_filter
def initialize(chain, filter, kind, options, klass)
@chain, @kind, @klass = chain, kind, klass
normalize_options!(options)
- @per_key = options.delete(:per_key)
@raw_filter, @options = filter, options
@filter = _compile_filter(filter)
@compiled_options = _compile_options(options)
@callback_id = next_id
-
- _compile_per_key_options
end
def clone(chain, klass)
obj = super()
obj.chain = chain
obj.klass = klass
- obj.per_key = @per_key.dup
obj.options = @options.dup
- obj.per_key[:if] = @per_key[:if].dup
- obj.per_key[:unless] = @per_key[:unless].dup
obj.options[:if] = @options[:if].dup
obj.options[:unless] = @options[:unless].dup
obj
@@ -124,8 +118,9 @@ module ActiveSupport
options[:unless] = Array(options[:unless])
options[:per_key] ||= {}
- options[:per_key][:if] = Array(options[:per_key][:if])
- options[:per_key][:unless] = Array(options[:per_key][:unless])
+
+ options[:if] += Array(options[:per_key][:if])
+ options[:unless] += Array(options[:per_key][:unless])
end
def name
@@ -147,22 +142,11 @@ module ActiveSupport
def recompile!(_options, _per_key)
_update_filter(self.options, _options)
- _update_filter(self.per_key, _per_key)
+ _update_filter(self.options, _per_key)
@callback_id = next_id
@filter = _compile_filter(@raw_filter)
@compiled_options = _compile_options(@options)
- _compile_per_key_options
- end
-
- def _compile_per_key_options
- key_options = _compile_options(@per_key)
-
- @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
- def _one_time_conditions_valid_#{@callback_id}?
- true if #{key_options}
- end
- RUBY_EVAL
end
# Wraps code with filter
@@ -200,11 +184,6 @@ module ActiveSupport
end
end
-
- def one_time_conditions_valid?(object)
- object.send("_one_time_conditions_valid_#{@callback_id}?")
- end
-
private
# Compile around filters with conditions into proxy methods
@@ -341,7 +320,7 @@ module ActiveSupport
method << "halted = false"
callbacks = yielding
- applicable_callbacks_for(key, object).reverse_each do |callback|
+ reverse_each do |callback|
callbacks = callback.apply(callbacks, key, object)
end
method << callbacks
@@ -369,13 +348,6 @@ module ActiveSupport
method.join("\n")
end
- # Selects callbacks that have valid <tt>:per_key</tt> condition
- def applicable_callbacks_for(key, object)
- return self unless key
- select do |callback|
- callback.one_time_conditions_valid?(object)
- end
- end
end
module ClassMethods
@@ -402,7 +374,7 @@ module ActiveSupport
end
def __callback_runner_name(key, kind)
- "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks"
+ "_run__#{self.name.hash.abs}__#{kind}__callbacks"
end
# This is used internally to append, prepend and skip callbacks to the
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