diff options
5 files changed, 44 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb index ca4385b7c2..1e697e1ba5 100644 --- a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb +++ b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb @@ -55,10 +55,6 @@ module ActiveSupport private def setup_autoloaders - Rails.autoloaders.each do |autoloader| - autoloader.inflector = Inflector - end - Dependencies.autoload_paths.each do |autoload_path| # Zeitwerk only accepts existing directories in `push_dir` to # prevent misconfigurations. diff --git a/railties/lib/rails/autoloaders.rb b/railties/lib/rails/autoloaders.rb index a6974cc207..1bd3f18a74 100644 --- a/railties/lib/rails/autoloaders.rb +++ b/railties/lib/rails/autoloaders.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_support/dependencies/zeitwerk_integration" + module Rails module Autoloaders # :nodoc: class << self @@ -7,13 +9,19 @@ module Rails def main if zeitwerk_enabled? - @main ||= Zeitwerk::Loader.new.tap { |loader| loader.tag = "rails.main" } + @main ||= Zeitwerk::Loader.new.tap do |loader| + loader.tag = "rails.main" + loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector + end end end def once if zeitwerk_enabled? - @once ||= Zeitwerk::Loader.new.tap { |loader| loader.tag = "rails.once" } + @once ||= Zeitwerk::Loader.new.tap do |loader| + loader.tag = "rails.once" + loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector + end end end diff --git a/railties/lib/rails/generators/rails/db/system/change/change_generator.rb b/railties/lib/rails/generators/rails/db/system/change/change_generator.rb index 63849eb18d..24db92fad7 100644 --- a/railties/lib/rails/generators/rails/db/system/change/change_generator.rb +++ b/railties/lib/rails/generators/rails/db/system/change/change_generator.rb @@ -35,8 +35,9 @@ module Rails end def edit_gemfile - database_gem_name, _ = gem_for_database - gsub_file("Gemfile", all_database_gems_regex, database_gem_name) + name, version = gem_for_database + gsub_file("Gemfile", all_database_gems_regex, name) + gsub_file("Gemfile", gem_entry_regex_for(name), gem_entry_for(name, *version)) end private @@ -48,6 +49,15 @@ module Rails all_database_gem_names = all_database_gems.map(&:first) /(\b#{all_database_gem_names.join('\b|\b')}\b)/ end + + def gem_entry_regex_for(gem_name) + /^gem.*\b#{gem_name}\b.*/ + end + + def gem_entry_for(*gem_name_and_version) + gem_name_and_version.map! { |segment| "'#{segment}'" } + "gem #{gem_name_and_version.join(", ")}" + end end end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 73773602a3..9ba5517afd 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1167,6 +1167,8 @@ module ApplicationTests assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once assert_equal "rails.once", Rails.autoloaders.once.tag assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a + assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.main.inflector + assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.once.inflector config.autoloader = :classic assert_not Rails.autoloaders.zeitwerk_enabled? @@ -1181,6 +1183,8 @@ module ApplicationTests assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once assert_equal "rails.once", Rails.autoloaders.once.tag assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a + assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.main.inflector + assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.once.inflector assert_raises(ArgumentError) { config.autoloader = :unknown } end diff --git a/railties/test/generators/db_system_change_generator_test.rb b/railties/test/generators/db_system_change_generator_test.rb index d476bfd2dc..d3d27b616a 100644 --- a/railties/test/generators/db_system_change_generator_test.rb +++ b/railties/test/generators/db_system_change_generator_test.rb @@ -40,7 +40,7 @@ module Rails assert_file("Gemfile") do |content| assert_match "# Use pg as the database for Active Record", content - assert_match "gem 'pg'", content + assert_match "gem 'pg', '>= 0.18', '< 2.0'", content end end @@ -54,7 +54,7 @@ module Rails assert_file("Gemfile") do |content| assert_match "# Use mysql2 as the database for Active Record", content - assert_match "gem 'mysql2'", content + assert_match "gem 'mysql2', '>= 0.4.4'", content end end @@ -68,7 +68,22 @@ module Rails assert_file("Gemfile") do |content| assert_match "# Use sqlite3 as the database for Active Record", content - assert_match "gem 'sqlite3'", content + assert_match "gem 'sqlite3', '~> 1.3', '>= 1.3.6'", content + end + end + + test "change from versioned gem to other versioned gem" do + run_generator ["--to", "sqlite3"] + run_generator ["--to", "mysql", "--force"] + + assert_file("config/database.yml") do |content| + assert_match "adapter: mysql2", content + assert_match "database: test_app", content + end + + assert_file("Gemfile") do |content| + assert_match "# Use mysql2 as the database for Active Record", content + assert_match "gem 'mysql2', '>= 0.4.4'", content end end end |