diff --git a/actionpack/lib/action_dispatch/middleware/callbacks.rb b/actionpack/lib/action_dispatch/middleware/callbacks.rb index d07841218a..e4ae480bfb 100644 --- a/actionpack/lib/action_dispatch/middleware/callbacks.rb +++ b/actionpack/lib/action_dispatch/middleware/callbacks.rb @@ -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 diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index a32fb7d399..0386fd4651 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -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 diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index 0a435f0f36..44e26b5713 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -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 diff --git a/railties/test/application/initializers/load_path_test.rb b/railties/test/application/initializers/load_path_test.rb index d31915e129..714d62311d 100644 --- a/railties/test/application/initializers/load_path_test.rb +++ b/railties/test/application/initializers/load_path_test.rb @@ -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 diff --git a/railties/test/application/loading_test.rb b/railties/test/application/loading_test.rb new file mode 100644 index 0000000000..b337d3fc6e --- /dev/null +++ b/railties/test/application/loading_test.rb @@ -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 diff --git a/railties/test/application/model_initialization_test.rb b/railties/test/application/model_initialization_test.rb deleted file mode 100644 index 6a22f8d8df..0000000000 --- a/railties/test/application/model_initialization_test.rb +++ /dev/null @@ -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 diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 6b7a471494..40fb446b16 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -9,7 +9,7 @@ module ApplicationTests 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")