Skip to content

Commit

Permalink
feat: Support model-specific table_name in migration generation (#46)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
buntine authored Sep 13, 2024
1 parent d20c9d1 commit 2624617
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/generators/hoardable/migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/test_migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2624617

Please sign in to comment.