Class: Athar::Generators::ModelGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
FxHelper, Rails::Generators::Migration
Defined in:
lib/generators/athar/model/model_generator.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

ALLOWED_ID_TYPES =
%w[bigint integer uuid].freeze
CAPTURE_MODES =
%w[identity only snapshot].freeze
UNSAFE_COLUMN_REGEX =
/[\s,{}"\\']/
SAFE_IDENTIFIER_REGEX =

PostgreSQL unquoted identifier surface: starts with letter or _, then letters/digits/underscores. Matches what the generator embeds inside both ‘“identifier”` and `’string’‘ SQL contexts.

/\A[A-Za-z_][A-Za-z0-9_]*\z/
SAFE_CLASS_NAME_REGEX =

Ruby class names, with optional ‘::` namespacing.

/\A[A-Z][A-Za-z0-9_]*(::[A-Z][A-Za-z0-9_]*)*\z/

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FxHelper

#athar_foreign_key_type, #athar_primary_key_type, #ensure_raw_sql_supported!, #fx?, included, #indent_sql, #primary_key_setting, #schema_format

Class Method Details

.next_migration_number(dir) ⇒ Object



277
278
279
# File 'lib/generators/athar/model/model_generator.rb', line 277

def self.next_migration_number(dir)
  ::ActiveRecord::Generators::Base.next_migration_number(dir)
end

Instance Method Details

#generate_migrationObject



64
65
66
67
# File 'lib/generators/athar/model/model_generator.rb', line 64

def generate_migration
  template = fx? ? "migration_fx.rb.erb" : "migration.rb.erb"
  migration_template template, "db/migrate/#{migration_filename}.rb"
end

#validate_options!Object



38
39
40
41
42
43
44
# File 'lib/generators/athar/model/model_generator.rb', line 38

def validate_options!
  validate_capture_mode!
  validate_identifiers!
  validate_id_type!
  validate_columns!
  ensure_raw_sql_supported! unless fx?
end

#write_trigger_filesObject

rubocop:disable Metrics/AbcSize



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/athar/model/model_generator.rb', line 46

def write_trigger_files # rubocop:disable Metrics/AbcSize
  return unless fx?
  return if remove?

  FileUtils.mkdir_p(triggers_destination)
  # Reading trigger_descriptors first caches the version computation
  # against the on-disk state *before* we write the new files.
  trigger_descriptors.each do |descriptor|
    path = File.join(
      triggers_destination,
      "#{descriptor[:name]}_v#{descriptor[:version].to_s.rjust(2, "0")}.sql"
    )
    next if File.exist?(path) && File.read(path) == descriptor[:body]

    File.write(path, descriptor[:body])
  end
end