Class: Dbwatcher::Services::DiagramAnalyzers::ModelAssociationAnalyzer

Inherits:
BaseAnalyzer show all
Includes:
Concerns::ActiverecordIntrospection, Concerns::AssociationScopeFiltering
Defined in:
lib/dbwatcher/services/diagram_analyzers/model_association_analyzer.rb

Overview

Analyzes relationships based on ActiveRecord model associations

This service examines ActiveRecord models to detect associations between models that were involved in a session. It uses direct model enumeration from ActiveRecord::Base.descendants to ensure reliable model discovery.

Supported model scenarios:

  • Regular models with standard table names

  • Namespaced models (e.g., Admin::User)

  • Models with custom table names (using self.table_name)

  • Models from external gems and complex inheritance hierarchies

Examples:

analyzer = ModelAssociationAnalyzer.new(session)
dataset = analyzer.call

Instance Method Summary collapse

Methods inherited from BaseAnalyzer

#call

Methods inherited from BaseService

call, #call

Methods included from Logging

#debug_enabled?, #log_debug, #log_error, #log_info, #log_warn

Constructor Details

#initialize(session = nil) ⇒ ModelAssociationAnalyzer

Initialize with session

Parameters:

  • session (Session) (defaults to: nil)

    session to analyze (optional for global analysis)



34
35
36
37
38
39
40
41
42
# File 'lib/dbwatcher/services/diagram_analyzers/model_association_analyzer.rb', line 34

def initialize(session = nil)
  @session = session
  @session_tables = session ? extract_session_tables(session) : []
  @model_discovery = ModelAnalysis::ModelDiscovery.new(@session_tables)
  @association_extractor = ModelAnalysis::AssociationExtractor.new(@session_tables)
  @dataset_builder = ModelAnalysis::DatasetBuilder.new
  @models = []
  super()
end

Instance Method Details

#analyze(_context) ⇒ Array<Hash>

Analyze model associations

Parameters:

  • context (Hash)

    analysis context

Returns:

  • (Array<Hash>)

    array of association data



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dbwatcher/services/diagram_analyzers/model_association_analyzer.rb', line 48

def analyze(_context)
  return [] unless models_available?

  @models = @model_discovery.discover
  return [] if @models.empty?

  Rails.logger.debug "ModelAssociationAnalyzer: Starting analysis with #{@models.length} models"
  associations = @association_extractor.extract_all(@models)
  if associations.empty? && @models.any?
    associations = @association_extractor.generate_placeholder_associations(@models)
  end

  log_analysis_results(associations)
  associations
end

#analyzer_typeString

Get analyzer type

Returns:

  • (String)

    analyzer type identifier



75
76
77
# File 'lib/dbwatcher/services/diagram_analyzers/model_association_analyzer.rb', line 75

def analyzer_type
  "model_association"
end

#transform_to_dataset(raw_data) ⇒ DiagramData::Dataset

Transform raw association data to Dataset

Parameters:

  • raw_data (Array<Hash>)

    raw association data

Returns:



68
69
70
# File 'lib/dbwatcher/services/diagram_analyzers/model_association_analyzer.rb', line 68

def transform_to_dataset(raw_data)
  @dataset_builder.build_from_associations(raw_data, @models)
end