class DynamicActiveModel::Database

DynamicActiveModel::Database iterates over the tables of a

database and create ActiveRecord models

Attributes

factory[R]
models[R]
table_class_names[R]

Public Class Methods

new(base_module, connection_options, base_class_name = nil) click to toggle source
# File lib/dynamic-active-model/database.rb, line 11
def initialize(base_module, connection_options, base_class_name = nil)
  @factory = Factory.new(base_module, connection_options, base_class_name)
  @table_class_names = {}
  @skip_tables = []
  @skip_table_matchers = []
  @include_tables = []
  @include_table_matchers = []
  @models = []
end

Public Instance Methods

create_models!() click to toggle source
# File lib/dynamic-active-model/database.rb, line 49
def create_models!
  @factory.base_class.connection.tables.each do |table_name|
    next if skip_table?(table_name)
    next unless include_table?(table_name)

    @models << @factory.create(table_name, @table_class_names[table_name])
  end
end
disable_standard_table_inheritance!() click to toggle source
# File lib/dynamic-active-model/database.rb, line 66
def disable_standard_table_inheritance!
  models.each do |model|
    model.inheritance_column = :_type_disabled if model.attribute_names.include?('type')
  end
end
Also aliased as: disable_sti!
disable_sti!()
include_table(table) click to toggle source
# File lib/dynamic-active-model/database.rb, line 33
def include_table(table)
  if table.is_a?(Regexp)
    @include_table_matchers << table
  else
    @include_tables << table.to_s
  end
end
include_tables(tables) click to toggle source
# File lib/dynamic-active-model/database.rb, line 41
def include_tables(tables)
  tables.each { |table| include_table(table) }
end
included_tables() click to toggle source
# File lib/dynamic-active-model/database.rb, line 62
def included_tables
  @include_tables + @include_table_matchers
end
skip_table(table) click to toggle source
# File lib/dynamic-active-model/database.rb, line 21
def skip_table(table)
  if table.is_a?(Regexp)
    @skip_table_matchers << table
  else
    @skip_tables << table.to_s
  end
end
skip_tables(tables) click to toggle source
# File lib/dynamic-active-model/database.rb, line 29
def skip_tables(tables)
  tables.each { |table| skip_table(table) }
end
skipped_tables() click to toggle source
# File lib/dynamic-active-model/database.rb, line 58
def skipped_tables
  @skip_tables + @skip_table_matchers
end
table_class_name(table_name, class_name) click to toggle source
# File lib/dynamic-active-model/database.rb, line 45
def table_class_name(table_name, class_name)
  @table_class_names[table_name.to_s] = class_name
end

Private Instance Methods

include_table?(table_name) click to toggle source
# File lib/dynamic-active-model/database.rb, line 80
def include_table?(table_name)
  (@include_tables.empty? && @include_table_matchers.empty?) ||
    @include_tables.include?(table_name) ||
    @include_table_matchers.any? { |r| r.match(table_name) }
end
skip_table?(table_name) click to toggle source
# File lib/dynamic-active-model/database.rb, line 75
def skip_table?(table_name)
  @skip_tables.include?(table_name.to_s) ||
    @skip_table_matchers.any? { |r| r.match(table_name) }
end