# frozen_string_literal: true require "rails-mermaid_erd" require "active_support" require "active_support/core_ext/enumerable" require "active_support/core_ext/object" module MermaidErdMarkdown class SourceData def data @data ||= generate_model_data end def split_output(depth = 1) source_models = data[:Models] source_relations = data[:Relations] output = [] source_models.each do |model| model_names = [model[:ModelName]] search_models = model_names relations = [] depth.times do found_relations = [] next_search_models = [] search_models.each do |search_model| found_relations += related_models(search_model, source_relations) next_search_models += related_model_names(search_model, found_relations) end search_models = next_search_models model_names += search_models relations += found_relations end output << { Models: models(model_names.uniq, source_models), Relations: relations.uniq } end output end private def configuration @configuration ||= MermaidErdMarkdown::Configuration.new end def generate_model_data raw_data = RailsMermaidErd::Builder.model_data return raw_data unless filter_model_data? filter_model_data(raw_data) end def filter_model_data? configuration.ignored_models.present? end def filter_model_data(raw_data) filtered_data = {} filtered_data[:Models] = raw_data[:Models].select do |model_hash| not_filtered?(model_hash[:ModelName]) end filtered_data[:Relations] = raw_data[:Relations].select do |relation_hash| not_filtered?(relation_hash[:LeftModelName]) && not_filtered?(relation_hash[:RightModelName]) end filtered_data end def not_filtered?(model_name) configuration.ignored_models.exclude?(model_name) end def models(model_names, source_models) model_names.map do |model_name| source_models.find { |m| m[:ModelName] == model_name } end.compact end def related_model_names(model_name, relations) relations.map do |r| r[:LeftModelName] == model_name ? r[:RightModelName] : r[:LeftModelName] end end def related_models(model_name, relations) relations.select do |relation| relation[:LeftModelName] == model_name || relation[:RightModelName] == model_name end end end end