From 75ccdfed8d43d79f6590653212ecea7124759439 Mon Sep 17 00:00:00 2001 From: Lisa Ugray Date: Mon, 24 Jul 2017 14:17:56 -0400 Subject: Stop creating ApplicationRecord on model generation When generating models, we created ApplicationRecord in the default location if no file existed there. That was annoying for people who moved it to somewhere else in the autoload path. At this point, the vast majority of apps should have either run the upgrade script or generated a model since upgrading. For those that haven't the error message after generating a new model should be helpful: NameError: uninitialized constant ApplicationRecord To ease friction in that case, this also adds a generator for ApplicationRecord. --- .../application_record_generator.rb | 9 +++++++++ .../generators/application_record_generator_test.rb | 14 ++++++++++++++ railties/test/generators/model_generator_test.rb | 19 ------------------- .../test/generators/namespaced_generators_test.rb | 11 +++++++++++ railties/test/generators/plugin_generator_test.rb | 14 -------------- railties/test/generators_test.rb | 2 +- 6 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 railties/lib/rails/generators/rails/application_record/application_record_generator.rb create mode 100644 railties/test/generators/application_record_generator_test.rb (limited to 'railties') diff --git a/railties/lib/rails/generators/rails/application_record/application_record_generator.rb b/railties/lib/rails/generators/rails/application_record/application_record_generator.rb new file mode 100644 index 0000000000..f6b6e76b1d --- /dev/null +++ b/railties/lib/rails/generators/rails/application_record/application_record_generator.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Rails + module Generators + class ApplicationRecordGenerator < Base # :nodoc: + hook_for :orm, required: true, desc: "ORM to be invoked" + end + end +end diff --git a/railties/test/generators/application_record_generator_test.rb b/railties/test/generators/application_record_generator_test.rb new file mode 100644 index 0000000000..b734d786c0 --- /dev/null +++ b/railties/test/generators/application_record_generator_test.rb @@ -0,0 +1,14 @@ +require "generators/generators_test_helper" +require "rails/generators/rails/application_record/application_record_generator" + +class ApplicationRecordGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def test_application_record_skeleton_is_created + run_generator + assert_file "app/models/application_record.rb" do |record| + assert_match(/class ApplicationRecord < ActiveRecord::Base/, record) + assert_match(/self\.abstract_class = true/, record) + end + end +end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index f41969fc46..651713d3c0 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -6,14 +6,6 @@ class ModelGeneratorTest < Rails::Generators::TestCase include GeneratorsTestHelper arguments %w(Account name:string age:integer) - def test_application_record_skeleton_is_created - run_generator - assert_file "app/models/application_record.rb" do |record| - assert_match(/class ApplicationRecord < ActiveRecord::Base/, record) - assert_match(/self\.abstract_class = true/, record) - end - end - def test_help_shows_invoked_generators_options content = run_generator ["--help"] assert_match(/ActiveRecord options:/, content) @@ -43,17 +35,6 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_no_migration "db/migrate/create_accounts.rb" end - def test_model_with_existent_application_record - mkdir_p "#{destination_root}/app/models" - touch "#{destination_root}/app/models/application_record.rb" - - Dir.chdir(destination_root) do - run_generator ["account"] - end - - assert_file "app/models/account.rb", /class Account < ApplicationRecord/ - end - def test_plural_names_are_singularized content = run_generator ["accounts".freeze] assert_file "app/models/account.rb", /class Account < ApplicationRecord/ diff --git a/railties/test/generators/namespaced_generators_test.rb b/railties/test/generators/namespaced_generators_test.rb index 1caabbe6b1..9315a1b9da 100644 --- a/railties/test/generators/namespaced_generators_test.rb +++ b/railties/test/generators/namespaced_generators_test.rb @@ -3,6 +3,7 @@ require "rails/generators/rails/controller/controller_generator" require "rails/generators/rails/model/model_generator" require "rails/generators/mailer/mailer_generator" require "rails/generators/rails/scaffold/scaffold_generator" +require "rails/generators/rails/application_record/application_record_generator" class NamespacedGeneratorTestCase < Rails::Generators::TestCase include GeneratorsTestHelper @@ -421,3 +422,13 @@ class NamespacedScaffoldGeneratorTest < NamespacedGeneratorTestCase /module TestApp\n class Admin::RolesControllerTest < ActionDispatch::IntegrationTest/ end end + +class NamespacedApplicationRecordGeneratorTest < NamespacedGeneratorTestCase + include GeneratorsTestHelper + tests Rails::Generators::ApplicationRecordGenerator + + def test_adds_namespace_to_application_record + run_generator + assert_file "app/models/test_app/application_record.rb", /module TestApp/, / class ApplicationRecord < ActiveRecord::Base/ + end +end diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index f8512f9157..e8372dea47 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -679,20 +679,6 @@ class PluginGeneratorTest < Rails::Generators::TestCase assert_file "app/models/bukkits/article.rb", /class Article < ApplicationRecord/ end - def test_generate_application_record_when_does_not_exist_in_mountable_engine - run_generator [destination_root, "--mountable"] - FileUtils.rm "#{destination_root}/app/models/bukkits/application_record.rb" - capture(:stdout) do - `#{destination_root}/bin/rails g model article` - end - - assert_file "#{destination_root}/app/models/bukkits/application_record.rb" do |record| - assert_match(/module Bukkits/, record) - assert_match(/class ApplicationRecord < ActiveRecord::Base/, record) - assert_match(/self\.abstract_class = true/, record) - end - end - def test_generate_application_mailer_when_does_not_exist_in_mountable_engine run_generator [destination_root, "--mountable"] FileUtils.rm "#{destination_root}/app/mailers/bukkits/application_mailer.rb" diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index e07627f36d..5063e864ca 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -124,7 +124,7 @@ class GeneratorsTest < Rails::Generators::TestCase def test_rails_generators_help_does_not_include_app_nor_plugin_new output = capture(:stdout) { Rails::Generators.help } - assert_no_match(/app/, output) + assert_no_match(/app\W/, output) assert_no_match(/[^:]plugin/, output) end -- cgit v1.2.3