1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #25231 from prathamesh-sonpatki/collapse-new-initializers

Collapse all new default initializers into a single file
This commit is contained in:
David Heinemeier Hansson 2016-06-01 11:37:47 +02:00
commit 1221eb4f5e
10 changed files with 59 additions and 155 deletions

View file

@ -90,41 +90,26 @@ module Rails
def config_when_updating
cookie_serializer_config_exist = File.exist?('config/initializers/cookies_serializer.rb')
callback_terminator_config_exist = File.exist?('config/initializers/new_framework_defaults/callback_terminator.rb')
active_record_belongs_to_required_by_default_config_exist = File.exist?('config/initializers/new_framework_defaults/active_record_belongs_to_required_by_default.rb')
to_time_preserves_timezone_config_exist = File.exist?('config/initializers/new_framework_defaults/to_time_preserves_timezone.rb')
new_framework_defaults_config_exist = File.exist?('config/initializers/new_framework_defaults.rb')
action_cable_config_exist = File.exist?('config/cable.yml')
ssl_options_exist = File.exist?('config/initializers/new_framework_defaults/ssl_options.rb')
rack_cors_config_exist = File.exist?('config/initializers/cors.rb')
config
gsub_file 'config/environments/development.rb', /^(\s+)config\.file_watcher/, '\1# config.file_watcher'
unless callback_terminator_config_exist
remove_file 'config/initializers/new_framework_defaults/callback_terminator.rb'
end
unless cookie_serializer_config_exist
gsub_file 'config/initializers/cookies_serializer.rb', /json(?!,)/, 'marshal'
end
unless active_record_belongs_to_required_by_default_config_exist
remove_file 'config/initializers/new_framework_defaults/active_record_belongs_to_required_by_default.rb'
end
unless to_time_preserves_timezone_config_exist
remove_file 'config/initializers/new_framework_defaults/to_time_preserves_timezone.rb'
unless new_framework_defaults_config_exist
remove_file 'config/initializers/new_framework_defaults.rb'
end
unless action_cable_config_exist
template 'config/cable.yml'
end
unless ssl_options_exist
remove_file 'config/initializers/new_framework_defaults/ssl_options.rb'
end
unless rack_cors_config_exist
remove_file 'config/initializers/cors.rb'
end
@ -342,12 +327,6 @@ module Rails
end
end
def delete_active_record_initializers_skipping_active_record
if options[:skip_active_record]
remove_file 'config/initializers/new_framework_defaults/active_record_belongs_to_required_by_default.rb'
end
end
def delete_action_cable_files_skipping_action_cable
if options[:skip_action_cable]
remove_file 'config/cable.yml'
@ -360,8 +339,6 @@ module Rails
if options[:api]
remove_file 'config/initializers/session_store.rb'
remove_file 'config/initializers/cookies_serializer.rb'
remove_file 'config/initializers/new_framework_defaults/request_forgery_protection.rb'
remove_file 'config/initializers/new_framework_defaults/per_form_csrf_tokens.rb'
end
end

View file

@ -0,0 +1,37 @@
# Be sure to restart your server when you modify this file.
# This file contains all the new default configuration options from
# Rails 5.0.
<%- unless options[:skip_active_record] -%>
# Require `belongs_to` associations by default. This is a new Rails 5.0
# default, so it is introduced as a configuration option to ensure that apps
# made on earlier versions of Rails are not affected when upgrading.
Rails.application.config.active_record.belongs_to_required_by_default = true
<%- end -%>
# Do not halt callback chains when a callback returns false. This is a new
# Rails 5.0 default, so it is introduced as a configuration option to ensure
# that apps made with earlier versions of Rails are not affected when upgrading.
ActiveSupport.halt_callback_chains_on_return_false = false
<%- unless options[:api] -%>
# Enable per-form CSRF tokens.
Rails.application.config.action_controller.per_form_csrf_tokens = true
# Enable origin-checking CSRF mitigation.
Rails.application.config.action_controller.forgery_protection_origin_check = true
<%- end -%>
# Configure SSL options to enable HSTS with subdomains. This is a new
# Rails 5.0 default, so it is introduced as a configuration option to ensure
# that apps made on earlier versions of Rails are not affected when upgrading.
Rails.application.config.ssl_options = { hsts: { subdomains: true } }
# Preserve the timezone of the receiver when calling to `to_time`.
# Ruby 2.4 will change the behavior of `to_time` to preserve the timezone
# when converting to an instance of `Time` instead of the previous behavior
# of converting to the local system timezone.
#
# Rails 5.0 introduced this config option so that apps made with earlier
# versions of Rails are not affected when upgrading.
ActiveSupport.to_time_preserves_timezone = true

View file

@ -1,6 +0,0 @@
# Be sure to restart your server when you modify this file.
# Require `belongs_to` associations by default. This is a new Rails 5.0
# default, so it is introduced as a configuration option to ensure that apps
# made on earlier versions of Rails are not affected when upgrading.
Rails.application.config.active_record.belongs_to_required_by_default = true

View file

@ -1,6 +0,0 @@
# Be sure to restart your server when you modify this file.
# Do not halt callback chains when a callback returns false. This is a new
# Rails 5.0 default, so it is introduced as a configuration option to ensure
# that apps made with earlier versions of Rails are not affected when upgrading.
ActiveSupport.halt_callback_chains_on_return_false = false

