Skip to content

Commit

Permalink
copy-pasta rails migration template here verbatim
Browse files Browse the repository at this point in the history
well, with only one rename of `table_name` to
`domain_prefixed_relation_name`. otherwise, no changes.
  • Loading branch information
yawboakye committed Jun 6, 2024
1 parent 7435c64 commit 35d87f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/generators/categoria/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions lib/generators/categoria/templates/model_migration.rb.erb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 35d87f6

Please sign in to comment.