rails_model_viz 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/rails_model_viz/graph_controller.rb +40 -49
- data/lib/rails_model_viz/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 555d1be09e597009a4331ff9f6ffc36f0fc2653c0c70308f0327f521cf98ced6
|
4
|
+
data.tar.gz: 5c345818a3d34674a1169ba538edbd22205c911a6820f58b3053da45ce92b046
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bd9f1de678047d167a147a77b29f6ac09e023013b7f4c95f6ea79a0adca34663d5cbd7d93d77d62d5e3cf845f9e76223aaf3e148a58ae8089f31db5a708e3cb
|
7
|
+
data.tar.gz: 6f15e86bf00f35bba4e497207837cf03c805fcbfaad2ba8eea0a9f9ab63279f53031eb961d7bd0c3a9da6de463fd4743881db3503b94dfa25df426ee34fab8b1
|
@@ -6,7 +6,6 @@ module RailsModelViz
|
|
6
6
|
@mode = params[:mode] || 'relations'
|
7
7
|
models = ApplicationRecord.descendants
|
8
8
|
|
9
|
-
# Generate text for Mermaid's ER diagram
|
10
9
|
@mermaid_text = build_mermaid_text(models, @mode)
|
11
10
|
end
|
12
11
|
|
@@ -18,78 +17,64 @@ module RailsModelViz
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def build_graph_data(models, mode)
|
21
|
-
# For erDiagram, we assemble the list of columns each model has
|
22
|
-
# and information about their associations
|
23
20
|
nodes = []
|
24
|
-
edges =
|
21
|
+
edges = Set.new # Use a Set to avoid duplicates
|
25
22
|
|
26
23
|
models.each do |model|
|
27
|
-
|
24
|
+
sanitized_model_name = sanitize_model_name(model.name)
|
25
|
+
|
26
|
+
# Get columns if mode == 'columns'
|
28
27
|
columns = []
|
29
28
|
if mode == 'columns'
|
30
29
|
model.load_schema
|
31
30
|
columns = model.columns.map { |col| [col.name, col.type.to_s] }
|
32
31
|
end
|
33
32
|
|
34
|
-
# Node
|
33
|
+
# Node info
|
35
34
|
nodes << {
|
36
|
-
id:
|
37
|
-
columns: columns
|
35
|
+
id: sanitized_model_name, # e.g. "User" or "PaperTrail_Version"
|
36
|
+
columns: columns
|
38
37
|
}
|
39
38
|
|
40
|
-
#
|
39
|
+
# Build edges for each association
|
41
40
|
model.reflect_on_all_associations.each do |assoc|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
41
|
+
from_name = sanitized_model_name
|
42
|
+
to_class = assoc.polymorphic? ? "Polymorphic(#{assoc.name})" : assoc.klass.name
|
43
|
+
to_name = sanitize_model_name(to_class)
|
44
|
+
rel_type = assoc.macro.to_s # "has_many", "belongs_to", etc.
|
45
|
+
|
46
|
+
# OPTIONAL: Skip self-referencing if not desired
|
47
|
+
# next if from_name == to_name
|
48
|
+
|
49
|
+
# Build a unique key so we don't add duplicates
|
50
|
+
edge_key = [from_name, to_name, rel_type]
|
51
|
+
unless edges.include?(edge_key)
|
52
|
+
edges << edge_key
|
48
53
|
end
|
49
54
|
end
|
50
55
|
end
|
51
56
|
|
52
|
-
{ nodes: nodes, edges: edges }
|
57
|
+
{ nodes: nodes, edges: edges.to_a } # convert Set back to an Array for rendering
|
53
58
|
end
|
54
59
|
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
# erDiagram
|
60
|
-
# TABLE_NAME {
|
61
|
-
# column_name data_type
|
62
|
-
# column_name data_type
|
63
|
-
# }
|
64
|
-
#
|
65
|
-
# TABLE_NAME ||--|{ OTHER_TABLE : "relationship"
|
66
|
-
# ===========================================================================
|
60
|
+
# ------------------------------------------------------
|
61
|
+
# Convert our graph data into Mermaid 'erDiagram' format
|
62
|
+
# ------------------------------------------------------
|
67
63
|
def to_mermaid_er_diagram(graph_data)
|
68
64
|
mermaid = "erDiagram\n"
|
69
65
|
|
70
66
|
# (1) Define each table (entity)
|
71
67
|
graph_data[:nodes].each do |node|
|
72
68
|
mermaid << " #{node[:id]} {\n"
|
73
|
-
|
74
|
-
# Output field definitions only if node[:columns] is present
|
75
|
-
node[:columns].each do |(col_name, col_type)|
|
76
|
-
# For example: "id integer", "name string", etc.
|
69
|
+
node[:columns].each do |col_name, col_type|
|
77
70
|
mermaid << " #{col_name} #{col_type}\n"
|
78
71
|
end
|
79
|
-
|
80
72
|
mermaid << " }\n\n"
|
81
73
|
end
|
82
74
|
|
83
|
-
# (2) Define relationships
|
84
|
-
|
85
|
-
graph_data[:edges].each do |edge|
|
86
|
-
from = edge[:from]
|
87
|
-
to = edge[:to]
|
88
|
-
rel_type = edge[:type] # e.g. "has_many", "belongs_to"
|
89
|
-
|
90
|
-
# Convert association type to an ER diagram multiplicity symbol
|
75
|
+
# (2) Define relationships (edges)
|
76
|
+
graph_data[:edges].each do |(from, to, rel_type)|
|
91
77
|
cardinality = map_assoc_to_er_cardinality(rel_type)
|
92
|
-
|
93
78
|
# Example: User ||--|{ Post : "has_many"
|
94
79
|
mermaid << " #{from} #{cardinality} #{to} : \"#{rel_type}\"\n"
|
95
80
|
end
|
@@ -97,13 +82,11 @@ module RailsModelViz
|
|
97
82
|
mermaid
|
98
83
|
end
|
99
84
|
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
# - has_one → 1-to-1
|
104
|
-
# - has_and_belongs_to_many → many-to-many
|
85
|
+
# ------------------------------------------------------
|
86
|
+
# Map Rails association to Mermaid ER cardinality symbol
|
87
|
+
# ------------------------------------------------------
|
105
88
|
def map_assoc_to_er_cardinality(rel_type)
|
106
|
-
case rel_type
|
89
|
+
case rel_type
|
107
90
|
when "belongs_to"
|
108
91
|
"||--|{"
|
109
92
|
when "has_many"
|
@@ -113,8 +96,16 @@ module RailsModelViz
|
|
113
96
|
when "has_and_belongs_to_many"
|
114
97
|
"}o--o{"
|
115
98
|
else
|
116
|
-
"||--||" #
|
99
|
+
"||--||" # default to 1-to-1
|
117
100
|
end
|
118
101
|
end
|
102
|
+
|
103
|
+
# ------------------------------------------------
|
104
|
+
# Sanitize model/association names for Mermaid
|
105
|
+
# Replace :: with _ to prevent syntax errors
|
106
|
+
# ------------------------------------------------
|
107
|
+
def sanitize_model_name(name)
|
108
|
+
name.gsub("::", "_")
|
109
|
+
end
|
119
110
|
end
|
120
111
|
end
|