Skip to content

Commit 924e53f

Browse files
committed
[Railties] Add config rake_eager_load
1 parent 4cc1c14 commit 924e53f

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

railties/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Allow configuration of eager_load behaviour for rake environment:
2+
3+
`config.rake_eager_load`
4+
5+
Defaults to `false` as per previous behaviour.
6+
7+
*Thierry Joyal*
8+
19
## Rails 5.1.0.beta1 (February 23, 2017) ##
210

311
* Fix running multiple tests in one `rake` command

railties/lib/rails/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def run_tasks_blocks(app) #:nodoc:
438438
super
439439
require "rails/tasks"
440440
task :environment do
441-
ActiveSupport.on_load(:before_initialize) { config.eager_load = false }
441+
ActiveSupport.on_load(:before_initialize) { config.eager_load = config.rake_eager_load }
442442

443443
require_environment!
444444
end

railties/lib/rails/application/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Configuration < ::Rails::Engine::Configuration
1414
:ssl_options, :public_file_server,
1515
:session_options, :time_zone, :reload_classes_only_on_change,
1616
:beginning_of_week, :filter_redirect, :x, :enable_dependency_loading,
17-
:read_encrypted_secrets
17+
:rake_eager_load, :read_encrypted_secrets
1818

1919
attr_writer :log_level
2020
attr_reader :encoding, :api_only
@@ -52,6 +52,7 @@ def initialize(*)
5252
@debug_exception_response_format = nil
5353
@x = Custom.new
5454
@enable_dependency_loading = false
55+
@rake_eager_load = false
5556
@read_encrypted_secrets = false
5657
end
5758

railties/test/application/configuration_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,5 +1545,21 @@ def index
15451545
assert_equal 301, last_response.status
15461546
assert_equal "https://example.org/", last_response.location
15471547
end
1548+
1549+
test "rake_eager_load is false by default" do
1550+
app "development"
1551+
assert_equal false, Rails.application.config.rake_eager_load
1552+
end
1553+
1554+
test "rake_eager_load is set correctly" do
1555+
add_to_config <<-RUBY
1556+
config.root = "#{app_path}"
1557+
config.rake_eager_load = true
1558+
RUBY
1559+
1560+
app "development"
1561+
1562+
assert_equal true, Rails.application.config.rake_eager_load
1563+
end
15481564
end
15491565
end

railties/test/application/rake_test.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def world
9494
assert_match "Hello world", output
9595
end
9696

97-
def test_should_not_eager_load_model_for_rake
97+
def test_should_not_eager_load_model_for_rake_when_rake_eager_load_is_false
9898
add_to_config <<-RUBY
9999
rake_tasks do
100100
task do_nothing: :environment do
@@ -116,6 +116,29 @@ def test_should_not_eager_load_model_for_rake
116116
end
117117
end
118118

119+
def test_should_eager_load_model_for_rake_when_rake_eager_load_is_true
120+
add_to_config <<-RUBY
121+
rake_tasks do
122+
task do_something: :environment do
123+
puts "Answer: " + Hello::TEST.to_s
124+
end
125+
end
126+
RUBY
127+
128+
add_to_env_config "production", <<-RUBY
129+
config.rake_eager_load = true
130+
RUBY
131+
132+
app_file "app/models/hello.rb", <<-RUBY
133+
class Hello
134+
TEST = 42
135+
end
136+
RUBY
137+
138+
output = Dir.chdir(app_path) { `bin/rails do_something RAILS_ENV=production` }
139+
assert_equal "Answer: 42\n", output
140+
end
141+
119142
def test_code_statistics_sanity
120143
assert_match "Code LOC: 26 Test LOC: 0 Code to Test Ratio: 1:0.0",
121144
Dir.chdir(app_path) { `bin/rails stats` }

0 commit comments

Comments
 (0)