Skip to content

Commit

Permalink
Merge pull request #29916 from lugray/stop_generating_application_record
Browse files Browse the repository at this point in the history
Stop creating ApplicationRecord on model generation
  • Loading branch information
eileencodes committed Jul 25, 2017
2 parents 1d3b6d2 + 75ccdfe commit ec21b97
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 57 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions railties/test/generators/application_record_generator_test.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 0 additions & 19 deletions railties/test/generators/model_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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/
Expand Down
11 changes: 11 additions & 0 deletions railties/test/generators/namespaced_generators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
14 changes: 0 additions & 14 deletions railties/test/generators/plugin_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion railties/test/generators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ec21b97

Please sign in to comment.