Module: DynamicActiveModel::Explorer

Defined in:
lib/dynamic-active-model/explorer.rb

Overview

The Explorer module provides a high-level interface for automatically discovering and setting up ActiveRecord models and their relationships from a database schema. It combines the functionality of Database and Associations classes into a simple one-call interface.

Examples:

Basic Usage

module DB; end
DynamicActiveModel::Explorer.explore(DB, database_config)

With Table Filtering

skip_tables = ['temporary_data', 'audit_logs']
DynamicActiveModel::Explorer.explore(DB, database_config, skip_tables)

With Foreign Key Constraints and has_many :through

DynamicActiveModel::Explorer.explore(DB, database_config, foreign_key_constraints: true,
                                                          has_many_through: true)

With Custom Relationships

relationships = {
  'users' => {
    'manager_id' => 'manager',
    'department_id' => 'department'
  }
}
DynamicActiveModel::Explorer.explore(DB, database_config, [], relationships)

Class Method Summary collapse

Class Method Details

.build_relationships!(database, relationships, foreign_key_constraints: false, has_many_through: false, optional_belongs_to: false) ⇒ void

This method returns an undefined value.

Sets up relationships between created models

Parameters:

  • database (Database) —

    The database instance containing the models

  • relationships (Hash) —

    Custom foreign key relationships to add

  • foreign_key_constraints (Boolean) (defaults to: false) —

    Also relate columns through foreign key constraints

  • has_many_through (Boolean) (defaults to: false) —

    Also add has_many :through across join models

  • optional_belongs_to (Boolean) (defaults to: false) —

    Make every belongs_to optional



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dynamic-active-model/explorer.rb', line 87

def self.build_relationships!(database, relationships, foreign_key_constraints: false, has_many_through: false,
                              optional_belongs_to: false)
  relations = Associations.new(database)
  relations.use_foreign_key_constraints! if foreign_key_constraints
  relations.use_has_many_through! if has_many_through
  relations.use_optional_belongs_to! if optional_belongs_to
  relationships.each do |table_name, foreign_keys|
    foreign_keys.each do |foreign_key, relationship_name|
      relations.add_foreign_key(table_name, foreign_key, relationship_name)
    end
  end
  relations.build!
end

.create_models!(base_module, connection_options, skip_tables, table_class_names = {}, parent_class: nil) ⇒ Database

Creates ActiveRecord models from database tables

Parameters:

  • base_module (Module) —

    The namespace for created models

  • connection_options (Hash) —

    Database connection options

  • skip_tables (Array<String, Regexp>) —

    Tables to exclude from model creation

  • table_class_names (Hash) (defaults to: {}) —

    Custom class names by table name

  • parent_class (Class, nil) (defaults to: nil) —

    Optional superclass for the base class (see Factory)

Returns:

  • (Database) —

    The configured database instance



63
64
65
66
67
68
69
# File 'lib/dynamic-active-model/explorer.rb', line 63

def self.create_models!(base_module, connection_options, skip_tables, table_class_names = {}, parent_class: nil)
  database = Database.new(base_module, connection_options, parent_class: parent_class)
  skip_tables.each { |table| database.skip_table(skip_table_matcher(table)) }
  table_class_names.each { |table_name, class_name| database.table_class_name(table_name, class_name) }
  database.create_models!
  database
end

.explore(base_module, connection_options, skip_tables = [], relationships = {}, table_class_names = {}, parent_class: nil, foreign_key_constraints: false, has_many_through: false, optional_belongs_to: false) ⇒ Database

Creates models and sets up relationships in a single call

Parameters:

  • base_module (Module) —

    The namespace for created models

  • connection_options (Hash) —

    Database connection options

  • skip_tables (Array<String, Regexp>) (defaults to: []) —

    Tables to exclude from model creation

  • relationships (Hash) (defaults to: {}) —

    Custom foreign key relationships to add

  • table_class_names (Hash) (defaults to: {}) —

    Custom class names by table name

  • parent_class (Class, nil) (defaults to: nil) —

    Optional superclass for the base class (see Factory)

  • foreign_key_constraints (Boolean) (defaults to: false) —

    Also relate columns through the database's foreign key constraints (see ForeignKeyConstraints)

  • has_many_through (Boolean) (defaults to: false) —

    Also add has_many :through across join models (see HasManyThrough)

  • optional_belongs_to (Boolean) (defaults to: false) —

    Make every belongs_to optional, even on NOT NULL foreign keys, so saves don't validate the parent exists

Returns:

  • (Database) —

    The configured database instance

Raises:



45
46
47
48
49
50
51
52
53
54
# File 'lib/dynamic-active-model/explorer.rb', line 45

def self.explore(base_module, connection_options, skip_tables = [], relationships = {}, table_class_names = {},
                 parent_class: nil, foreign_key_constraints: false, has_many_through: false,
                 optional_belongs_to: false)
  database = create_models!(base_module, connection_options, skip_tables, table_class_names,
                            parent_class: parent_class)
  build_relationships!(database, relationships, foreign_key_constraints: foreign_key_constraints,
                                                has_many_through: has_many_through,
                                                optional_belongs_to: optional_belongs_to)
  database
end

.skip_table_matcher(table) ⇒ String, Regexp

Converts a table name containing * wildcards into an anchored Regexp

Parameters:

  • table (String, Regexp) —

    Table name, wildcard pattern, or Regexp

Returns:

  • (String, Regexp) —

    The table name or matching Regexp



74
75
76
77
78
# File 'lib/dynamic-active-model/explorer.rb', line 74

def self.skip_table_matcher(table)
  return table unless table.is_a?(String) && table.include?('*')

  Regexp.new("\\A#{Regexp.escape(table).gsub('\\*', '.*')}\\z")
end