Module: Rodauth::TemplateInspector

Defined in:
lib/rodauth/template_inspector.rb

Overview

Inspects ERB templates to extract table information

This module solves the "hidden tables" problem where ERB templates create tables that don't have corresponding *_table methods in Rodauth features. For example, base.erb creates account_statuses and account_password_hashes tables, but only accounts_table method exists.

By evaluating ERB templates, we can discover ALL tables that will be created, which is essential for generating complete DROP statements.

Defined Under Namespace

Classes: BindingContext

Class Method Summary collapse

Class Method Details

.all_tables_for_features(features, table_prefix: 'account', db_type: :postgres) ⇒ Array<Symbol>

Get all tables for a set of features

Parameters:

  • features (Array<Symbol>)

    Feature names

  • table_prefix (String) (defaults to: 'account')

    Table prefix to use

  • db_type (Symbol) (defaults to: :postgres)

    Database type

Returns:

  • (Array<Symbol>)

    Array of all table names across features



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rodauth/template_inspector.rb', line 55

def self.all_tables_for_features(features, table_prefix: 'account', db_type: :postgres)
  tables = []

  features.each do |feature|
    feature_tables = extract_tables_from_template(
      feature,
      table_prefix: table_prefix,
      db_type: db_type
    )
    tables.concat(feature_tables)
  end

  tables.uniq
end

.extract_tables_from_template(feature, table_prefix: 'account', db_type: :postgres) ⇒ Array<Symbol>

Extract table names from a single ERB template

Parameters:

  • feature (Symbol)

    Feature name (e.g., :base, :verify_account)

  • table_prefix (String) (defaults to: 'account')

    Table prefix to use (default: 'account')

  • db_type (Symbol) (defaults to: :postgres)

    Database type for conditional logic (default: :postgres)

Returns:

  • (Array<Symbol>)

    Array of table names that will be created



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rodauth/template_inspector.rb', line 25

def self.extract_tables_from_template(feature, table_prefix: 'account', db_type: :postgres)
  template_path = template_path_for_feature(feature)
  return [] unless File.exist?(template_path)

  template_content = File.read(template_path)

  # Create binding context with necessary methods
  context = BindingContext.new(table_prefix, db_type)

  # Evaluate ERB template
  begin
    rendered = ERB.new(template_content, trim_mode: '-').result(context.get_binding)
  rescue StandardError => e
    warn "Failed to evaluate template for #{feature}: #{e.message}"
    return []
  end

  # Extract table names from create_table calls
  # Matches: create_table(:table_name) or create_table?(:table_name)
  tables = rendered.scan(/create_table\??[:(\s]+:?(\w+)/).flatten

  tables.map(&:to_sym).uniq
end

.template_path_for_feature(feature) ⇒ String

Get template path for a feature

Parameters:

  • feature (Symbol)

    Feature name

Returns:

  • (String)

    Absolute path to ERB template



74
75
76
# File 'lib/rodauth/template_inspector.rb', line 74

def self.template_path_for_feature(feature)
  File.join(__dir__, 'tools', 'migration', 'sequel', "#{feature}.erb")
end