3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/rails-mermaid_erd/builder.rb', line 3
def model_data
result = {
Models: [],
Relations: []
}
::Rails.application.eager_load!
ignore_patterns = RailsMermaidErd.configuration.ignore_tables.map do |pattern|
Regexp.new(pattern)
rescue RegexpError => e
raise ArgumentError, "config/mermaid_erd.yml: invalid `ignore_tables` pattern #{pattern.inspect}: #{e.message}"
end
ignored_model_names = compute_ignored_model_names(ignore_patterns)
::ActiveRecord::Base.descendants.sort_by(&:name).each do |defined_model|
next unless defined_model.table_exists?
next if defined_model.name.include?("HABTM_")
next if defined_model.table_name.blank?
next if ignored_model_names.key?(defined_model.name)
table_name = defined_model.table_name
model = {
TableName: table_name,
TableComment: ::ActiveRecord::Base.connection.(table_name.to_sym) || "",
ModelName: defined_model.name,
IsModelExist: true,
Columns: []
}
foreign_keys = ::ActiveRecord::Schema.foreign_keys(defined_model.table_name).map { |k| k.options[:column] }
primary_key = defined_model.primary_key
defined_model.columns.each do |column|
key = ""
if column.name == primary_key
key = "PK"
elsif foreign_keys.include?(column.name)
key = "FK"
end
model[:Columns] << {
name: column.name,
type: column.type,
key: key,
comment: column.
}
end
result[:Models] << model
defined_model.reflect_on_all_associations(:has_many).each do |reflection|
reflection_model_name = get_reflection_model_name(reflection)
next if ignored_model_names.key?(reflection_model_name)
reverse_relation = result[:Relations].find { |r|
if reflection.options[:through]
r[:RightModelName] == model[:ModelName] && r[:LeftModelName] == reflection_model_name && r[:Line] == ".."
else
r[:RightModelName] == model[:ModelName] && r[:LeftModelName] == reflection_model_name && r[:Line] == "--"
end
}
if reverse_relation
reverse_relation[:Comment] = if reflection.options[:through]
"#{reverse_relation[:Comment]}, HMT:#{reflection.name}"
else
"#{reverse_relation[:Comment]}, HM:#{reflection.name}"
end
else
result[:Relations] << {
LeftModelName: model[:ModelName],
LeftValue: reflection.options[:through] ? "}o" : "||",
Line: reflection.options[:through] ? ".." : "--",
RightModelName: reflection_model_name,
RightValue: "o{",
Comment: reflection.options[:through] ? "HMT:#{reflection.name}" : "HM:#{reflection.name}"
}
end
end
defined_model.reflect_on_all_associations(:has_and_belongs_to_many).each do |reflection|
reflection_model_name = get_reflection_model_name(reflection)
next if ignored_model_names.key?(reflection_model_name)
reverse_relation = result[:Relations].find { |r| r[:RightModelName] == model[:ModelName] && r[:LeftModelName] == reflection_model_name }
if reverse_relation
reverse_relation[:Comment] = "HABTM"
else
result[:Relations] << {
LeftModelName: model[:ModelName],
LeftValue: "}o",
Line: "..",
RightModelName: reflection_model_name,
RightValue: "o{",
Comment: "HABTM"
}
end
end
defined_model.reflect_on_all_associations(:belongs_to).each do |reflection|
next if reflection.polymorphic?
reflection_model_name = get_reflection_model_name(reflection)
next if ignored_model_names.key?(reflection_model_name)
reverse_relation = result[:Relations].find { |r| r[:RightModelName] == model[:ModelName] && r[:LeftModelName] == reflection_model_name }
if reverse_relation
if (::Rails.application.config.active_record.belongs_to_required_by_default && reflection.options[:optional]) || (!::Rails.application.config.active_record.belongs_to_required_by_default && !reflection.options[:required])
reverse_relation[:LeftValue] = "|o"
end
reverse_relation[:Comment] = "#{reverse_relation[:Comment]}, BT:#{reflection.name}"
else
right_value = if (::Rails.application.config.active_record.belongs_to_required_by_default && reflection.options[:optional]) || (!::Rails.application.config.active_record.belongs_to_required_by_default && !reflection.options[:required])
"o|"
else
"||"
end
result[:Relations] << {
LeftModelName: model[:ModelName],
LeftValue: "}o",
Line: "--",
RightModelName: reflection_model_name,
RightValue: right_value,
Comment: "BT:#{reflection.name}"
}
end
end
defined_model.reflect_on_all_associations(:has_one).each do |reflection|
reflection_model_name = get_reflection_model_name(reflection)
next if ignored_model_names.key?(reflection_model_name)
reverse_relation = result[:Relations].find { |r|
if reflection.options[:through]
r[:RightModelName] == model[:ModelName] && r[:LeftModelName] == reflection_model_name && r[:Line] == ".."
else
r[:RightModelName] == model[:ModelName] && r[:LeftModelName] == reflection_model_name && r[:Line] == "--"
end
}
if reverse_relation
reverse_relation[:LeftValue] = "|o"
reverse_relation[:Comment] = if reflection.options[:through]
"#{reverse_relation[:Comment]}, HOT:#{reflection.name}"
else
"#{reverse_relation[:Comment]}, HO:#{reflection.name}"
end
else
result[:Relations] << {
LeftModelName: model[:ModelName],
LeftValue: reflection.options[:through] ? "}o" : "||",
Line: reflection.options[:through] ? ".." : "--",
RightModelName: reflection_model_name,
RightValue: "o|",
Comment: reflection.options[:through] ? "HOT:#{reflection.name}" : "HO:#{reflection.name}"
}
end
end
end
result
end
|