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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d7286f0c8faf1b1a2110ddd83d629e60da4c30066e4628a9f4c7ef225735f63
4
- data.tar.gz: 9a523bf923177660563d8fb3ae3bdaebba962a3e8bd34f845eda598066659d92
3
+ metadata.gz: 555d1be09e597009a4331ff9f6ffc36f0fc2653c0c70308f0327f521cf98ced6
4
+ data.tar.gz: 5c345818a3d34674a1169ba538edbd22205c911a6820f58b3053da45ce92b046
5
5
  SHA512:
6
- metadata.gz: 9681fb9367802103df78dcc80e273644a5b900a55cbaf2f9aee2764589c0240e2382b54d01c1fe459f823ca6b55729eb21f5c210af8ae065fca81091bce733c3
7
- data.tar.gz: 69da66c4d72f8a891067b5eb1632e6984e7fc2027fd9ea4e478b8d51cd7c71fff5db3e51e128547c12c0397bf7d2fcccda2932afa84adf96c1b7d12fbfc3b995
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
- # Retrieve columns only when mode=='columns' to include details
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 (table) information
33
+ # Node info
35
34
  nodes << {
36
- id: model.name, # e.g., "User"
37
- columns: columns # e.g., [ ["id","integer"], ["name","string"], ... ]
35
+ id: sanitized_model_name, # e.g. "User" or "PaperTrail_Version"
36
+ columns: columns
38
37
  }
39
38
 
40
- # Gather associations as edges
39
+ # Build edges for each association
41
40
  model.reflect_on_all_associations.each do |assoc|
42
- # Polymorphic associations do not have a single definite class,
43
- # so we treat them separately
44
- if assoc.polymorphic?
45
- edges << { from: model.name, to: "Polymorphic(#{assoc.name})", type: assoc.macro.to_s }
46
- else
47
- edges << { from: model.name, to: assoc.klass.name, type: assoc.macro.to_s }
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
- # Build a Mermaid erDiagram notation string
57
- # https://mermaid.js.org/syntax/entityRelationshipDiagram.html
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
- # ER notation like "||--||" indicates multiplicity (1, n, etc.)
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
- # Map association types to ER diagram cardinality symbols:
101
- # - belongs_to 1-to-many from the other side, often "||--|{"
102
- # - has_many → 1-to-many
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.to_s
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
- "||--||" # Default to 1-to-1
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsModelViz
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_model_viz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Takahashi