提交 75ccdfed 编写于 作者: L Lisa Ugray

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.
上级 9c35bf2a
* 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*
......
# 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
......@@ -23,13 +23,11 @@ def create_migration_file
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 @@ def attributes_with_index
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
# 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
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
......@@ -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 @@ def test_model_with_parent_option
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/
......
......@@ -3,6 +3,7 @@
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 @@ def test_api_scaffold_with_namespace_on_invoke
/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
......@@ -679,20 +679,6 @@ def test_model_with_existent_application_record_in_mountable_engine
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"
......
......@@ -124,7 +124,7 @@ def test_rails_generators_help_with_builtin_information
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册