View file

@ -1,4 +0,0 @@
# Be sure to restart your server when you modify this file.
# Enable per-form CSRF tokens.
Rails.application.config.action_controller.per_form_csrf_tokens = true

View file

@ -1,4 +0,0 @@
# Be sure to restart your server when you modify this file.
# Enable origin-checking CSRF mitigation.
Rails.application.config.action_controller.forgery_protection_origin_check = true

View file

@ -1,6 +0,0 @@
# Be sure to restart your server when you modify this file.
# Configure SSL options to enable HSTS with subdomains. This is a new
# Rails 5.0 default, so it is introduced as a configuration option to ensure
# that apps made on earlier versions of Rails are not affected when upgrading.
Rails.application.config.ssl_options = { hsts: { subdomains: true } }

View file

@ -1,10 +0,0 @@
# Be sure to restart your server when you modify this file.
# Preserve the timezone of the receiver when calling to `to_time`.
# Ruby 2.4 will change the behavior of `to_time` to preserve the timezone
# when converting to an instance of `Time` instead of the previous behavior
# of converting to the local system timezone.
#
# Rails 5.0 introduced this config option so that apps made with earlier
# versions of Rails are not affected when upgrading.
ActiveSupport.to_time_preserves_timezone = true

View file

@ -62,6 +62,15 @@ class ApiAppGeneratorTest < Rails::Generators::TestCase
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
@ -100,8 +109,6 @@ class ApiAppGeneratorTest < Rails::Generators::TestCase
config/initializers/assets.rb
config/initializers/cookies_serializer.rb
config/initializers/session_store.rb
config/initializers/new_framework_defaults/request_forgery_protection.rb
config/initializers/new_framework_defaults/per_form_csrf_tokens.rb
lib/assets
vendor/assets
test/helpers

View file

@ -172,34 +172,6 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
def test_rails_update_does_not_create_callback_terminator_initializer
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults/callback_terminator.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell
generator.send(:app_const)
quietly { generator.send(:update_config_files) }
assert_no_file "#{app_root}/config/initializers/new_framework_defaults/callback_terminator.rb"
end
end
def test_rails_update_does_not_remove_callback_terminator_initializer_if_already_present
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.touch("#{app_root}/config/initializers/new_framework_defaults/callback_terminator.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], 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/callback_terminator.rb"
end
end
def test_rails_update_set_the_cookie_serializer_to_marshal_if_it_is_not_already_configured
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
@ -229,87 +201,31 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
def test_rails_update_does_not_create_active_record_belongs_to_required_by_default
def test_rails_update_does_not_create_new_framework_defaults_by_default
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults/active_record_belongs_to_required_by_default.rb")
FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell
generator.send(:app_const)
quietly { generator.send(:update_config_files) }
assert_no_file "#{app_root}/config/initializers/new_framework_defaults/active_record_belongs_to_required_by_default.rb"
assert_no_file "#{app_root}/config/initializers/new_framework_defaults.rb"
end
end
def test_rails_update_does_not_remove_active_record_belongs_to_required_by_default_if_already_present
def test_rails_update_does_not_new_framework_defaults_if_already_present
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.touch("#{app_root}/config/initializers/new_framework_defaults/active_record_belongs_to_required_by_default.rb")
FileUtils.touch("#{app_root}/config/initializers/new_framework_defaults.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], 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/active_record_belongs_to_required_by_default.rb"
end
end
def test_rails_update_does_not_create_to_time_preserves_timezone
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults/to_time_preserves_timezone.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell
generator.send(:app_const)
quietly { generator.send(:update_config_files) }
assert_no_file "#{app_root}/config/initializers/new_framework_defaults/to_time_preserves_timezone.rb"
end
end
def test_rails_update_does_not_remove_to_time_preserves_timezone_if_already_present
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.touch("#{app_root}/config/initializers/new_framework_defaults/to_time_preserves_timezone.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], 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/to_time_preserves_timezone.rb"
end
end
def test_rails_update_does_not_create_ssl_options_by_default
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults/ssl_options.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell
generator.send(:app_const)
quietly { generator.send(:update_config_files) }
assert_no_file "#{app_root}/config/initializers/new_framework_defaults/ssl_options.rb"
end
end
def test_rails_update_does_not_remove_ssl_options_if_already_present
app_root = File.join(destination_root, 'myapp')
run_generator [app_root]
FileUtils.touch("#{app_root}/config/initializers/new_framework_defaults/ssl_options.rb")
stub_rails_application(app_root) do
generator = Rails::Generators::AppGenerator.new ["rails"], [], 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/ssl_options.rb"
assert_file "#{app_root}/config/initializers/new_framework_defaults.rb"
end
end
@ -452,12 +368,15 @@ class AppGeneratorTest < Rails::Generators::TestCase
def test_generator_if_skip_active_record_is_given
run_generator [destination_root, "--skip-active-record"]
assert_no_file "config/database.yml"
assert_no_file "config/initializers/new_framework_defaults/active_record_belongs_to_required_by_default.rb"
assert_no_file "app/models/application_record.rb"
assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
assert_file "test/test_helper.rb" do |helper_content|
assert_no_match(/fixtures :all/, helper_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