mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Clear DescendantsTracker on each request.
This commit is contained in:
parent
d430db9fd4
commit
312f433241
7 changed files with 81 additions and 40 deletions
|
@ -8,7 +8,7 @@ module ActionDispatch
|
|||
class Callbacks
|
||||
include ActiveSupport::Callbacks
|
||||
|
||||
define_callbacks :call, :terminator => "result == false", :rescuable => true
|
||||
define_callbacks :call, :rescuable => true
|
||||
define_callbacks :prepare, :scope => :name
|
||||
|
||||
# Add a preparation callback. Preparation callbacks are run before every
|
||||
|
@ -37,12 +37,12 @@ module ActionDispatch
|
|||
|
||||
def initialize(app, prepare_each_request = false)
|
||||
@app, @prepare_each_request = app, prepare_each_request
|
||||
run_callbacks(:prepare)
|
||||
_run_prepare_callbacks
|
||||
end
|
||||
|
||||
def call(env)
|
||||
run_callbacks(:call) do
|
||||
run_callbacks(:prepare) if @prepare_each_request
|
||||
_run_call_callbacks do
|
||||
_run_prepare_callbacks if @prepare_each_request
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -68,7 +68,6 @@ module ActiveRecord
|
|||
unless app.config.cache_classes
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
ActionDispatch::Callbacks.after do
|
||||
ActiveRecord::Base.reset_subclasses
|
||||
ActiveRecord::Base.clear_reloadable_connections!
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "active_support/notifications"
|
||||
require "active_support/descendants_tracker"
|
||||
|
||||
module Rails
|
||||
class Application
|
||||
|
@ -55,6 +56,7 @@ module Rails
|
|||
initializer :set_clear_dependencies_hook do
|
||||
unless config.cache_classes
|
||||
ActionDispatch::Callbacks.after do
|
||||
ActiveSupport::DescendantsTracker.clear
|
||||
ActiveSupport::Dependencies.clear
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,7 +19,7 @@ module ApplicationTests
|
|||
assert $:.include?("#{app_path}/app/models")
|
||||
end
|
||||
|
||||
test "initializing an application adds lib path on inheritance hook" do
|
||||
test "initializing an application allows to load code on lib path inside application class definitation" do
|
||||
app_file "lib/foo.rb", <<-RUBY
|
||||
module Foo; end
|
||||
RUBY
|
||||
|
|
73
railties/test/application/loading_test.rb
Normal file
73
railties/test/application/loading_test.rb
Normal file
|
@ -0,0 +1,73 @@
|
|||
require 'isolation/abstract_unit'
|
||||
|
||||
class LoadingTest < Test::Unit::TestCase
|
||||
include ActiveSupport::Testing::Isolation
|
||||
|
||||
def setup
|
||||
build_app
|
||||
boot_rails
|
||||
end
|
||||
|
||||
def app
|
||||
@app ||= Rails.application
|
||||
end
|
||||
|
||||
def test_load_should_load_constants
|
||||
app_file "app/models/post.rb", <<-MODEL
|
||||
class Post < ActiveRecord::Base
|
||||
validates_acceptance_of :title, :accept => "omg"
|
||||
end
|
||||
MODEL
|
||||
|
||||
require "#{rails_root}/config/environment"
|
||||
setup_ar!
|
||||
|
||||
p = Post.create(:title => 'omg')
|
||||
assert_equal 1, Post.count
|
||||
assert_equal 'omg', p.title
|
||||
p = Post.first
|
||||
assert_equal 'omg', p.title
|
||||
end
|
||||
|
||||
def test_descendants_are_cleaned_on_each_request_without_cache_classes
|
||||
add_to_config <<-RUBY
|
||||
config.cache_classes = false
|
||||
RUBY
|
||||
|
||||
app_file "app/models/post.rb", <<-MODEL
|
||||
class Post < ActiveRecord::Base
|
||||
end
|
||||
MODEL
|
||||
|
||||
app_file 'config/routes.rb', <<-RUBY
|
||||
AppTemplate::Application.routes.draw do |map|
|
||||
match '/load', :to => lambda { |env| [200, {}, Post.all] }
|
||||
match '/unload', :to => lambda { |env| [200, {}, []] }
|
||||
end
|
||||
RUBY
|
||||
|
||||
require 'rack/test'
|
||||
extend Rack::Test::Methods
|
||||
|
||||
require "#{rails_root}/config/environment"
|
||||
setup_ar!
|
||||
|
||||
assert_equal [], ActiveRecord::Base.descendants
|
||||
get "/load"
|
||||
assert_equal [Post], ActiveRecord::Base.descendants
|
||||
get "/unload"
|
||||
assert_equal [], ActiveRecord::Base.descendants
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def setup_ar!
|
||||
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
||||
ActiveRecord::Migration.verbose = false
|
||||
ActiveRecord::Schema.define(:version => 1) do
|
||||
create_table :posts do |t|
|
||||
t.string :title
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,33 +0,0 @@
|
|||
require 'isolation/abstract_unit'
|
||||
|
||||
class PostTest < Test::Unit::TestCase
|
||||
include ActiveSupport::Testing::Isolation
|
||||
|
||||
def setup
|
||||
build_app
|
||||
boot_rails
|
||||
end
|
||||
|
||||
def test_reload_should_reload_constants
|
||||
app_file "app/models/post.rb", <<-MODEL
|
||||
class Post < ActiveRecord::Base
|
||||
validates_acceptance_of :title, :accept => "omg"
|
||||
end
|
||||
MODEL
|
||||
|
||||
require "#{rails_root}/config/environment"
|
||||
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
||||
ActiveRecord::Migration.verbose = false
|
||||
ActiveRecord::Schema.define(:version => 1) do
|
||||
create_table :posts do |t|
|
||||
t.string :title
|
||||
end
|
||||
end
|
||||
|
||||
p = Post.create(:title => 'omg')
|
||||
assert_equal 1, Post.count
|
||||
assert_equal 'omg', p.title
|
||||
p = Post.first
|
||||
assert_equal 'omg', p.title
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue