Skip to content

Commit e7e81b4

Browse files
author
José Valim
committed
Merge pull request rails#11389 from jetthoughts/11381_fix_hit_database_on_precompile
rails#11381: Ignore config.eager_load=true for rake
2 parents ddf79ab + 9cac69c commit e7e81b4

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `rake environment` to do not eager load modules
2+
3+
*Paul Nikitochkin*
4+
15
* Fix `rake notes` to look into `*.sass` files
26

37
*Yuri Artemev*

railties/lib/rails/application.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ def run_tasks_blocks(app) #:nodoc:
291291
require "rails/tasks"
292292
config = self.config
293293
task :environment do
294-
config.eager_load = false
294+
ActiveSupport.on_load(:before_initialize) { config.eager_load = false }
295+
295296
require_environment!
296297
end
297298
end

railties/test/application/assets_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_precompile_does_not_hit_the_database
9191
class UsersController < ApplicationController; end
9292
eoruby
9393
app_file "app/models/user.rb", <<-eoruby
94-
class User < ActiveRecord::Base; end
94+
class User < ActiveRecord::Base; raise 'should not be reached'; end
9595
eoruby
9696

9797
ENV['RAILS_ENV'] = 'production'

railties/test/application/rake_test.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class RakeTest < ActiveSupport::TestCase
99
def setup
1010
build_app
1111
boot_rails
12-
FileUtils.rm_rf("#{app_path}/config/environments")
1312
end
1413

1514
def teardown
@@ -56,44 +55,47 @@ def test_initializers_are_executed_in_rake_tasks
5655
assert_match "Doing something...", output
5756
end
5857

59-
def test_does_not_explode_when_accessing_a_model_with_eager_load
58+
def test_does_not_explode_when_accessing_a_model
6059
add_to_config <<-RUBY
61-
config.eager_load = true
62-
6360
rake_tasks do
6461
task do_nothing: :environment do
6562
Hello.new.world
6663
end
6764
end
6865
RUBY
6966

70-
app_file "app/models/hello.rb", <<-RUBY
71-
class Hello
72-
def world
73-
puts "Hello world"
67+
app_file 'app/models/hello.rb', <<-RUBY
68+
class Hello
69+
def world
70+
puts 'Hello world'
71+
end
7472
end
75-
end
7673
RUBY
7774

78-
output = Dir.chdir(app_path){ `rake do_nothing` }
79-
assert_match "Hello world", output
75+
output = Dir.chdir(app_path) { `rake do_nothing` }
76+
assert_match 'Hello world', output
8077
end
8178

82-
def test_should_not_eager_load_model_path_for_rake
79+
def test_should_not_eager_load_model_for_rake
8380
add_to_config <<-RUBY
84-
config.eager_load = true
85-
8681
rake_tasks do
8782
task do_nothing: :environment do
8883
end
8984
end
9085
RUBY
9186

92-
app_file "app/models/hello.rb", <<-RUBY
93-
raise 'should not be pre-required for rake even `eager_load=true`'
87+
add_to_env_config 'production', <<-RUBY
88+
config.eager_load = true
89+
RUBY
90+
91+
app_file 'app/models/hello.rb', <<-RUBY
92+
raise 'should not be pre-required for rake even eager_load=true'
9493
RUBY
9594

96-
Dir.chdir(app_path){ `rake do_nothing` }
95+
Dir.chdir(app_path) do
96+
assert system('rake do_nothing RAILS_ENV=production'),
97+
'should not be pre-required for rake even eager_load=true'
98+
end
9799
end
98100

99101
def test_code_statistics_sanity

0 commit comments

Comments
 (0)