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, base_class: ActiveRecord::Base, &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
29
# File 'lib/database_introspection/dynamic_model.rb', line 23

def self.add_table(scoped_table_name, table_prefix: :user_defined, base_class: ActiveRecord::Base, &block)
  for_action_on_table(scoped_table_name, table_prefix) do |table_prefix, real_table_name|
    Migration::create_for "#{table_prefix}_#{real_table_name}", &block
  end
ensure
  analyse_domain table_prefix, base_class
end

.alter_table(scoped_table_name, table_prefix: :user_defined, base_class: ActiveRecord::Base, &block) ⇒ Object

Modifies a table 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.



35
36
37
38
39
40
41
42
# File 'lib/database_introspection/dynamic_model.rb', line 35

def self.alter_table(scoped_table_name, table_prefix: :user_defined, base_class: ActiveRecord::Base, &block)
  raise "Missing block parameter" unless block_given?
  for_action_on_table(scoped_table_name, table_prefix) do |table_prefix, real_table_name|
    Migration::update_for "#{table_prefix}_#{real_table_name}", &block
  end
ensure
  analyse_domain table_prefix, base_class
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.



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

def self.introspect_database(table_prefix = :user_defined, base_class = ActiveRecord::Base)
  if table_prefix.class == Array
    table_prefix.each {|p| self.introspect_database p, base_class}
    return
  end
  table_prefix = table_prefix.to_s
  analyse_domain table_prefix, base_class
end