diff options
author | Jon Moss <me@jonathanmoss.me> | 2016-02-10 17:11:53 -0500 |
---|---|---|
committer | Jon Moss <me@jonathanmoss.me> | 2016-02-22 20:45:36 -0500 |
commit | 1813b29fc7632959800252f36e4b2e6ed4ac7266 (patch) | |
tree | 6ec6ea3e813be72f421716b70af8c0b09c7229c3 | |
parent | 063e81691933fd244fe04b362bc66b58250c0c64 (diff) | |
download | rails-1813b29fc7632959800252f36e4b2e6ed4ac7266.tar.gz rails-1813b29fc7632959800252f36e4b2e6ed4ac7266.tar.bz2 rails-1813b29fc7632959800252f36e4b2e6ed4ac7266.zip |
Generate ApplicationRecord if it does not already exist
3 files changed, 19 insertions, 0 deletions
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 7395839fca..09d3e7d9b9 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -22,11 +22,13 @@ module ActiveRecord def create_model_file template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") + generate_application_record end def create_module_file return if regular_class_path.empty? template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke + generate_application_record end hook_for :test_framework @@ -37,6 +39,12 @@ module ActiveRecord attributes.select { |a| !a.reference? && a.has_index? } end + def generate_application_record + if self.behavior == :invoke && !File.exist?('app/models/application_record.rb') + template 'application_record.rb', 'app/models/application_record.rb' + end + end + # Used by the migration template to determine the parent name of the model def parent_class_name options[:parent] || determine_default_parent_class diff --git a/activerecord/lib/rails/generators/active_record/model/templates/application_record.rb b/activerecord/lib/rails/generators/active_record/model/templates/application_record.rb new file mode 100644 index 0000000000..10a4cba84d --- /dev/null +++ b/activerecord/lib/rails/generators/active_record/model/templates/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true +end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index c8c8f0aa3b..ed6846abc3 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -6,6 +6,14 @@ 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) |