Skip to content

Commit

Permalink
wip2: generate model class and migration file
Browse files Browse the repository at this point in the history
  • Loading branch information
yawboakye committed Jun 5, 2024
1 parent 01afcba commit 7435c64
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
10 changes: 10 additions & 0 deletions lib/categoria/data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# typed: strict
# frozen_string_literal: true

module Categoria
class Data
extend T::Sig

abstract!
end
end
34 changes: 26 additions & 8 deletions lib/generators/categoria/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@

require "rails/generators"
require "rails/generators/active_record"
require "rails/generators/active_record/migration"
require "rails/generators/model_helpers"
require "rails/generators/migration"
require "active_support/inflector"
require "active_record"

module Categoria
module Generators
# `ModelGenerator` is almost an exact copy of the ActiveRecord
# model generator. significant departures are as follows:
# - migrations cannot be skipped since a model should be an ORM class.
# - migrations cannot be skipped since a model should be an orm class,
# otherwise a data class is what you need. a data class typically
# serializes one or more model records into a form that is publicized.
# they are allowed to perform all sorts of key & value transformations
# for purposes of data interchange format and/or
# security-by-obscurity.
# - models are not in `app/models` but instead internal to domain.
class ModelGenerator < ::Rails::Generators::NamedBase
include ::Rails::Generators::ModelHelpers
include ::Rails::Generators::Migration
include ::ActiveRecord::Generators::Migration

source_root File.expand_path("templates", __dir__)

Expand All @@ -27,11 +35,9 @@ class ModelGenerator < ::Rails::Generators::NamedBase

sig { void }
def create_migration_file
debugger

migration_template(
"model_migration.rb.erb",
"db/migrate/create_#{relation_name}_table.rb",
File.join(db_migrate_path, "create_#{relation_name}_table.rb"),
migration_version:
)
end
Expand All @@ -40,26 +46,38 @@ def create_migration_file
def migration_version = "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"

sig { void }
def generate_internal_model; end
def generate_internal_model
in_root do
template \
"model.rb.erb",
"app/lib/#{domain_name}/internal/models/#{internal_model_name}.rb"
end
end

sig { returns(String) }
def domain_module = domain_name.capitalize

sig { returns(String) }
def internal_model_class_name = ActiveSupport::Inflector.classify(internal_model_name)

sig { returns(String) }
private def domain_name
def domain_name
@domain_name ||= T.let(
T.must(name.split(/:/)[0]),
T.nilable(String)
)
end

sig { returns(String) }
private def internal_model_name
def internal_model_name
@internal_model_name ||= T.let(
T.must(name.split(/:/)[1]),
T.nilable(String)
)
end

sig { returns(String) }
private def relation_name = %(#{domain_name}_#{internal_model_name.pluralize})
def relation_name = %(#{domain_name}_#{internal_model_name.pluralize})

sig { params(dirname: String).returns(String) }
def self.next_migration_number(dirname)
Expand Down
12 changes: 9 additions & 3 deletions lib/generators/categoria/templates/model.rb.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# frozen_string_literal: true

<% module_namespacing do -%>
class <%= class_name %> < ApplicationRecord; end
<% end -%>
module <%= Rails.application.class.module_parent %>
module <%= domain_module %>
module Internal
module Models
class <%= internal_model_class_name %> < ApplicationRecord; end
end
end
end
end

0 comments on commit 7435c64

Please sign in to comment.