class DBDiagram::Domain

The domain describes your Rails domain model. This class is the starting point to get information about your models.

Options

The following options are available:

warn

When set to false, no warnings are printed to the command line while processing the domain model. Defaults to true.

Attributes

options[R]

The options that are used to generate this domain model.

Public Class Methods

generate(options = {}) click to toggle source

Generates a domain model object based on all loaded subclasses of ActiveRecord::Base. Make sure your models are loaded before calling this method.

The options hash allows you to override the default options. For a list of available options, see DBDiagram.

# File lib/db_diagram/domain.rb, line 25
def generate(options = {})
  base_klass = options.delete(:base_klass) || ActiveRecord::Base
  new base_klass.descendants, options
end
new(models = [], options = {}) click to toggle source

Create a new domain model object based on the given array of models. The given models are assumed to be subclasses of ActiveRecord::Base.

# File lib/db_diagram/domain.rb, line 45
def initialize(models = [], options = {})
  @source_models, @options = models, DBDiagram.options.merge(options)
end

Public Instance Methods

app_name() click to toggle source

Returns the domain model name, which is the name of your Rails application or nil outside of Rails.

# File lib/db_diagram/domain.rb, line 51
def app_name
  return unless defined?(Rails) && Rails.application

  if Rails.application.class.respond_to?(:module_parent)
    Rails.application.class.module_parent.name
  else
    Rails.application.class.parent.name
  end
end
current_migration_version() click to toggle source
# File lib/db_diagram/domain.rb, line 68
def current_migration_version
  raise '' if @source_models.empty?
  @source_models.first.connection.migration_context.current_version
rescue
  '0001'
end
entities() click to toggle source

Returns all entities of your domain model.

# File lib/db_diagram/domain.rb, line 76
def entities
  @entities ||= Entity.from_models(self, models)
end
name() click to toggle source
# File lib/db_diagram/domain.rb, line 61
def name
  return app_name if @source_models.empty?
  @source_models.first.connection.current_database.presence || app_name
rescue
  app_name
end
relationships() click to toggle source

Returns all relationships in your domain model.

# File lib/db_diagram/domain.rb, line 81
def relationships
  @relationships ||= Relationship.from_associations(self, associations)
end

Private Instance Methods

association_description(association) click to toggle source
# File lib/db_diagram/domain.rb, line 165
def association_description(association)
  "#{association.name.inspect} on #{association.active_record}"
end
associations() click to toggle source
# File lib/db_diagram/domain.rb, line 122
def associations
  @associations ||= models.collect(&:reflect_on_all_associations).flatten.select { |assoc| check_association_validity(assoc) }
end
check_association_validity(association) click to toggle source
# File lib/db_diagram/domain.rb, line 140
def check_association_validity(association)
  # Raises an ActiveRecord::ActiveRecordError if the association is broken.
  association.check_validity!

  if association.options[:polymorphic]
    check_polymorphic_association_validity(association)
  else
    entity_name = association.klass.name # Raises NameError if the associated class cannot be found.
    entity_by_name(entity_name) or raise "model #{entity_name} exists, but is not included in domain"
  end
rescue => e
  warn "Ignoring invalid association #{association_description(association)} (#{e.message})"
end
check_habtm_model(model) click to toggle source
# File lib/db_diagram/domain.rb, line 169
def check_habtm_model(model)
  model.name.start_with?("HABTM_")
end
check_model_validity(model) click to toggle source
# File lib/db_diagram/domain.rb, line 126
def check_model_validity(model)
  if model.abstract_class? || model.table_exists?
    if model.name.nil?
      raise "is anonymous class"
    else
      true
    end
  else
    raise "table #{model.table_name} does not exist"
  end
rescue => e
  warn "Ignoring invalid model #{model.name} (#{e.message})"
end
check_polymorphic_association_validity(association) click to toggle source
# File lib/db_diagram/domain.rb, line 154
def check_polymorphic_association_validity(association)
  entity_name = association.class_name
  entity = entity_by_name(entity_name)

  if entity || (entity && entity.generalized?)
    return entity
  else
    raise("polymorphic interface #{entity_name} does not exist")
  end
end
entity_mapping() click to toggle source
# File lib/db_diagram/domain.rb, line 101
def entity_mapping
  @entity_mapping ||= {}.tap do |mapping|
    entities.each do |entity|
      mapping[entity.model_name] = entity
    end
  end
end
models() click to toggle source
# File lib/db_diagram/domain.rb, line 118
def models
  @models ||= @source_models.select { |model| check_model_validity(model) }.reject { |model| check_habtm_model(model) }
end
relationships_mapping() click to toggle source
# File lib/db_diagram/domain.rb, line 109
def relationships_mapping
  @relationships_mapping ||= {}.tap do |mapping|
    relationships.each do |relationship|
      (mapping[relationship.source.name] ||= []) << relationship
      (mapping[relationship.destination.name] ||= []) << relationship
    end
  end
end