Class: Yes::Auth::Generators::BaseGenerator Abstract

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/yes/auth/generators/base_generator.rb

Overview

This class is abstract.

Subclass and implement #_table_name and #_source_template

Base class for auth principal migration generators

Provides shared migration generation logic including timestamp calculation, existence checking, and template rendering.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(migrations_root) ⇒ String

Calculates the next migration number based on existing migrations

Parameters:

  • migrations_root (String)

    path to the migrations directory

Returns:

  • (String)

    the next migration number as a timestamp string



23
24
25
26
27
28
29
30
# File 'lib/yes/auth/generators/base_generator.rb', line 23

def self.next_migration_number(migrations_root)
  paths = Dir["#{migrations_root}/*.rb"].map do |path|
    File.basename(path)
  end
  timestamps = paths.map { |name| name.split('_').first }
  latest = timestamps.max || '0'
  [Time.now.utc.strftime('%Y%m%d%H%M%S'), format('%.14d', latest.next)].max
end

Instance Method Details

#_destinationObject



46
47
48
# File 'lib/yes/auth/generators/base_generator.rb', line 46

def _destination
  "#{_migration_dir}/#{_migration_file_name}"
end

#_migration_dirObject



54
55
56
# File 'lib/yes/auth/generators/base_generator.rb', line 54

def _migration_dir
  'db/migrate'
end

#_migration_file_nameObject



50
51
52
# File 'lib/yes/auth/generators/base_generator.rb', line 50

def _migration_file_name
  "create_#{_table_name.singularize}.rb"
end

#_source_templateObject

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/yes/auth/generators/base_generator.rb', line 42

def _source_template
  raise NotImplementedError
end

#_table_nameObject

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/yes/auth/generators/base_generator.rb', line 38

def _table_name
  raise NotImplementedError
end

#createObject



32
33
34
35
36
# File 'lib/yes/auth/generators/base_generator.rb', line 32

def create
  raise "Migration already exists in #{_migration_dir}" if self.class.migration_exists?(_migration_dir, _migration_file_name)

  migration_template(_source_template, _destination)
end