Skip to content

Commit

Permalink
Generate ApplicationRecord if it does not already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
maclover7 committed Feb 23, 2016
1 parent 063e816 commit 1813b29
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Expand Up @@ -22,11 +22,13 @@ def create_migration_file

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
Expand All @@ -37,6 +39,12 @@ def attributes_with_index
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
Expand Down
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
8 changes: 8 additions & 0 deletions railties/test/generators/model_generator_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 1813b29

Please sign in to comment.