Class: Pu::Rodauth::MigrationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration, Concerns::AccountSelector, Concerns::Configuration
Defined in:
lib/generators/pu/rodauth/migration_generator.rb

Constant Summary collapse

MIGRATION_DIR =
"#{__dir__}/migration/active_record"

Constants included from Concerns::Configuration

Concerns::Configuration::CONFIGURATION, Concerns::Configuration::FEATURE_CONFIG, Concerns::Configuration::MIGRATION_CONFIG, Concerns::Configuration::VIEW_CONFIG

Instance Method Summary collapse

Methods included from Concerns::AccountSelector

included

Instance Method Details

#configure_rodauth_plugin_tablesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/pu/rodauth/migration_generator.rb', line 44

def configure_rodauth_plugin_tables
  in_root do
    break unless table_prefix != "account" && File.exist?(plugin_filename)

    gsub_file plugin_filename, /.*# accounts_table.*\n/, "" if selected_features.include? :base

    migration_overrides.sort.reverse_each do |key, value|
      override = indent "#{key} :#{value}\n", 4
      insert_into_file plugin_filename, override,
        after: /.*# Change prefix of table and.*\n/
    end
  end
end

#create_rodauth_migrationObject



40
41
42
# File 'lib/generators/pu/rodauth/migration_generator.rb', line 40

def create_rodauth_migration
  migration_template "db/migrate/create_rodauth.rb", File.join(db_migrate_path, "#{migration_name}.rb")
end

#show_instructionsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/generators/pu/rodauth/migration_generator.rb', line 58

def show_instructions
  in_root do
    break if table_prefix == "account" || File.exist?(plugin_filename)

    configuration = migration_overrides
      .map { |config, format| "#{config} :#{format}" }
      .join("\n")
      .indent(2)

    say "\n\nAdd the following to your Rodauth plugin configure block:", :blue
    say "\n\n#{configuration}\n\n\n\n", :magenta
  end
end

#validate_selected_featuresObject



30
31
32
33
34
35
36
37
38
# File 'lib/generators/pu/rodauth/migration_generator.rb', line 30

def validate_selected_features
  if selected_features.empty?
    say "No migration features specified!", :yellow
    exit(1)
  elsif (selected_features - valid_features).any?
    say "No available migration for feature(s): #{(selected_features - valid_features).join(", ")}", :red
    exit(1)
  end
end