From 7435c649c4e80c2c7ceed855622c0b1fb7d42bfb Mon Sep 17 00:00:00 2001 From: Yaw Boakye Date: Wed, 5 Jun 2024 14:49:24 +0100 Subject: [PATCH] wip2: generate model class and migration file --- lib/categoria/data.rb | 10 ++++++ lib/generators/categoria/model_generator.rb | 34 ++++++++++++++----- .../categoria/templates/model.rb.erb | 12 +++++-- 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 lib/categoria/data.rb diff --git a/lib/categoria/data.rb b/lib/categoria/data.rb new file mode 100644 index 0000000..3242941 --- /dev/null +++ b/lib/categoria/data.rb @@ -0,0 +1,10 @@ +# typed: strict +# frozen_string_literal: true + +module Categoria + class Data + extend T::Sig + + abstract! + end +end diff --git a/lib/generators/categoria/model_generator.rb b/lib/generators/categoria/model_generator.rb index e879c02..4b89a0f 100644 --- a/lib/generators/categoria/model_generator.rb +++ b/lib/generators/categoria/model_generator.rb @@ -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__) @@ -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 @@ -40,10 +46,22 @@ 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) @@ -51,7 +69,7 @@ def generate_internal_model; end 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) @@ -59,7 +77,7 @@ def generate_internal_model; end 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) diff --git a/lib/generators/categoria/templates/model.rb.erb b/lib/generators/categoria/templates/model.rb.erb index 53e5b41..a5b44cc 100644 --- a/lib/generators/categoria/templates/model.rb.erb +++ b/lib/generators/categoria/templates/model.rb.erb @@ -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