aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLisa Ugray <lisa.ugray@shopify.com>2017-07-24 14:17:56 -0400
committerLisa Ugray <lisa.ugray@shopify.com>2017-07-24 21:22:48 -0400
commit75ccdfed8d43d79f6590653212ecea7124759439 (patch)
tree3f0917b8813a7f6e05dd732306a759911d3ce4d2
parent9c35bf2a6a27431c6aa283db781c19f61c5155be (diff)
downloadrails-75ccdfed8d43d79f6590653212ecea7124759439.tar.gz
rails-75ccdfed8d43d79f6590653212ecea7124759439.tar.bz2
rails-75ccdfed8d43d79f6590653212ecea7124759439.zip
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.
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/rails/generators/active_record/application_record/application_record_generator.rb26
-rw-r--r--activerecord/lib/rails/generators/active_record/application_record/templates/application_record.rb (renamed from activerecord/lib/rails/generators/active_record/model/templates/application_record.rb)0
-rw-r--r--activerecord/lib/rails/generators/active_record/model/model_generator.rb23
-rw-r--r--railties/lib/rails/generators/rails/application_record/application_record_generator.rb9
-rw-r--r--railties/test/generators/application_record_generator_test.rb14
-rw-r--r--railties/test/generators/model_generator_test.rb19
-rw-r--r--railties/test/generators/namespaced_generators_test.rb11
-rw-r--r--railties/test/generators/plugin_generator_test.rb14
-rw-r--r--railties/test/generators_test.rb2
10 files changed, 66 insertions, 57 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index afc459ef68..a656c767c0 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* ApplicationRecord is no longer generated when generating models. If you
+ need to generate it, it can be created with `rails g application_record`.
+
+ *Lisa Ugray*
+
* Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT` to keep the existing select list.
*Ryuta Kamizono*
diff --git a/activerecord/lib/rails/generators/active_record/application_record/application_record_generator.rb b/activerecord/lib/rails/generators/active_record/application_record/application_record_generator.rb
new file mode 100644
index 0000000000..d18330f5b2
--- /dev/null
+++ b/activerecord/lib/rails/generators/active_record/application_record/application_record_generator.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+require "rails/generators/active_record"
+
+module ActiveRecord
+ module Generators # :nodoc:
+ class ApplicationRecordGenerator < ::Rails::Generators::Base # :nodoc:
+ source_root File.expand_path("templates", __dir__)
+
+ # FIXME: Change this file to a symlink once RubyGems 2.5.0 is required.
+ def create_application_record
+ template "application_record.rb", application_record_file_name
+ end
+
+ private
+
+ def application_record_file_name
+ @application_record_file_name ||= if namespaced?
+ "app/models/#{namespaced_path}/application_record.rb"
+ else
+ "app/models/application_record.rb"
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/rails/generators/active_record/model/templates/application_record.rb b/activerecord/lib/rails/generators/active_record/application_record/templates/application_record.rb
index 60050e0bf8..60050e0bf8 100644
--- a/activerecord/lib/rails/generators/active_record/model/templates/application_record.rb
+++ b/activerecord/lib/rails/generators/active_record/application_record/templates/application_record.rb
diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb
index 7a1bb7efdc..25e54f3ac8 100644
--- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb
+++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb
@@ -23,13 +23,11 @@ module ActiveRecord
end
def create_model_file
- generate_application_record
template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
end
def create_module_file
return if regular_class_path.empty?
- generate_application_record
template "module.rb", File.join("app/models", "#{class_path.join('/')}.rb") if behavior == :invoke
end
@@ -41,31 +39,10 @@ module ActiveRecord
attributes.select { |a| !a.reference? && a.has_index? }
end
- # FIXME: Change this file to a symlink once RubyGems 2.5.0 is required.
- def generate_application_record
- if behavior == :invoke && !application_record_exist?
- template "application_record.rb", application_record_file_name
- end
- end
-
# Used by the migration template to determine the parent name of the model
def parent_class_name
options[:parent] || "ApplicationRecord"
end
-
- def application_record_exist?
- file_exist = nil
- in_root { file_exist = File.exist?(application_record_file_name) }
- file_exist
- end
-
- def application_record_file_name
- @application_record_file_name ||= if mountable_engine?
- "app/models/#{namespaced_path}/application_record.rb"
- else
- "app/models/application_record.rb"
- end
- end
end
end
end
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