Skip to content

Commit 1813b29

Browse files
committed
Generate ApplicationRecord if it does not already exist
1 parent 063e816 commit 1813b29

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

activerecord/lib/rails/generators/active_record/model/model_generator.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ def create_migration_file
2222

2323
def create_model_file
2424
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
25+
generate_application_record
2526
end
2627

2728
def create_module_file
2829
return if regular_class_path.empty?
2930
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
31+
generate_application_record
3032
end
3133

3234
hook_for :test_framework
@@ -37,6 +39,12 @@ def attributes_with_index
3739
attributes.select { |a| !a.reference? && a.has_index? }
3840
end
3941

42+
def generate_application_record
43+
if self.behavior == :invoke && !File.exist?('app/models/application_record.rb')
44+
template 'application_record.rb', 'app/models/application_record.rb'
45+
end
46+
end
47+
4048
# Used by the migration template to determine the parent name of the model
4149
def parent_class_name
4250
options[:parent] || determine_default_parent_class
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ApplicationRecord < ActiveRecord::Base
2+
self.abstract_class = true
3+
end

railties/test/generators/model_generator_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ class ModelGeneratorTest < Rails::Generators::TestCase
66
include GeneratorsTestHelper
77
arguments %w(Account name:string age:integer)
88

9+
def test_application_record_skeleton_is_created
10+
run_generator
11+
assert_file "app/models/application_record.rb" do |record|
12+
assert_match(/class ApplicationRecord < ActiveRecord::Base/, record)
13+
assert_match(/self.abstract_class = true/, record)
14+
end
15+
end
16+
917
def test_help_shows_invoked_generators_options
1018
content = run_generator ["--help"]
1119
assert_match(/ActiveRecord options:/, content)

0 commit comments

Comments
 (0)