提交 312f4332 编写于 作者: J José Valim

Clear DescendantsTracker on each request.

上级 d430db9f
......@@ -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 @@ def self.after(*args, &block)
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 @@ class Railtie < Rails::Railtie
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
......
require "active_support/notifications"
require "active_support/descendants_tracker"
module Rails
class Application
......@@ -55,6 +56,7 @@ module Bootstrap
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 @@ def setup
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
......
require 'isolation/abstract_unit'
class PostTest < Test::Unit::TestCase
class LoadingTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
......@@ -8,14 +8,60 @@ def setup
boot_rails
end
def test_reload_should_reload_constants
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
......@@ -23,11 +69,5 @@ class Post < ActiveRecord::Base
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
......@@ -9,7 +9,7 @@ def setup
boot_rails
FileUtils.rm_rf("#{app_path}/config/environments")
end
def test_gems_tasks_are_loaded_first_than_application_ones
app_file "lib/tasks/app.rake", <<-RUBY
$task_loaded = Rake::Task.task_defined?("db:create:all")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册