Skip to content

Commit

Permalink
Do not update load_defaults version when running app:update
Browse files Browse the repository at this point in the history
Incompatible settings are included in the settings set by `load_defaults`.
So, I think that target version should be updated by a user when becomes
available, and should not be updated with `app:update`.
  • Loading branch information
y-yagi committed Feb 11, 2018
1 parent b91a4a0 commit 66b4752
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion railties/lib/rails/application/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Configuration < ::Rails::Engine::Configuration
:read_encrypted_secrets, :log_level, :content_security_policy_report_only,
:require_master_key

attr_reader :encoding, :api_only
attr_reader :encoding, :api_only, :loaded_config_version

def initialize(*)
super
Expand Down Expand Up @@ -58,6 +58,7 @@ def initialize(*)
@content_security_policy = nil
@content_security_policy_report_only = false
@require_master_key = false
@loaded_config_version = nil
end

def load_defaults(target_version)
Expand Down Expand Up @@ -118,6 +119,8 @@ def load_defaults(target_version)
else
raise "Unknown version #{target_version.to_s.inspect}"
end

@loaded_config_version = target_version
end

def encoding=(value)
Expand Down
6 changes: 6 additions & 0 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def config_when_updating
assets_config_exist = File.exist?("config/initializers/assets.rb")
csp_config_exist = File.exist?("config/initializers/content_security_policy.rb")

@config_target_version = Rails.application.config.loaded_config_version || "5.0"

config

unless cookie_serializer_config_exist
Expand Down Expand Up @@ -233,6 +235,10 @@ def tmp
def vendor
empty_directory_with_keep_file "vendor"
end

def config_target_version
defined?(@config_target_version) ? @config_target_version : Rails::VERSION::STRING.to_f
end
end

module Generators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Bundler.require(*Rails.groups)
module <%= app_const_base %>
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults <%= Rails::VERSION::STRING.to_f %>
config.load_defaults <%= build(:config_target_version) %>

# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
Expand Down
15 changes: 15 additions & 0 deletions railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def test_new_application_load_defaults
run_generator [app_root]
output = nil

assert_file "#{app_root}/config/application.rb", /\s+config\.load_defaults #{Rails::VERSION::STRING.to_f}/

Dir.chdir(app_root) do
output = `./bin/rails r "puts Rails.application.config.assets.unknown_asset_fallback"`
end
Expand Down Expand Up @@ -367,6 +369,19 @@ def test_app_update_does_not_generate_active_storage_contents_when_skip_active_r
end
end

def test_app_update_does_not_change_config_target_version
run_generator

FileUtils.cd(destination_root) do
config = "config/application.rb"
content = File.read(config)
File.write(config, content.gsub(/config\.load_defaults #{Rails::VERSION::STRING.to_f}/, "config.load_defaults 5.1"))
quietly { system("bin/rails app:update") }
end

assert_file "config/application.rb", /\s+config\.load_defaults 5\.1/
end

def test_application_names_are_not_singularized
run_generator [File.join(destination_root, "hats")]
assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/
Expand Down

0 comments on commit 66b4752

Please sign in to comment.