rails_model_viz 0.1.0
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 +7 -0
- data/ CHANGELOG.md +0 -0
- data/.rspec +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +8 -0
- data/app/controllers/rails_model_viz/graph_controller.rb +120 -0
- data/app/views/rails_model_viz/graph/index.html.erb +74 -0
- data/config/routes.rb +4 -0
- data/docs/img/columns.png +0 -0
- data/docs/img/relations.png +0 -0
- data/lib/rails_model_viz/engine.rb +5 -0
- data/lib/rails_model_viz/version.rb +5 -0
- data/lib/rails_model_viz.rb +8 -0
- data/sig/rails_model_viz.rbs +4 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6d7286f0c8faf1b1a2110ddd83d629e60da4c30066e4628a9f4c7ef225735f63
|
4
|
+
data.tar.gz: 9a523bf923177660563d8fb3ae3bdaebba962a3e8bd34f845eda598066659d92
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9681fb9367802103df78dcc80e273644a5b900a55cbaf2f9aee2764589c0240e2382b54d01c1fe459f823ca6b55729eb21f5c210af8ae065fca81091bce733c3
|
7
|
+
data.tar.gz: 69da66c4d72f8a891067b5eb1632e6984e7fc2027fd9ea4e478b8d51cd7c71fff5db3e51e128547c12c0397bf7d2fcccda2932afa84adf96c1b7d12fbfc3b995
|
data/ CHANGELOG.md
ADDED
File without changes
|
data/.rspec
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Seiya Takahashi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# RailsModelViz
|
2
|
+
|
3
|
+
RailsModelViz is a Rails engine (and optional rake task) that visualizes your ActiveRecord models and associations using Mermaid.js. It automatically generates diagrams of your models’relationships (and optionally columns) in a clear and interactive format.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
1. Add this line to your application’s Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rails_model_viz'
|
11
|
+
```
|
12
|
+
|
13
|
+
2. Then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
1. Mount the Rails Engine
|
22
|
+
|
23
|
+
In your main application’s config/routes.rb, mount the engine so you can access the visualization:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Rails.application.routes.draw do
|
27
|
+
|
28
|
+
# Other routes...
|
29
|
+
if Rails.env.development?
|
30
|
+
mount RailsModelViz::Engine, at: '/rails_model_viz'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
After this, start your Rails server and navigate to:
|
36
|
+
|
37
|
+
http://localhost:3000/rails_model_viz
|
38
|
+
|
39
|
+
You should see a Mermaid-based diagram of your models’ relationships.
|
40
|
+
|
41
|
+

|
42
|
+
|
43
|
+
2. Toggle Columns View
|
44
|
+
|
45
|
+
By default, RailsModelViz may show only associations. If you’d like to view columns as well, you can pass a mode=columns parameter. For example:
|
46
|
+
|
47
|
+
http://localhost:3000/rails_model_viz?mode=columns
|
48
|
+
|
49
|
+
This will enrich each entity in the diagram with column names (and types).
|
50
|
+
|
51
|
+

|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/PeterTakahashi/rails_model_viz. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the MIT License.
|
60
|
+
|
61
|
+
Feel free to add more sections (e.g., advanced configuration, known issues, screenshots, etc.) as you see fit!
|
data/Rakefile
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
module RailsModelViz
|
2
|
+
class GraphController < ActionController::Base
|
3
|
+
def index
|
4
|
+
Rails.application.eager_load!
|
5
|
+
|
6
|
+
@mode = params[:mode] || 'relations'
|
7
|
+
models = ApplicationRecord.descendants
|
8
|
+
|
9
|
+
# Generate text for Mermaid's ER diagram
|
10
|
+
@mermaid_text = build_mermaid_text(models, @mode)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def build_mermaid_text(models, mode)
|
16
|
+
graph_data = build_graph_data(models, mode)
|
17
|
+
to_mermaid_er_diagram(graph_data)
|
18
|
+
end
|
19
|
+
|
20
|
+
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
|
+
nodes = []
|
24
|
+
edges = []
|
25
|
+
|
26
|
+
models.each do |model|
|
27
|
+
# Retrieve columns only when mode=='columns' to include details
|
28
|
+
columns = []
|
29
|
+
if mode == 'columns'
|
30
|
+
model.load_schema
|
31
|
+
columns = model.columns.map { |col| [col.name, col.type.to_s] }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Node (table) information
|
35
|
+
nodes << {
|
36
|
+
id: model.name, # e.g., "User"
|
37
|
+
columns: columns # e.g., [ ["id","integer"], ["name","string"], ... ]
|
38
|
+
}
|
39
|
+
|
40
|
+
# Gather associations as edges
|
41
|
+
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 }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
{ nodes: nodes, edges: edges }
|
53
|
+
end
|
54
|
+
|
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
|
+
# ===========================================================================
|
67
|
+
def to_mermaid_er_diagram(graph_data)
|
68
|
+
mermaid = "erDiagram\n"
|
69
|
+
|
70
|
+
# (1) Define each table (entity)
|
71
|
+
graph_data[:nodes].each do |node|
|
72
|
+
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.
|
77
|
+
mermaid << " #{col_name} #{col_type}\n"
|
78
|
+
end
|
79
|
+
|
80
|
+
mermaid << " }\n\n"
|
81
|
+
end
|
82
|
+
|
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
|
91
|
+
cardinality = map_assoc_to_er_cardinality(rel_type)
|
92
|
+
|
93
|
+
# Example: User ||--|{ Post : "has_many"
|
94
|
+
mermaid << " #{from} #{cardinality} #{to} : \"#{rel_type}\"\n"
|
95
|
+
end
|
96
|
+
|
97
|
+
mermaid
|
98
|
+
end
|
99
|
+
|
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
|
105
|
+
def map_assoc_to_er_cardinality(rel_type)
|
106
|
+
case rel_type.to_s
|
107
|
+
when "belongs_to"
|
108
|
+
"||--|{"
|
109
|
+
when "has_many"
|
110
|
+
"||--|{"
|
111
|
+
when "has_one"
|
112
|
+
"||--||"
|
113
|
+
when "has_and_belongs_to_many"
|
114
|
+
"}o--o{"
|
115
|
+
else
|
116
|
+
"||--||" # Default to 1-to-1
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8">
|
5
|
+
<title>Rails Model Viz (Mermaid)</title>
|
6
|
+
<!-- Example using Mermaid's CDN -->
|
7
|
+
<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
|
8
|
+
|
9
|
+
<style>
|
10
|
+
/* Header design */
|
11
|
+
h1 {
|
12
|
+
display: flex;
|
13
|
+
justify-content: space-between; /* Place items at both ends horizontally */
|
14
|
+
align-items: center;
|
15
|
+
background: #f5f5f5;
|
16
|
+
padding: 10px 15px;
|
17
|
+
margin: 0 0 20px 0; /* Add bottom spacing */
|
18
|
+
border-bottom: 2px solid #ccc;
|
19
|
+
font-family: sans-serif;
|
20
|
+
}
|
21
|
+
|
22
|
+
/* Toggle button style */
|
23
|
+
.toggle-button {
|
24
|
+
text-decoration: none;
|
25
|
+
color: #333;
|
26
|
+
background: #e0e0e0;
|
27
|
+
padding: 6px 10px;
|
28
|
+
border-radius: 4px;
|
29
|
+
font-size: 14px;
|
30
|
+
border: 1px solid #ccc;
|
31
|
+
transition: background 0.2s;
|
32
|
+
}
|
33
|
+
|
34
|
+
.toggle-button:hover {
|
35
|
+
background: #ccc;
|
36
|
+
}
|
37
|
+
|
38
|
+
/* Optional additional styling for the Mermaid container */
|
39
|
+
.mermaid {
|
40
|
+
margin: 0 20px;
|
41
|
+
}
|
42
|
+
</style>
|
43
|
+
</head>
|
44
|
+
<body>
|
45
|
+
<!-- Title + Toggle button -->
|
46
|
+
<h1>
|
47
|
+
Rails Model Visualization (Mermaid)
|
48
|
+
|
49
|
+
<!-- Display links to switch modes -->
|
50
|
+
<% if params[:mode] == 'columns' %>
|
51
|
+
<!-- Currently in "columns" mode: switch to "relations only" -->
|
52
|
+
<a class="toggle-button" href="<%= graph_index_path(mode: 'relations') %>">
|
53
|
+
Show relations only
|
54
|
+
</a>
|
55
|
+
<% else %>
|
56
|
+
<!-- Currently in "relations only" mode: switch to "columns" -->
|
57
|
+
<a class="toggle-button" href="<%= graph_index_path(mode: 'columns') %>">
|
58
|
+
Show columns
|
59
|
+
</a>
|
60
|
+
<% end %>
|
61
|
+
</h1>
|
62
|
+
|
63
|
+
<!-- Mermaid rendering area -->
|
64
|
+
<!-- Insert the @mermaid_text generated by the controller -->
|
65
|
+
<div class="mermaid">
|
66
|
+
<%= @mermaid_text %>
|
67
|
+
</div>
|
68
|
+
|
69
|
+
<!-- Mermaid initialization script -->
|
70
|
+
<script>
|
71
|
+
mermaid.initialize({ startOnLoad: true });
|
72
|
+
</script>
|
73
|
+
</body>
|
74
|
+
</html>
|
data/config/routes.rb
ADDED
Binary file
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_model_viz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Takahashi
|
8
|
+
- Seiya Takahashi
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2025-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: "**Rails Model Viz** is a development tool that automatically visualizes
|
15
|
+
your Rails application's models, \nassociations, and (optionally) column details
|
16
|
+
using Mermaid.js. This gem provides both a command-line task \nand a Rails Engine
|
17
|
+
interface, allowing you to view interactive entity-relationship diagrams (ER diagrams)
|
18
|
+
\ndirectly in your browser. It helps developers quickly understand, explore, and
|
19
|
+
document the structure of \ntheir ActiveRecord models.\n"
|
20
|
+
email:
|
21
|
+
- seiya4@icloud.com
|
22
|
+
executables: []
|
23
|
+
extensions: []
|
24
|
+
extra_rdoc_files: []
|
25
|
+
files:
|
26
|
+
- " CHANGELOG.md"
|
27
|
+
- ".rspec"
|
28
|
+
- LICENSE.txt
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- app/controllers/rails_model_viz/graph_controller.rb
|
32
|
+
- app/views/rails_model_viz/graph/index.html.erb
|
33
|
+
- config/routes.rb
|
34
|
+
- docs/img/columns.png
|
35
|
+
- docs/img/relations.png
|
36
|
+
- lib/rails_model_viz.rb
|
37
|
+
- lib/rails_model_viz/engine.rb
|
38
|
+
- lib/rails_model_viz/version.rb
|
39
|
+
- sig/rails_model_viz.rbs
|
40
|
+
homepage: https://github.com/PeterTakahashi/rails_model_viz
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://github.com/PeterTakahashi/rails_model_viz
|
45
|
+
source_code_uri: https://github.com/PeterTakahashi/rails_model_viz
|
46
|
+
changelog_uri: https://github.com/PeterTakahashi/rails_model_viz/blob/main/CHANGELOG.md
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
- app
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '2.6'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.5.11
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: A Rails engine that visualizes your ActiveRecord models and associations
|
67
|
+
using Mermaid.js.
|
68
|
+
test_files: []
|