Class: DBDiagram::Domain

Inherits:
Object
  • Object
show all
Extended by:
Inspectable
Defined in:
lib/db_diagram/domain.rb,
lib/db_diagram/domain/entity.rb,
lib/db_diagram/domain/attribute.rb,
lib/db_diagram/domain/relationship.rb,
lib/db_diagram/domain/relationship/cardinality.rb

Overview

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.

Defined Under Namespace

Classes: Attribute, Entity, Relationship

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

inspection_attributes

Constructor Details

#initialize(models = [], options = {}) ⇒ Domain

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



45
46
47
# File 'lib/db_diagram/domain.rb', line 45

def initialize(models = [], options = {})
  @source_models, @options = models, DBDiagram.options.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

The options that are used to generate this domain model.



41
42
43
# File 'lib/db_diagram/domain.rb', line 41

def options
  @options
end

Class Method Details

.foreign_key_method_nameObject

Returns the method name to retrieve the foreign key from an association reflection object.



32
33
34
# File 'lib/db_diagram/domain.rb', line 32

def foreign_key_method_name # @private :nodoc:
  @foreign_key_method_name ||= ActiveRecord::Reflection::AssociationReflection.method_defined?(:foreign_key) ? :foreign_key : :primary_key_name
end

.generate(options = {}) ⇒ Object

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.



25
26
27
28
# 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

Instance Method Details

#app_nameObject

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



51
52
53
54
55
56
57
58
59
# 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_versionObject



68
69
70
71
72
73
# 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

#entitiesObject

Returns all entities of your domain model.



76
77
78
# File 'lib/db_diagram/domain.rb', line 76

def entities
  @entities ||= Entity.from_models(self, models)
end

#entity_by_name(name) ⇒ Object

Returns a specific entity object for the given Active Record model.



86
87
88
# File 'lib/db_diagram/domain.rb', line 86

def entity_by_name(name) # @private :nodoc:
  entity_mapping[name]
end

#nameObject



61
62
63
64
65
66
# 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

#relationshipsObject

Returns all relationships in your domain model.



81
82
83
# File 'lib/db_diagram/domain.rb', line 81

def relationships
  @relationships ||= Relationship.from_associations(self, associations)
end

#relationships_by_entity_name(name) ⇒ Object

Returns an array of relationships for the given Active Record model.



91
92
93
# File 'lib/db_diagram/domain.rb', line 91

def relationships_by_entity_name(name) # @private :nodoc:
  relationships_mapping[name] or []
end

#warn(message) ⇒ Object



95
96
97
# File 'lib/db_diagram/domain.rb', line 95

def warn(message) # @private :nodoc:
  puts "Warning: #{message}" if options.warn
end