Module: DynamicModel

Defined in:
lib/database_introspection/dynamic_model.rb

Defined Under Namespace

Modules: ActiveRecordExtension, DomainExtension, ManagedDomains Classes: Migration, RelationsAnalyser, TablesAnalyser

Class Method Summary collapse

Class Method Details

.add_table(scoped_table_name, table_prefix: :user_defined, &block) ⇒ Object

Creates a new table with auto numbered id in the database with the name prefix provided by creating a live migration. If block is provided it will behave like create_table method for migrations, allowing to create any other column.



23
24
25
26
27
28
# File 'lib/database_introspection/dynamic_model.rb', line 23

def self.add_table(scoped_table_name, table_prefix: :user_defined, &block)
  scoped_table_name = scoped_table_name.to_s
  table_prefix = table_prefix.to_s
  real_table_name = scoped_table_name.underscore.pluralize
  Migration::create_with "#{table_prefix}_#{real_table_name}", &block
end

.introspect_database(table_prefix = :user_defined, base_class = ActiveRecord::Base) ⇒ Object

Creates ActiveRecord::Base descendants from the database. ActiveRecord descendant classes are dynamically created from database introspection in their own namespace (DynamicModel::<NameSpace>::) The name of the module NameSpace is derived from table_prefix.



8
9
10
11
12
13
14
15
16
17
# File 'lib/database_introspection/dynamic_model.rb', line 8

def self.introspect_database(table_prefix = :user_defined, base_class = ActiveRecord::Base)
  table_prefix = table_prefix.to_s
  @domain_analyser ||= {}

  # Confines Activerecord classes into a module named from table_prefix
  @domain_analyser[table_prefix] ||= DynamicModel::TablesAnalyser.new table_prefix, base_class
  klasses = @domain_analyser[table_prefix].scan_database
  relation_analyser = RelationsAnalyser.new klasses
  relation_analyser.run
end