class RailsERD::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 RailsERD.

# File lib/rails_erd/domain.rb, line 26
def generate(options = {})
  new ActiveRecord::Base.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/rails_erd/domain.rb, line 45
def initialize(models = [], options = {})
  @source_models, @options = models, RailsERD.options.merge(options)
end

Public Instance Methods

entities() click to toggle source

Returns all entities of your domain model.

# File lib/rails_erd/domain.rb, line 62
def entities
  @entities ||= Entity.from_models(self, models)
end
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/rails_erd/domain.rb, line 51
def 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
relationships() click to toggle source

Returns all relationships in your domain model.

# File lib/rails_erd/domain.rb, line 67
def relationships
  @relationships ||= Relationship.from_associations(self, associations)
end
specializations() click to toggle source

Returns all specializations in your domain model.

# File lib/rails_erd/domain.rb, line 72
def specializations
  @specializations ||= Specialization.from_models(self, models)
end
specializations_by_entity_name(name) click to toggle source
# File lib/rails_erd/domain.rb, line 86
def specializations_by_entity_name(name)
  specializations_mapping[name] or []
end

Private Instance Methods

association_description(association) click to toggle source
# File lib/rails_erd/domain.rb, line 169
def association_description(association)
  "#{association.name.inspect} on #{association.active_record}"
end
associations() click to toggle source
# File lib/rails_erd/domain.rb, line 126
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/rails_erd/domain.rb, line 144
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/rails_erd/domain.rb, line 173
def check_habtm_model(model)
  model.name.start_with?("HABTM_")
end
check_model_validity(model) click to toggle source
# File lib/rails_erd/domain.rb, line 130
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/rails_erd/domain.rb, line 158
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/rails_erd/domain.rb, line 96
def entity_mapping
  @entity_mapping ||= {}.tap do |mapping|
    entities.each do |entity|
      mapping[entity.name] = entity
    end
  end
end
models() click to toggle source
# File lib/rails_erd/domain.rb, line 122
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/rails_erd/domain.rb, line 104
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
specializations_mapping() click to toggle source
# File lib/rails_erd/domain.rb, line 113
def specializations_mapping
  @specializations_mapping ||= {}.tap do |mapping|
    specializations.each do |specialization|
      (mapping[specialization.generalized.name] ||= []) << specialization
      (mapping[specialization.specialized.name] ||= []) << specialization
    end
  end
end