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
-
.all_tables_for_features(features, table_prefix: 'account', db_type: :postgres) ⇒ Array<Symbol>
Get all tables for a set of features.
-
.extract_tables_from_template(feature, table_prefix: 'account', db_type: :postgres) ⇒ Array<Symbol>
Extract table names from a single ERB template.
-
.template_path_for_feature(feature) ⇒ String
Get template path for a feature.
Class Method Details
.all_tables_for_features(features, table_prefix: 'account', db_type: :postgres) ⇒ Array<Symbol>
Get all tables for a set of 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
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.}" 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
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 |