rails-plant_uml 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: 5a7c3a523233698db255ec7c48fac143292c8d797c164006c99671843438c764
4
+ data.tar.gz: 8870df85ab949000effb9049e0e4e8aa96ed973b92554d39b4a45acdd60701d5
5
+ SHA512:
6
+ metadata.gz: 66b9129bf10a625cc87058f0f7de5cff2da5c2d4be58b294ab064f4be5c94c9bfa5d55f818e9dc654b27f2a5ae05da4f55640bbe4f21fdfe212da6bcdb42c6da
7
+ data.tar.gz: a53486bfb65dd6a44fd62bd1ea13b9c5d9ebe99898a4e08790ff5c18fadbba6154e2943f3e06cdce794552030431b26f89c7b11bac4ba2109841882d29d23a6b
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+
5
+ module Rails
6
+ module PlantUml
7
+ class Railtie < Rails::Railtie
8
+ rake_tasks do
9
+ # add tasks from rails/plant_uml/tasks
10
+ load File.expand_path("../tasks/rails_erd.rake", __FILE__)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :erd do
4
+ desc "Generate a PlantUML diagram of your models"
5
+ task plant_uml: :environment do
6
+ require "rails/plant_uml"
7
+
8
+ options = {
9
+ filename: "erd.puml",
10
+ title: "Entity Relationship Diagram",
11
+ app_name: Rails.application.class.module_parent_name
12
+ }
13
+
14
+ diagram = RailsErd::PlantUml.new(RailsERD::Domain.generate, options)
15
+ diagram.save
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module PlantUml
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_erd"
4
+ require "rails_erd/diagram"
5
+ require "rails_erd/diagram/graphviz"
6
+ require "active_support/core_ext/string"
7
+ require "rails/plant_uml/railtie"
8
+
9
+ module RailsErd
10
+ # PlantUML implementation of the Diagram class, which will output a .puml file
11
+ # that can be processed by the PlantUML renderer.
12
+ class PlantUml < RailsERD::Diagram
13
+ setup do
14
+ @markup = []
15
+ @relationships = []
16
+ end
17
+
18
+ # Initialize the diagram
19
+ def initialize(domain, options = {})
20
+ @domain = domain
21
+ @title = options[:title] || "Entity Relationship Diagram"
22
+ @app_name = options[:app_name] || "SOFware"
23
+ @filename = options[:filename] || "erd.puml"
24
+ @filename += ".puml" unless @filename.end_with?(".puml")
25
+ @only = options[:only]
26
+ @timestamp = Time.current.iso8601
27
+ @markup = []
28
+ end
29
+
30
+ # Main method to generate the PlantUML diagram
31
+ def save
32
+ # Generate the PlantUML content
33
+ generate_diagram
34
+
35
+ # Write to file
36
+ File.write(@filename, @markup.join("\n"))
37
+
38
+ # Return the filename so it can be used in the CLI output
39
+ @filename
40
+ end
41
+
42
+ private
43
+
44
+ # Assemble the full PlantUML diagram
45
+ def generate_diagram
46
+ # Start the PlantUML diagram with a name based on the title
47
+ diagram_name = @title ? @title.gsub(/\s+/, "_") : "ERD"
48
+ @markup << "@startuml #{diagram_name}"
49
+
50
+ # Add title if provided
51
+ @markup << "title #{@title}".indent(2) if @title
52
+
53
+ # Add creation timestamp as a note
54
+ @markup << "note \"Generated by #{@app_name} on #{@timestamp}\" as GenerationNote".indent(2)
55
+
56
+ if @only
57
+ @domain.entities.select { |e| @only.include?(e.name) }
58
+ else
59
+ @domain.entities
60
+ end.each do |entity|
61
+ add_entity(entity)
62
+ end
63
+
64
+ if @only
65
+ @domain.relationships.select { |r| @only.include?(r.source.name) && @only.include?(r.destination.name) }
66
+ else
67
+ @domain.relationships
68
+ end.each do |relationship|
69
+ add_relationship(relationship)
70
+ end
71
+
72
+ # End the PlantUML diagram
73
+ @markup << "@enduml"
74
+ end
75
+
76
+ # Convert an entity (model) to PlantUML class notation
77
+ def add_entity(entity)
78
+ # Start class definition
79
+ @markup << "class #{entity.name} {".indent(2)
80
+
81
+ # Get foreign key attributes directly
82
+ foreign_key_attributes = entity.attributes.select { |attr| attr.foreign_key? }
83
+
84
+ # Only show foreign keys section if there are foreign keys
85
+ if foreign_key_attributes.any?
86
+ @markup << " .. Foreign Keys ..".indent(2)
87
+ foreign_key_attributes.each do |attribute|
88
+ @markup << " + #{attribute.name} : #{attribute.type}".indent(2)
89
+ end
90
+ end
91
+
92
+ # End class definition
93
+ @markup << "}".indent(2)
94
+ @markup << "" # Empty line for readability
95
+ end
96
+
97
+ # Convert a relationship to PlantUML relationship notation
98
+ def add_relationship(relationship)
99
+ source = relationship.source.name
100
+ destination = relationship.destination.name
101
+
102
+ # Get the Rails models to check their associations
103
+ source_model = begin
104
+ source.constantize
105
+ rescue
106
+ nil
107
+ end
108
+ destination_model = begin
109
+ destination.constantize
110
+ rescue
111
+ nil
112
+ end
113
+
114
+ # Default to the relationship cardinality from RailsERD
115
+ cardinality = relationship.cardinality
116
+
117
+ # Try to determine the relationship type from Rails models
118
+ if source_model && destination_model && source_model.is_a?(Class) && destination_model.is_a?(Class)
119
+ source_association = source_model.reflect_on_association(destination.underscore.to_sym)&.macro
120
+ destination_association = destination_model.reflect_on_association(source.underscore.to_sym)&.macro
121
+
122
+ cardinality = if ([source_association, destination_association].compact & [:belongs_to, :has_many]).present?
123
+ :many
124
+ elsif [source_association, destination_association].include? :has_one
125
+ :one
126
+ elsif source_model.reflect_on_association(destination.pluralize.underscore.to_sym)&.macro == :has_and_belongs_to_many ||
127
+ destination_model.reflect_on_association(source.pluralize.underscore.to_sym)&.macro == :has_and_belongs_to_many
128
+ :many_to_many
129
+ end
130
+ end
131
+
132
+ # Determine relationship type and convert to PlantUML arrow notation
133
+ @markup << case cardinality
134
+ when :one
135
+ # One-to-one relationship
136
+ "#{source} \"1\" -- \"1\" #{destination}".indent(2)
137
+ when :many
138
+ # One-to-many relationship
139
+ if relationship.source_optional?
140
+ "#{source} \"0..1\" -- \"*\" #{destination}".indent(2)
141
+ else
142
+ "#{source} \"1\" -- \"*\" #{destination}".indent(2)
143
+ end
144
+ when :many_to_many
145
+ # Many-to-many relationship
146
+ "#{source} \"*\" -- \"*\" #{destination}".indent(2)
147
+ else
148
+ # Default relationship
149
+ "#{source} -- #{destination}".indent(2)
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1 @@
1
+ require_relative "rails/plant_uml"
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-plant_uml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jim Gay
8
+ bindir: exe
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: '7.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rails-erd
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.7.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.7.0
40
+ description: Adds a PlantUML output format to RailsERD.
41
+ email:
42
+ - jim@saturnflyer.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/rails-plant_uml.rb
48
+ - lib/rails/plant_uml.rb
49
+ - lib/rails/plant_uml/railtie.rb
50
+ - lib/rails/plant_uml/tasks/rails_erd.rake
51
+ - lib/rails/plant_uml/version.rb
52
+ homepage: https://github.com/SOFware/rails-plant_uml
53
+ licenses: []
54
+ metadata:
55
+ homepage_uri: https://github.com/SOFware/rails-plant_uml
56
+ source_code_uri: https://github.com/SOFware/rails-plant_uml
57
+ changelog_uri: https://github.com/SOFware/rails-plant_uml/blob/main/CHANGELOG.md
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 3.1.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.6.7
73
+ specification_version: 4
74
+ summary: Tie into RailsERD to generate PlantUML diagrams.
75
+ test_files: []