mermaid_erd 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4173a04658fdd3df40665fad885d894fef4fa18360f6042957ab649af1f9a740
4
+ data.tar.gz: 83857a699138a58476968f44e221a9b268f0973e3fa11ae2709141397972e3b3
5
+ SHA512:
6
+ metadata.gz: 68e1cc8ba51f13ac889c7e73f8850b4bf732245f3f6330b75bae224b69026b5f5e49bbc4357d20c0e7b963b0b0665446b1866b4398b8bedb53ee4a7a86b86876
7
+ data.tar.gz: 9b1bdfea5aa64c9053193bd374ee53971575278522c7e906b787073782233cc030a00a64354f037b3b7f8092281a2ce350ccb717b4331f0762647c3476edf9df
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-12-18
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Avin Hurry
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,173 @@
1
+ # MermaidErd
2
+
3
+ Generate Mermaid-compatible Entity Relationship Diagrams (ERDs) from your Rails models, with support for excluding specific models. Designed for modern Rails apps using Active Record.
4
+
5
+ ## Features
6
+
7
+ - Outputs a Mermaid `erDiagram` to `documentation/domain-model.md`
8
+ - Includes all Active Record models with a database table
9
+ - Excludes models listed in `config/mermaid_erd.yml` under `exclude`
10
+ - Visualises `belongs_to` associations
11
+ - Run via a Rake task (`rake erd:generate`)
12
+ - Adds a post-migration reminder in development
13
+
14
+ ## Requirements
15
+
16
+ - Ruby 3.4+
17
+ - Rails 8+
18
+ - PostgreSQL (uses array/enum column support)
19
+
20
+ ## Installation
21
+
22
+ Add this line to the development section of your Gemfile:
23
+
24
+ ```ruby
25
+ gem "mermaid_erd"
26
+ ```
27
+
28
+ Then install:
29
+
30
+ ```bash
31
+ bundle install
32
+ ```
33
+
34
+ Alternatively, install it directly:
35
+
36
+ ```bash
37
+ gem install mermaid_erd
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ Run the following Rake task to generate the ERD:
43
+
44
+ ```bash
45
+ bundle exec rake erd:generate
46
+ ```
47
+
48
+ This will output a Mermaid diagram to:
49
+
50
+ ```
51
+ documentation/domain-model.md
52
+ ```
53
+
54
+ ### Customization
55
+
56
+ You can customize which models to exclude by creating a file at:
57
+
58
+ ```yaml
59
+ # config/mermaid_erd.yml
60
+
61
+ exclude:
62
+ - ActiveStorage::*
63
+ - SolidQueue::*
64
+ - Blazer::*
65
+ ```
66
+ You can limit to specific models:
67
+
68
+ ```yaml
69
+ # config/mermaid_erd.yml
70
+
71
+ only:
72
+ - Order
73
+ - Customer
74
+ - Product
75
+ ```
76
+
77
+ ### Post-Migration Reminder
78
+
79
+ To remind developers to regenerate the diagram after each migration, this gem automatically hooks into `db:migrate` and prints a message like:
80
+
81
+ ```
82
+ [ℹ] If this migration added or modified database tables, consider:
83
+ • Updating the Mermaid ER diagram: bundle exec rake erd:generate
84
+ • Excluding models in: config/mermaid_erd.yml
85
+ ```
86
+
87
+ > ⚠ This task only runs in the development environment.
88
+
89
+ ## Output Example
90
+
91
+ Example output (`documentation/domain-model.md`):
92
+
93
+ ```mermaid
94
+ erDiagram
95
+ Saiyan {
96
+ integer id
97
+ string name
98
+ enum rank
99
+ integer power_level
100
+ boolean tail
101
+ datetime born_at
102
+ datetime created_at
103
+ datetime updated_at
104
+ }
105
+
106
+ Planet {
107
+ integer id
108
+ string name
109
+ boolean destroyed
110
+ datetime created_at
111
+ datetime updated_at
112
+ }
113
+
114
+ Transformation {
115
+ integer id
116
+ integer saiyan_id
117
+ enum form
118
+ integer power_multiplier
119
+ datetime achieved_at
120
+ datetime created_at
121
+ datetime updated_at
122
+ }
123
+
124
+ Battle {
125
+ integer id
126
+ string location
127
+ enum outcome
128
+ datetime started_at
129
+ datetime finished_at
130
+ datetime created_at
131
+ datetime updated_at
132
+ }
133
+
134
+ BattleParticipation {
135
+ integer id
136
+ integer battle_id
137
+ integer fighter_id
138
+ enum fighter_type
139
+ integer damage_dealt
140
+ boolean defeated
141
+ datetime created_at
142
+ datetime updated_at
143
+ }
144
+
145
+ Sensei {
146
+ integer id
147
+ string name
148
+ string technique_speciality
149
+ datetime created_at
150
+ datetime updated_at
151
+ }
152
+
153
+ TrainingSession {
154
+ integer id
155
+ integer saiyan_id
156
+ integer sensei_id
157
+ integer planet_id
158
+ integer duration_days
159
+ integer power_gained
160
+ datetime started_at
161
+ datetime finished_at
162
+ datetime created_at
163
+ datetime updated_at
164
+ }
165
+
166
+ Saiyan }o--|| Planet : belongs_to
167
+ Transformation }o--|| Saiyan : belongs_to
168
+ TrainingSession }o--|| Saiyan : belongs_to
169
+ TrainingSession }o--|| Sensei : belongs_to
170
+ TrainingSession }o--|| Planet : belongs_to
171
+ BattleParticipation }o--|| Battle : belongs_to
172
+ BattleParticipation }o--|| Saiyan : belongs_to
173
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Generates a Mermaid ERD to documentation/domain-model.md
4
+ # Excludes models listed in config/mermaid_erd.yml under `exclude`
5
+
6
+ module MermaidErd
7
+ class Generator
8
+ def initialize(config_path:, output_path:)
9
+ @config_path = config_path
10
+ @output_path = output_path
11
+ config = load_config(config_path)
12
+ @exclusions = config.fetch("exclude", [])
13
+ @inclusions = parse_inclusions(config)
14
+ end
15
+
16
+ def generate
17
+ raise "[⚠] Mermaid ERD generation is only allowed in development environment" unless Rails.env.development?
18
+
19
+ Rails.application.eager_load!
20
+ models = load_models
21
+ lines = [ "```mermaid", "erDiagram" ]
22
+
23
+ models.each do |model|
24
+ next unless model.table_exists?
25
+
26
+ sanitized_name = sanitize(model.name)
27
+ lines << " #{sanitized_name} {"
28
+ model.columns.each do |column|
29
+ base_type = column.type.to_s
30
+ type = column.array ? "array[#{base_type}]" : base_type
31
+ lines << " #{type} #{column.name}"
32
+ end
33
+
34
+ lines << " }"
35
+
36
+ model.reflect_on_all_associations(:belongs_to).each do |assoc|
37
+ next if assoc.polymorphic?
38
+
39
+ klass_name = assoc.class_name.to_s.strip
40
+ next if klass_name.empty?
41
+
42
+ to = sanitize(klass_name)
43
+ next if excluded?(klass_name)
44
+ next unless included?(klass_name)
45
+
46
+ lines << " #{sanitized_name} }o--|| #{to} : belongs_to"
47
+ end
48
+ end
49
+
50
+ lines << "```"
51
+ FileUtils.mkdir_p(@output_path.dirname)
52
+ File.write(@output_path, lines.join("\n"))
53
+ end
54
+
55
+ private
56
+
57
+ def sanitize(name)
58
+ name.gsub("::", "_")
59
+ end
60
+
61
+ def excluded?(model_name)
62
+ @exclusions.any? { |pattern| File.fnmatch(pattern, model_name) }
63
+ end
64
+
65
+ def included?(model_name)
66
+ return true if @inclusions.empty?
67
+
68
+ @inclusions.any? { |pattern| File.fnmatch(pattern, model_name) }
69
+ end
70
+
71
+ def load_models
72
+ ActiveRecord::Base.descendants.reject do |model|
73
+ model.abstract_class? ||
74
+ excluded?(model.name) ||
75
+ !included?(model.name) ||
76
+ model.name.start_with?("HABTM_")
77
+ end
78
+ end
79
+
80
+ def parse_inclusions(config)
81
+ only = config["only"]
82
+
83
+ case only
84
+ when String
85
+ only.split(",").map(&:strip).reject(&:empty?)
86
+ when Array
87
+ only.map(&:to_s).map(&:strip).reject(&:empty?)
88
+ else
89
+ []
90
+ end
91
+ end
92
+
93
+ def load_config(config_path)
94
+ return {} unless File.exist?(config_path)
95
+
96
+ YAML.safe_load_file(config_path, aliases: true) || {}
97
+ rescue Psych::SyntaxError => e
98
+ raise "YAML syntax error in #{config_path}: #{e.message}"
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MermaidErd
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load File.expand_path("../tasks/mermaid_erd.rake", __dir__)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MermaidErd
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mermaid_erd/version"
4
+ require_relative "mermaid_erd/generator"
5
+ require_relative "mermaid_erd/railtie" if defined?(Rails::Railtie)
6
+
7
+ module MermaidErd
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :erd do
4
+ desc "Generate Mermaid entity relationship diagram"
5
+ task generate: :environment do
6
+ unless Rails.env.development?
7
+ puts "[⚠] Mermaid ERD generation is only allowed in development environment"
8
+ next
9
+ end
10
+
11
+ require "mermaid_erd/generator"
12
+
13
+ config_path = Rails.root.join("config/mermaid_erd.yml")
14
+ output_path = Rails.root.join("documentation/domain-model.md")
15
+
16
+ MermaidErd::Generator.new(
17
+ config_path:,
18
+ output_path:
19
+ ).generate
20
+
21
+ puts "[✔] Mermaid ERD diagram added to: #{output_path.relative_path_from(Rails.root)}"
22
+ end
23
+
24
+ namespace :post_migrate do
25
+ desc "Show a reminder to update the Mermaid ER diagram after migrations"
26
+ task reminder: :environment do
27
+ if $stdout.tty?
28
+ cyan = "\e[36m"
29
+ bold = "\e[1m"
30
+ reset = "\e[0m"
31
+ else
32
+ cyan = bold = reset = ""
33
+ end
34
+
35
+ puts "\n#{bold}#{cyan}[ℹ] If this migration added or modified database tables, consider:#{reset}"
36
+ puts " • Updating the Mermaid ER diagram: #{bold}bundle exec rake erd:generate#{reset}"
37
+ puts " • Excluding models in: #{bold}config/mermaid_erd.yml#{reset}\n\n"
38
+ end
39
+ end
40
+ end
41
+
42
+ if Rails.env.development?
43
+ Rake::Task["db:migrate"].enhance do
44
+ Rake::Task["erd:post_migrate:reminder"].invoke
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mermaid_erd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Avin Hurry
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '8.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '8.0'
26
+ description: Generate Mermaid ER diagrams from Rails models as Markdown-friendly output
27
+ email: []
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - CHANGELOG.md
33
+ - CODE_OF_CONDUCT.md
34
+ - LICENSE.txt
35
+ - README.md
36
+ - Rakefile
37
+ - lib/mermaid_erd.rb
38
+ - lib/mermaid_erd/generator.rb
39
+ - lib/mermaid_erd/railtie.rb
40
+ - lib/mermaid_erd/version.rb
41
+ - lib/tasks/mermaid_erd.rake
42
+ homepage: https://github.com/avinhurry/mermaid_erd
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ allowed_push_host: https://rubygems.org
47
+ source_code_uri: https://github.com/avinhurry/mermaid_erd
48
+ changelog_uri: https://github.com/avinhurry/mermaid_erd/blob/main/CHANGELOG.md
49
+ rubygems_mfa_required: 'true'
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 3.4.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.7.2
65
+ specification_version: 4
66
+ summary: Generate Mermaid ER diagrams for Rails models in Markdown
67
+ test_files: []