Skip to content

Commit

Permalink
The abstract parent class file generated via generator should not be …
Browse files Browse the repository at this point in the history
…pluralized

Currently, the file generated via the generator is pluralized
but the parent class is singluar.

example: bundle exec rails g scaffold Pet name:string --database=animals

The above command should generate: apps/models/animals_record.rb

but the pets model would inherit from: `AnimalRecord` as
`"animals".classify` would be `Animal`

This will throw the `uninitialized constant AnimalRecord Did you mean? AnimalsRecord`
error.
  • Loading branch information
abhaynikam committed Aug 2, 2020
1 parent 843898c commit b9879bb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
Expand Up @@ -66,7 +66,7 @@ def generate_abstract_class
end

def abstract_class_name
"#{database.classify}Record"
"#{database.camelize}Record"
end

def database
Expand Down
8 changes: 4 additions & 4 deletions railties/test/generators/generators_test_helper.rb
Expand Up @@ -48,13 +48,13 @@ def self.included(base)
end
end

def with_secondary_database_configuration
def with_database_configuration(database_name = "secondary")
original_configurations = ActiveRecord::Base.configurations
ActiveRecord::Base.configurations = {
test: {
secondary: {
database: "db/secondary.sqlite3",
migrations_paths: "db/secondary_migrate",
"#{database_name}": {
database: "db/#{database_name}.sqlite3",
migrations_paths: "db/#{database_name}_migrate",
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions railties/test/generators/migration_generator_test.rb
Expand Up @@ -317,7 +317,7 @@ def test_add_migration_with_references_options_when_primary_key_uuid
end

def test_database_puts_migrations_in_configured_folder
with_secondary_database_configuration do
with_database_configuration do
run_generator ["create_books", "--database=secondary"]
assert_migration "db/secondary_migrate/create_books.rb" do |content|
assert_method :change, content do |change|
Expand All @@ -328,7 +328,7 @@ def test_database_puts_migrations_in_configured_folder
end

def test_database_puts_migrations_in_configured_folder_with_aliases
with_secondary_database_configuration do
with_database_configuration do
run_generator ["create_books", "--db=secondary"]
assert_migration "db/secondary_migrate/create_books.rb" do |content|
assert_method :change, content do |change|
Expand Down
21 changes: 15 additions & 6 deletions railties/test/generators/model_generator_test.rb
Expand Up @@ -50,7 +50,7 @@ def test_model_with_parent_option
end

def test_model_with_database_option
with_secondary_database_configuration do
with_database_configuration do
run_generator ["account", "--database", "secondary"]
assert_file "app/models/secondary_record.rb", /class SecondaryRecord < ApplicationRecord/
assert_file "app/models/account.rb", /class Account < SecondaryRecord/
Expand All @@ -59,15 +59,15 @@ def test_model_with_database_option
end

def test_model_with_parent_and_database_option
with_secondary_database_configuration do
with_database_configuration do
run_generator ["account", "--parent", "Admin::Account", "--database", "secondary"]
assert_file "app/models/account.rb", /class Account < Admin::Account/
assert_migration "db/secondary_migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/
end
end

def test_model_with_no_migration_and_database_option
with_secondary_database_configuration do
with_database_configuration do
run_generator ["account", "--migration", "false", "--database", "secondary"]
assert_file "app/models/account.rb", /class Account < SecondaryRecord/
assert_no_migration "db/secondary_migrate/create_accounts.rb"
Expand All @@ -81,13 +81,22 @@ def test_model_with_no_migration_option
end

def test_model_with_parent_option_database_option_and_no_migration_option
with_secondary_database_configuration do
with_database_configuration do
run_generator ["account", "--migration", "false", "--database", "secondary", "--migration", "false", "--parent", "Admin::Account"]
assert_file "app/models/account.rb", /class Account < Admin::Account/
assert_no_migration "db/secondary_migrate/create_accounts.rb"
end
end

def test_model_with_underscored_database_option
with_database_configuration("admin_accounts") do
run_generator ["account", "--database", "admin_accounts"]
assert_file "app/models/admin_accounts_record.rb", /class AdminAccountsRecord < ApplicationRecord/
assert_file "app/models/account.rb", /class Account < AdminAccountsRecord/
assert_migration "db/admin_accounts_migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/
end
end

def test_plural_names_are_singularized
content = run_generator ["accounts"]
assert_file "app/models/account.rb", /class Account < ApplicationRecord/
Expand Down Expand Up @@ -440,7 +449,7 @@ def test_add_uuid_to_create_table_migration
end

def test_database_puts_migrations_in_configured_folder
with_secondary_database_configuration do
with_database_configuration do
run_generator ["account", "--database=secondary"]
assert_migration "db/secondary_migrate/create_accounts.rb" do |content|
assert_method :change, content do |change|
Expand All @@ -451,7 +460,7 @@ def test_database_puts_migrations_in_configured_folder
end

def test_database_puts_migrations_in_configured_folder_with_aliases
with_secondary_database_configuration do
with_database_configuration do
run_generator ["account", "--db=secondary"]
assert_migration "db/secondary_migrate/create_accounts.rb" do |content|
assert_method :change, content do |change|
Expand Down
4 changes: 2 additions & 2 deletions railties/test/generators/scaffold_generator_test.rb
Expand Up @@ -520,7 +520,7 @@ def test_scaffold_generator_rich_text
end

def test_scaffold_generator_multi_db_abstract_class
with_secondary_database_configuration do
with_database_configuration do
run_generator ["posts", "--database=secondary"]

assert_migration "db/secondary_migrate/create_posts.rb"
Expand All @@ -533,7 +533,7 @@ def test_scaffold_generator_multi_db_abstract_class
end

def test_scaffold_generator_database_with_aliases
with_secondary_database_configuration do
with_database_configuration do
run_generator ["posts", "--db=secondary"]

assert_migration "db/secondary_migrate/create_posts.rb"
Expand Down

0 comments on commit b9879bb

Please sign in to comment.