Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #25319 from morgoth/generate-application-record-first
Browse files Browse the repository at this point in the history
Generate application_record.rb file before model file
  • Loading branch information
rafaelfranca committed Jun 13, 2016
2 parents 1efc81b + b991017 commit a973485
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
Expand Up @@ -21,8 +21,8 @@ def create_migration_file
end

def create_model_file
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
generate_application_record
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
end

def create_module_file
Expand All @@ -48,7 +48,7 @@ def generate_application_record

# Used by the migration template to determine the parent name of the model
def parent_class_name
options[:parent] || determine_default_parent_class
options[:parent] || "ApplicationRecord"
end

def application_record_exist?
Expand All @@ -64,14 +64,6 @@ def application_record_file_name
'app/models/application_record.rb'
end
end

def determine_default_parent_class
if application_record_exist?
"ApplicationRecord"
else
"ActiveRecord::Base"
end
end
end
end
end
14 changes: 7 additions & 7 deletions railties/test/generators/model_generator_test.rb
Expand Up @@ -34,7 +34,7 @@ def test_model_with_missing_attribute_type

def test_invokes_default_orm
run_generator
assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/
assert_file "app/models/account.rb", /class Account < ApplicationRecord/
end

def test_model_with_parent_option
Expand All @@ -56,7 +56,7 @@ def test_model_with_existent_application_record

def test_plural_names_are_singularized
content = run_generator ["accounts".freeze]
assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/
assert_file "app/models/account.rb", /class Account < ApplicationRecord/
assert_file "test/models/account_test.rb", /class AccountTest/
assert_match(/\[WARNING\] The model name 'accounts' was recognized as a plural, using the singular 'account' instead\. Override with --force-plural or setup custom inflection rules for this noun before running the generator\./, content)
end
Expand All @@ -71,7 +71,7 @@ def test_model_with_namespace
assert_file "app/models/admin.rb", /module Admin/
assert_file "app/models/admin.rb", /def self\.table_name_prefix/
assert_file "app/models/admin.rb", /'admin_'/
assert_file "app/models/admin/account.rb", /class Admin::Account < ActiveRecord::Base/
assert_file "app/models/admin/account.rb", /class Admin::Account < ApplicationRecord/
end

def test_migration
Expand Down Expand Up @@ -386,7 +386,7 @@ def test_required_belongs_to_adds_required_association
run_generator ["account", "supplier:references{required}"]

expected_file = <<-FILE.strip_heredoc
class Account < ActiveRecord::Base
class Account < ApplicationRecord
belongs_to :supplier, required: true
end
FILE
Expand All @@ -397,7 +397,7 @@ def test_required_polymorphic_belongs_to_generages_correct_model
run_generator ["account", "supplier:references{required,polymorphic}"]

expected_file = <<-FILE.strip_heredoc
class Account < ActiveRecord::Base
class Account < ApplicationRecord
belongs_to :supplier, polymorphic: true, required: true
end
FILE
Expand All @@ -408,7 +408,7 @@ def test_required_and_polymorphic_are_order_independent
run_generator ["account", "supplier:references{polymorphic.required}"]

expected_file = <<-FILE.strip_heredoc
class Account < ActiveRecord::Base
class Account < ApplicationRecord
belongs_to :supplier, polymorphic: true, required: true
end
FILE
Expand Down Expand Up @@ -459,7 +459,7 @@ def test_foreign_key_is_skipped_for_polymorphic_references
def test_token_option_adds_has_secure_token
run_generator ["user", "token:token", "auth_token:token"]
expected_file = <<-FILE.strip_heredoc
class User < ActiveRecord::Base
class User < ApplicationRecord
has_secure_token
has_secure_token :auth_token
end
Expand Down
12 changes: 6 additions & 6 deletions railties/test/generators/namespaced_generators_test.rb
Expand Up @@ -91,15 +91,15 @@ def test_module_file_is_not_created

def test_adds_namespace_to_model
run_generator
assert_file "app/models/test_app/account.rb", /module TestApp/, / class Account < ActiveRecord::Base/
assert_file "app/models/test_app/account.rb", /module TestApp/, / class Account < ApplicationRecord/
end

def test_model_with_namespace
run_generator ["admin/account"]
assert_file "app/models/test_app/admin.rb", /module TestApp/, /module Admin/
assert_file "app/models/test_app/admin.rb", /def self\.table_name_prefix/
assert_file "app/models/test_app/admin.rb", /'test_app_admin_'/
assert_file "app/models/test_app/admin/account.rb", /module TestApp/, /class Admin::Account < ActiveRecord::Base/
assert_file "app/models/test_app/admin/account.rb", /module TestApp/, /class Admin::Account < ApplicationRecord/
end

def test_migration
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_scaffold_on_invoke
run_generator

