Class: Athar::Generators::MaskGenerator

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

RESERVED_MASK_NAMES =
%w[email partial hash].freeze
SAFE_IDENTIFIER_REGEX =
/\A[A-Za-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



134
135
136
# File 'lib/generators/athar/mask/mask_generator.rb', line 134

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

Instance Method Details

#generate_migrationObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/athar/mask/mask_generator.rb', line 47

def generate_migration
  template_name =
    if remove?
      fx? ? "remove_migration_fx.rb.erb" : "remove_migration.rb.erb"
    elsif update?
      fx? ? "update_migration_fx.rb.erb" : "update_migration.rb.erb"
    else
      fx? ? "install_migration_fx.rb.erb" : "install_migration.rb.erb"
    end
  migration_template template_name, "db/migrate/#{migration_filename}.rb"
end

#validate_options!Object



31
32
33
34
35
36
# File 'lib/generators/athar/mask/mask_generator.rb', line 31

def validate_options!
  validate_name!
  validate_action!
  validate_no_active_references! if remove?
  ensure_raw_sql_supported! unless fx?
end

#write_function_fileObject



38
39
40
41
42
43
44
45
# File 'lib/generators/athar/mask/mask_generator.rb', line 38

def write_function_file
  return unless fx? && !remove?

  FileUtils.mkdir_p(functions_destination)
  version = next_version.to_s.rjust(2, "0")
  path = File.join(functions_destination, "athar_mask_#{name}_v#{version}.sql")
  File.write(path, function_body)
end