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 totrue.
Attributes
The options that are used to generate this domain model.
Public Class Methods
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
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
Returns all entities of your domain model.
# File lib/rails_erd/domain.rb, line 62 def entities @entities ||= Entity.from_models(self, models) end
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
Returns all relationships in your domain model.
# File lib/rails_erd/domain.rb, line 67 def relationships @relationships ||= Relationship.from_associations(self, associations) end
Returns all specializations in your domain model.
# File lib/rails_erd/domain.rb, line 72 def specializations @specializations ||= Specialization.from_models(self, models) end
# File lib/rails_erd/domain.rb, line 86 def specializations_by_entity_name(name) specializations_mapping[name] or [] end
Private Instance Methods
# File lib/rails_erd/domain.rb, line 194 def association_description(association) "#{association.name.inspect} on #{association.active_record}" end
# File lib/rails_erd/domain.rb, line 151 def associations @associations ||= models.collect(&:reflect_on_all_associations).flatten.select { |assoc| check_association_validity(assoc) } end
# File lib/rails_erd/domain.rb, line 169 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
# File lib/rails_erd/domain.rb, line 198 def check_habtm_model(model) model.name.start_with?("HABTM_") end
# File lib/rails_erd/domain.rb, line 155 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
# File lib/rails_erd/domain.rb, line 183 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
# 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
# File lib/rails_erd/domain.rb, line 122 def models @models ||= @source_models .reject { |model| tableless_rails_models.include?(model) } .select { |model| check_model_validity(model) } .reject { |model| check_habtm_model(model) } end
Returns Rails model classes defined in the app
# File lib/rails_erd/domain.rb, line 130 def rails_models %w( ActionMailbox::InboundEmail ActiveStorage::Attachment ActiveStorage::Blob ActiveStorage::VariantRecord ActionText::RichText ActionText::EncryptedRichText ).map{ |model| Object.const_get(model) rescue nil }.compact end
# 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
# 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
# File lib/rails_erd/domain.rb, line 141 def tableless_rails_models @tableless_rails_models ||= begin if defined? Rails rails_models.reject{ |model| model.table_exists? } else [] end end end