From 262461781519009979ce14da313df610b52ccf5a Mon Sep 17 00:00:00 2001 From: Andrew Buntine Date: Sat, 14 Sep 2024 03:24:49 +1000 Subject: [PATCH] feat: Support model-specific table_name in migration generation (#46) * feat: Add support for table alias when injecting ONLY clause * chore: Bumped version to 0.15.1 * fix: Linting * chore: Don't assume table name when generating migrations * chore: Added test for generator with table_name specified in model --- lib/generators/hoardable/migration_generator.rb | 6 ++++++ test/test_migration_generator.rb | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/generators/hoardable/migration_generator.rb b/lib/generators/hoardable/migration_generator.rb index 8688189..6b2d39b 100644 --- a/lib/generators/hoardable/migration_generator.rb +++ b/lib/generators/hoardable/migration_generator.rb @@ -37,6 +37,12 @@ def create_triggers end no_tasks do + def table_name + class_name.singularize.constantize.table_name + rescue StandardError + super + end + def foreign_key_type options[:foreign_key_type] || class_name.singularize.constantize.columns.find { |col| col.name == primary_key }.sql_type diff --git a/test/test_migration_generator.rb b/test/test_migration_generator.rb index 7fd685a..23691e7 100644 --- a/test/test_migration_generator.rb +++ b/test/test_migration_generator.rb @@ -38,6 +38,11 @@ def shared_book_assertions(foreign_key_type = "uuid") shared_post_assertions end + it "supports using a model with a custom table_name" do + run_generator ["Hoardable::Post"] + shared_post_assertions + end + it "generates book migration with pluralized resource" do run_generator ["books"] shared_book_assertions