class Metasploit::ERD::Relationship

The relationship in an Entity-Relationship Diagram. Modelled using an {#association} extracted from a `Class<ActiveRecord::Base>` using reflection.

Attributes

association[R]

Attributes

Public Class Methods

new(association) click to toggle source

@param association [ActiveRecord::Associations::BelongsToAssociation]

# File lib/metasploit/erd/relationship.rb, line 20
def initialize(association)
  @association = association
end

Public Instance Methods

class_set() click to toggle source

Set of classes pointed to by this association. Differs from `association.klass` as {#class_set} properly handles polymorphic associations by finding all `Class<ActiveRecord::Base>` that `has_many <inverse>, as: <name>` and so can fulfill `belongs_to <name>, polymorpic: true`.

@return [Set<Class<ActiveRecord::Base>>]

# File lib/metasploit/erd/relationship.rb, line 29
def class_set
  if association.options[:polymorphic]
    polymorphic_class_set
  else
    Set.new([association.klass])
  end
end

Private Instance Methods

polymorphic_class_set() click to toggle source

Finds the target classes for `belongs_to <name>, polymorphic: true`.

@return [Set<Class<ActiveRecord::Base>>]

# File lib/metasploit/erd/relationship.rb, line 42
def polymorphic_class_set
  name = association.name

  ActiveRecord::Base.descendants.each_with_object(Set.new) { |descendant, class_set|
    has_many_reflections = descendant.reflect_on_all_associations(:has_many)

    has_many_reflections.each do |has_many_reflection|
      as = has_many_reflection.options[:as]

      if as == name
        class_set.add descendant
      end
    end
  }
end