From 35d87f63ccc1556b667e93d658a704f2a14b7cf2 Mon Sep 17 00:00:00 2001 From: Yaw Boakye Date: Thu, 6 Jun 2024 09:43:58 +0100 Subject: [PATCH] copy-pasta rails migration template here verbatim well, with only one rename of `table_name` to `domain_prefixed_relation_name`. otherwise, no changes. --- lib/generators/categoria/model_generator.rb | 10 +++++-- .../templates/model_migration.rb.erb | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/generators/categoria/model_generator.rb b/lib/generators/categoria/model_generator.rb index 4b89a0f..4beaaed 100644 --- a/lib/generators/categoria/model_generator.rb +++ b/lib/generators/categoria/model_generator.rb @@ -21,6 +21,9 @@ module Generators # security-by-obscurity. # - models are not in `app/models` but instead internal to domain. class ModelGenerator < ::Rails::Generators::NamedBase + desc <<~DOC.squish + DOC + include ::Rails::Generators::ModelHelpers include ::Rails::Generators::Migration include ::ActiveRecord::Generators::Migration @@ -37,7 +40,7 @@ class ModelGenerator < ::Rails::Generators::NamedBase def create_migration_file migration_template( "model_migration.rb.erb", - File.join(db_migrate_path, "create_#{relation_name}_table.rb"), + File.join(db_migrate_path, "create_#{domain_prefixed_relation_name}_table.rb"), migration_version: ) end @@ -77,7 +80,10 @@ def internal_model_name end sig { returns(String) } - def relation_name = %(#{domain_name}_#{internal_model_name.pluralize}) + def domain_prefixed_relation_name = %(#{domain_name}_#{internal_model_name.pluralize}) + + sig { returns(T::Array[String]) } + def attributes_with_index = attributes.select { !_1.reference? && _1.has_index? } sig { params(dirname: String).returns(String) } def self.next_migration_number(dirname) diff --git a/lib/generators/categoria/templates/model_migration.rb.erb b/lib/generators/categoria/templates/model_migration.rb.erb index e69de29..1491c9c 100644 --- a/lib/generators/categoria/templates/model_migration.rb.erb +++ b/lib/generators/categoria/templates/model_migration.rb.erb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>] + def change + create_table :<%= domain_prefixed_relation_name %><%= primary_key_type %> do |t| +<% attributes.each do |attribute| -%> +<% if attribute.password_digest? -%> + t.string :password_digest<%= attribute.inject_options %> +<% elsif attribute.token? -%> + t.string :<%= attribute.name %><%= attribute.inject_options %> +<% elsif attribute.reference? -%> + t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %><%= foreign_key_type %> +<% elsif !attribute.virtual? -%> + t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %> +<% end -%> +<% end -%> +<% if options[:timestamps] %> + t.timestamps +<% end -%> + end +<% attributes.select(&:token?).each do |attribute| -%> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true +<% end -%> +<% attributes_with_index.each do |attribute| -%> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> +<% end -%> + end +end