Merge pull request #28209 from tjoyal/railties/add-config-rake_eager_load

[Railties] Add config rake_eager_load
This commit is contained in:
Rafael Mendonça França 2019-12-17 22:10:14 -03:00
commit ed5f2c6192
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
6 changed files with 52 additions and 3 deletions

View File

@ -137,6 +137,8 @@ defaults to `:debug` for all environments. The available log levels are: `:debug
* `config.middleware` allows you to configure the application's middleware. This is covered in depth in the [Configuring Middleware](#configuring-middleware) section below.
* `config.rake_eager_load` when `true`, eager load the application when running Rake tasks. Defaults to `false`.
* `config.reload_classes_only_on_change` enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to `true`. If `config.cache_classes` is `true`, this option is ignored.
* `config.credentials.content_path` configures lookup path for encrypted credentials.

View File

@ -1,3 +1,11 @@
* Allow configuration of eager_load behaviour for rake environment:
`config.rake_eager_load`
Defaults to `false` as per previous behaviour.
*Thierry Joyal*
* Ensure Rails migration generator respects system-wide primary key config
When rails is configured to use a specific primary key type:

View File

@ -505,7 +505,7 @@ module Rails
super
require "rails/tasks"
task :environment do
ActiveSupport.on_load(:before_initialize) { config.eager_load = false }
ActiveSupport.on_load(:before_initialize) { config.eager_load = config.rake_eager_load }
require_environment!
end

View File

@ -19,7 +19,8 @@ module Rails
:beginning_of_week, :filter_redirect, :x, :enable_dependency_loading,
:read_encrypted_secrets, :log_level, :content_security_policy_report_only,
:content_security_policy_nonce_generator, :content_security_policy_nonce_directives,
:require_master_key, :credentials, :disable_sandbox, :add_autoload_paths_to_load_path
:require_master_key, :credentials, :disable_sandbox, :add_autoload_paths_to_load_path,
:rake_eager_load
attr_reader :encoding, :api_only, :loaded_config_version, :autoloader
@ -70,6 +71,7 @@ module Rails
@disable_sandbox = false
@add_autoload_paths_to_load_path = true
@feature_policy = nil
@rake_eager_load = false
end
# Loads default configurations. See {the result of the method for each version}[https://guides.rubyonrails.org/configuring.html#results-of-config-load-defaults].

View File

@ -2487,6 +2487,20 @@ module ApplicationTests
assert Rails.configuration.disable_sandbox
end
test "rake_eager_load is false by default" do
app "development"
assert_equal false, Rails.application.config.rake_eager_load
end
test "rake_eager_load is set correctly" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.rake_eager_load = true
RUBY
assert_equal true, Rails.application.config.rake_eager_load
end
private
def force_lazy_load_hooks
yield # Tasty clarifying sugar, homie! We only need to reference a constant to load it.

View File

@ -96,7 +96,7 @@ module ApplicationTests
assert_match "Hello world", output
end
def test_should_not_eager_load_model_for_rake
def test_should_not_eager_load_model_for_rake_when_rake_eager_load_is_false
add_to_config <<-RUBY
rake_tasks do
task do_nothing: :environment do
@ -117,6 +117,29 @@ module ApplicationTests
assert_match "There is nothing", output
end
def test_should_eager_load_model_for_rake_when_rake_eager_load_is_true
add_to_config <<-RUBY
rake_tasks do
task do_something: :environment do
puts "Answer: " + Hello::TEST.to_s
end
end
RUBY
add_to_env_config "production", <<-RUBY
config.rake_eager_load = true
RUBY
app_file "app/models/hello.rb", <<-RUBY
class Hello
TEST = 42
end
RUBY
output = Dir.chdir(app_path) { `bin/rails do_something RAILS_ENV=production` }
assert_equal "Answer: 42\n", output
end
def test_code_statistics_sanity
assert_match "Code LOC: 32 Test LOC: 0 Code to Test Ratio: 1:0.0",
rails("stats")