# Model
assert_file "app/models/test_app/product_line.rb", /module TestApp\n class ProductLine < ActiveRecord::Base/
assert_file "app/models/test_app/product_line.rb", /module TestApp\n class ProductLine < ApplicationRecord/
assert_file "test/models/test_app/product_line_test.rb", /module TestApp\n class ProductLineTest < ActiveSupport::TestCase/
assert_file "test/fixtures/test_app/product_lines.yml"
assert_migration "db/migrate/create_test_app_product_lines.rb"
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_scaffold_with_namespace_on_invoke

# Model
assert_file "app/models/test_app/admin.rb", /module TestApp\n module Admin/
assert_file "app/models/test_app/admin/role.rb", /module TestApp\n class Admin::Role < ActiveRecord::Base/
assert_file "app/models/test_app/admin/role.rb", /module TestApp\n class Admin::Role < ApplicationRecord/
assert_file "test/models/test_app/admin/role_test.rb", /module TestApp\n class Admin::RoleTest < ActiveSupport::TestCase/
assert_file "test/fixtures/test_app/admin/roles.yml"
assert_migration "db/migrate/create_test_app_admin_roles.rb"
Expand Down Expand Up @@ -336,7 +336,7 @@ def test_scaffold_with_nested_namespace_on_invoke

# Model
assert_file "app/models/test_app/admin/user/special.rb", /module TestApp\n module Admin/
assert_file "app/models/test_app/admin/user/special/role.rb", /module TestApp\n class Admin::User::Special::Role < ActiveRecord::Base/
assert_file "app/models/test_app/admin/user/special/role.rb", /module TestApp\n class Admin::User::Special::Role < ApplicationRecord/
assert_file "test/models/test_app/admin/user/special/role_test.rb", /module TestApp\n class Admin::User::Special::RoleTest < ActiveSupport::TestCase/
assert_file "test/fixtures/test_app/admin/user/special/roles.yml"
assert_migration "db/migrate/create_test_app_admin_user_special_roles.rb"
Expand Down Expand Up @@ -402,7 +402,7 @@ def test_api_scaffold_with_namespace_on_invoke

# Model
assert_file "app/models/test_app/admin.rb", /module TestApp\n module Admin/
assert_file "app/models/test_app/admin/role.rb", /module TestApp\n class Admin::Role < ActiveRecord::Base/
assert_file "app/models/test_app/admin/role.rb", /module TestApp\n class Admin::Role < ApplicationRecord/
assert_file "test/models/test_app/admin/role_test.rb", /module TestApp\n class Admin::RoleTest < ActiveSupport::TestCase/
assert_file "test/fixtures/test_app/admin/roles.yml"
assert_migration "db/migrate/create_test_app_admin_roles.rb"
Expand Down
4 changes: 2 additions & 2 deletions railties/test/generators/resource_generator_test.rb
Expand Up @@ -60,14 +60,14 @@ def test_resource_routes_are_added

def test_plural_names_are_singularized
content = run_generator ["accounts".freeze]
assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/
assert_file "app/models/account.rb", /class Account < ApplicationRecord/
assert_file "test/models/account_test.rb", /class AccountTest/
assert_match(/\[WARNING\] The model name 'accounts' was recognized as a plural, using the singular 'account' instead\. Override with --force-plural or setup custom inflection rules for this noun before running the generator\./, content)
end

def test_plural_names_can_be_forced
content = run_generator ["accounts", "--force-plural"]
assert_file "app/models/accounts.rb", /class Accounts < ActiveRecord::Base/
assert_file "app/models/accounts.rb", /class Accounts < ApplicationRecord/
assert_file "test/models/accounts_test.rb", /class AccountsTest/
assert_no_match(/\[WARNING\]/, content)
end
Expand Down
6 changes: 3 additions & 3 deletions railties/test/generators/scaffold_generator_test.rb
Expand Up @@ -11,7 +11,7 @@ def test_scaffold_on_invoke
run_generator

# Model
assert_file "app/models/product_line.rb", /class ProductLine < ActiveRecord::Base/
assert_file "app/models/product_line.rb", /class ProductLine < ApplicationRecord/
assert_file "test/models/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/
assert_file "test/fixtures/product_lines.yml"
assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/
Expand Down Expand Up @@ -91,7 +91,7 @@ def test_api_scaffold_on_invoke
run_generator %w(product_line title:string product:belongs_to user:references --api --no-template-engine --no-helper --no-assets)

# Model
assert_file "app/models/product_line.rb", /class ProductLine < ActiveRecord::Base/
assert_file "app/models/product_line.rb", /class ProductLine < ApplicationRecord/
assert_file "test/models/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/
assert_file "test/fixtures/product_lines.yml"
assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_scaffold_with_namespace_on_invoke

# Model
assert_file "app/models/admin.rb", /module Admin/
assert_file "app/models/admin/role.rb", /class Admin::Role < ActiveRecord::Base/
assert_file "app/models/admin/role.rb", /class Admin::Role < ApplicationRecord/
assert_file "test/models/admin/role_test.rb", /class Admin::RoleTest < ActiveSupport::TestCase/
assert_file "test/fixtures/admin/roles.yml"
assert_migration "db/migrate/create_admin_roles.rb"
Expand Down

0 comments on commit a973485

Please sign in to comment.