Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track the version-compatible config settings inside railties #28469

Merged
merged 1 commit into from Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions railties/lib/rails/application/configuration.rb
Expand Up @@ -55,6 +55,34 @@ def initialize(*)
@read_encrypted_secrets = false
end

def load_defaults(target_version)
case target_version.to_s
when "5.0"
if defined?(action_controller)
action_controller.per_form_csrf_tokens = true
action_controller.forgery_protection_origin_check = true
end

ActiveSupport.to_time_preserves_timezone = true

if defined?(active_record)
active_record.belongs_to_required_by_default = true
end

self.ssl_options = { hsts: { subdomains: true } }

when "5.1"
load_defaults "5.0"

if defined?(assets)
assets.unknown_asset_fallback = false
end

else
raise "Unknown version #{target_version.to_s.inspect}"
end
end

def encoding=(value)
@encoding = value
silence_warnings do
Expand Down
8 changes: 8 additions & 0 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -371,6 +371,14 @@ def delete_api_initializers
end
end

def delete_new_framework_defaults
# Sprockets owns the only new default for 5.1: if it's disabled,
# we don't want the file.
unless options[:update] && !options[:skip_sprockets]
remove_file "config/initializers/new_framework_defaults_5_1.rb"
end
end

def delete_bin_yarn_if_skip_yarn_option
remove_file "bin/yarn" if options[:skip_yarn]
end
Expand Down
Expand Up @@ -22,6 +22,9 @@

module <%= app_const_base %>
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults <%= Rails::VERSION::STRING.to_f %>

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down

This file was deleted.

@@ -0,0 +1,13 @@
# Be sure to restart your server when you modify this file.
#
# This file contains migration options to ease your Rails 5.1 upgrade.
#
# Once upgraded flip defaults one by one to migrate to the new default.
#
# Read the Guide for Upgrading Ruby on Rails for more info on each option.
<%- unless options[:skip_sprockets] -%>

# Unknown asset fallback will return the path passed in when the given
# asset is not present in the asset pipeline.
# Rails.application.config.assets.unknown_asset_fallback = false
<%- end -%>
9 changes: 0 additions & 9 deletions railties/test/generators/api_app_generator_test.rb
Expand Up @@ -61,15 +61,6 @@ def test_generator_if_skip_action_cable_is_given
end
end

def test_generator_skips_per_form_csrf_token_and_origin_check_configs_for_api_apps
run_generator

assert_file "config/initializers/new_framework_defaults.rb" do |initializer_content|
assert_no_match(/per_form_csrf_tokens/, initializer_content)
assert_no_match(/forgery_protection_origin_check/, initializer_content)
end
end

private

def default_files
Expand Down
21 changes: 8 additions & 13 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -156,6 +156,10 @@ def test_new_application_not_include_api_initializers
assert_no_file "config/initializers/cors.rb"
end

def test_new_application_doesnt_need_defaults
assert_no_file "config/initializers/new_framework_defaults_5_1.rb"
end

def test_app_update_keep_the_cookie_serializer_if_it_is_already_configured
app_root = File.join(destination_root, "myapp")
run_generator [app_root]
Expand Down Expand Up @@ -197,21 +201,18 @@ def test_app_update_dont_set_file_watcher
end
end

def test_app_update_does_not_create_new_framework_defaults_by_default
def test_app_update_create_new_framework_defaults
app_root = File.join(destination_root, "myapp")
run_generator [app_root]

FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults.rb")
assert_no_file "#{app_root}/config/initializers/new_framework_defaults_5_1.rb"

stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], { update: true }, destination_root: app_root, shell: @shell
generator.send(:app_const)
quietly { generator.send(:update_config_files) }

assert_file "#{app_root}/config/initializers/new_framework_defaults.rb" do |content|
assert_match(/Rails\.application\.config.active_record\.belongs_to_required_by_default = false/, content)
assert_no_match(/Rails\.application\.config\.ssl_options/, content)
end
assert_file "#{app_root}/config/initializers/new_framework_defaults_5_1.rb"
end
end

Expand Down Expand Up @@ -367,10 +368,6 @@ def test_generator_if_skip_active_record_is_given
assert_file "bin/update" do |update_content|
assert_no_match(/db:migrate/, update_content)
end

assert_file "config/initializers/new_framework_defaults.rb" do |initializer_content|
assert_no_match(/belongs_to_required_by_default/, initializer_content)
end
end

def test_generator_if_skip_action_mailer_is_given
Expand Down Expand Up @@ -415,9 +412,7 @@ def test_generator_if_skip_sprockets_is_given
assert_no_match(/config\.assets\.js_compressor = :uglifier/, content)
assert_no_match(/config\.assets\.css_compressor = :sass/, content)
end
assert_file "config/initializers/new_framework_defaults.rb" do |content|
assert_no_match(/unknown_asset_fallback/, content)
end
assert_no_file "config/initializers/new_framework_defaults_5_1.rb"
end

def test_generator_if_skip_yarn_is_given
Expand Down