rails-erd 2.0.0 → 2.0.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: 9dadf622a2d2bcae2e4961d23b1f86647b7eaf4e68ae062dad5a18050b2fc935
4
- data.tar.gz: 72115211691f90710ccdfea75c6b8456bae393cc1794b73727006ee3ec7475af
3
+ metadata.gz: 96a5321997e7b69697914d2555515de577676734e66f60cf0b76ff6c6f307846
4
+ data.tar.gz: 973f6409aeb4fcd221659e4c9dabadd4a32a047218ea056b4b24b9c71476454c
5
5
  SHA512:
6
- metadata.gz: f19813191fc9a1258053cab237535fb5358e2a804af4bf394bcae0eb1e2b743c1d48c284d100b2044bed76aa57e8f95cd998379e84fc16de9cecbf452d29d816
7
- data.tar.gz: b77b4b3e57436b9a32fe92cfec304aa35c8f094fc0c00d615f01c394fae6680f899eab1f548d50986a61534372b48b661d656638502ad1bdad5bc4da3757c139
6
+ metadata.gz: c5b98489174be741cf8056040f3a6eadd2c5ab673dfb9463b2db0b4fe7e9209a742765464b02c842b859c700231285cae40cfbcbe4eca3bed24d3cf1a191b453
7
+ data.tar.gz: 3793cbe2da98c90a4e379864e7dd5e3d4072248ff8245dfa5dfc75d0957e9c9e1f6aab4140ec54af00010312c53791e513e3898b41e2669c365802151497d9aa
@@ -20,7 +20,8 @@ module RailsERD
20
20
  each_entity do |entity, attributes|
21
21
  if er_diagram?
22
22
  # Build entity block as a single string to avoid uniq issues with closing braces
23
- entity_lines = ["\t#{entity} {"]
23
+ quoted_entity = quote_entity_name(entity)
24
+ entity_lines = ["\t#{quoted_entity} {"]
24
25
  attributes.each do |attr|
25
26
  key_marker = attribute_key_marker(attr)
26
27
  entity_lines << "\t\t#{attr.type} #{attr.name}#{key_marker}"
@@ -39,7 +40,7 @@ module RailsERD
39
40
  from, to = specialization.generalized, specialization.specialized
40
41
  if er_diagram?
41
42
  # erDiagram doesn't have a direct polymorphic notation, use inheritance-like
42
- graph << "\t#{from.name} ||--o{ #{to.name} : \"specializes\""
43
+ graph << "\t#{quote_entity_name(from.name)} ||--o{ #{quote_entity_name(to.name)} : \"specializes\""
43
44
  else
44
45
  graph << "\t<<polymorphic>> `#{specialization.generalized}`"
45
46
  graph << "\t #{from.name} <|-- #{to.name}"
@@ -51,14 +52,14 @@ module RailsERD
51
52
  next unless from && to
52
53
 
53
54
  if er_diagram?
54
- graph << "\t#{from.name} #{er_relation_notation(relationship)} #{to.name} : \"\""
55
+ graph << "\t#{quote_entity_name(from.name)} #{er_relation_notation(relationship)} #{quote_entity_name(to.name)} : \"\""
55
56
 
56
57
  from.children.each do |child|
57
- graph << "\t#{child.name} #{er_relation_notation(relationship)} #{to.name} : \"\""
58
+ graph << "\t#{quote_entity_name(child.name)} #{er_relation_notation(relationship)} #{quote_entity_name(to.name)} : \"\""
58
59
  end
59
60
 
60
61
  to.children.each do |child|
61
- graph << "\t#{from.name} #{er_relation_notation(relationship)} #{child.name} : \"\""
62
+ graph << "\t#{quote_entity_name(from.name)} #{er_relation_notation(relationship)} #{quote_entity_name(child.name)} : \"\""
62
63
  end
63
64
  else
64
65
  graph << "\t`#{from.name}` #{relation_arrow(relationship)} `#{to.name}`"
@@ -92,6 +93,17 @@ module RailsERD
92
93
  options[:mermaid_style] == :erdiagram || options[:mermaid_style] == :er
93
94
  end
94
95
 
96
+ # Quote entity names that contain special characters (like :: for namespaces)
97
+ # Mermaid erDiagram requires double quotes for names with special characters
98
+ def quote_entity_name(name)
99
+ name = name.to_s
100
+ if name.include?("::")
101
+ %("#{name}")
102
+ else
103
+ name
104
+ end
105
+ end
106
+
95
107
  # erDiagram relationship notation using crow's foot
96
108
  # Format: left_cardinality--right_cardinality
97
109
  # Cardinality markers:
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsERD
4
- VERSION = "2.0.0"
4
+ VERSION = "2.0.1"
5
5
  BANNER = "RailsERD #{VERSION}"
6
6
  end
data/test/test_helper.rb CHANGED
@@ -2,6 +2,7 @@ require "rubygems"
2
2
  require "bundler/setup"
3
3
  require 'pry'
4
4
  require 'pry-nav'
5
+ require 'tmpdir'
5
6
 
6
7
  require "active_record"
7
8
 
@@ -60,14 +61,17 @@ class ActiveSupport::TestCase
60
61
  superklass = args.first.kind_of?(Class) ? args.shift : ActiveRecord::Base
61
62
 
62
63
  names = full_name.split('::')
64
+ name = names.pop # Remove and store the model name
63
65
 
64
- parent_module = names[0..-1].inject(Object) do |parent,child|
65
- parent = parent.const_set(child.to_sym, Module.new)
66
+ # Create namespace modules (all parts except the model name)
67
+ parent_module = names.inject(Object) do |parent, child|
68
+ if parent.const_defined?(child.to_sym, false)
69
+ parent.const_get(child.to_sym)
70
+ else
71
+ parent.const_set(child.to_sym, Module.new)
72
+ end
66
73
  end
67
74
 
68
- parent_module ||= Object
69
- name = names.last
70
-
71
75
  columns = args.first || {}
72
76
  klass = parent_module.const_set name.to_sym, Class.new(superklass)
73
77
  konstant = parent_module.const_get(name.to_sym)
@@ -173,6 +177,9 @@ class ActiveSupport::TestCase
173
177
  File.expand_path("../../examples/erdconfig.not_exists", __FILE__)
174
178
 
175
179
  RailsERD.options = RailsERD.default_options.merge(Config.load)
180
+
181
+ # Use temp directory for test output to avoid polluting the project root
182
+ RailsERD.options.filename = File.join(Dir.tmpdir, "rails_erd_test_erd")
176
183
  end
177
184
 
178
185
  def name_to_object_symbol_pairs(name)
@@ -7,10 +7,6 @@ class GraphvizTest < ActiveSupport::TestCase
7
7
  RailsERD.options.warn = false
8
8
  end
9
9
 
10
- def teardown
11
- FileUtils.rm Dir["erd*.*"] rescue nil
12
- end
13
-
14
10
  def diagram(options = {})
15
11
  @diagram ||= Diagram::Graphviz.new(Domain.generate(options), options).tap do |diagram|
16
12
  diagram.generate
@@ -52,11 +48,8 @@ class GraphvizTest < ActiveSupport::TestCase
52
48
  # Diagram properties =======================================================
53
49
  test "file name should depend on file type" do
54
50
  create_simple_domain
55
- begin
56
- assert_equal "erd.svg", Diagram::Graphviz.create(:filetype => :svg)
57
- ensure
58
- FileUtils.rm "erd.svg" rescue nil
59
- end
51
+ result = Diagram::Graphviz.create(:filetype => :svg)
52
+ assert result.end_with?(".svg"), "Expected filename to end with .svg, got: #{result}"
60
53
  end
61
54
 
62
55
  test "rank direction should be tb for horizontal orientation" do
@@ -8,10 +8,6 @@ class MermaidTest < ActiveSupport::TestCase
8
8
  RailsERD.options.mermaid_style = :classdiagram
9
9
  end
10
10
 
11
- def teardown
12
- FileUtils.rm Dir["erd*.*"] rescue nil
13
- end
14
-
15
11
  def diagram(options = {})
16
12
  @diagram ||= Diagram::Mermaid.new(Domain.generate(options), options).tap do |diagram|
17
13
  diagram.generate
@@ -27,13 +23,10 @@ class MermaidTest < ActiveSupport::TestCase
27
23
  end
28
24
 
29
25
  # Diagram properties =======================================================
30
- test "file name should be mmd" do
26
+ test "file name should have mmd extension" do
31
27
  create_simple_domain
32
- begin
33
- assert_equal "erd.mmd", Diagram::Mermaid.create
34
- ensure
35
- FileUtils.rm "erd.mmd" rescue nil
36
- end
28
+ result = Diagram::Mermaid.create
29
+ assert result.end_with?(".mmd"), "Expected filename to end with .mmd, got: #{result}"
37
30
  end
38
31
 
39
32
  test "direction should be top to bottom by default" do
@@ -385,4 +378,63 @@ class MermaidTest < ActiveSupport::TestCase
385
378
 
386
379
  assert result.include?("erDiagram")
387
380
  end
381
+
382
+ # Namespace tests (Issue #450) ================================================
383
+
384
+ test "classDiagram should handle namespaced model names" do
385
+ create_model "Post"
386
+ create_module_model "Admin::Author", :post => :references do
387
+ belongs_to :post
388
+ end
389
+
390
+ result = diagram.graph
391
+
392
+ assert result.include?("classDiagram")
393
+ # Backticks should properly escape the namespace
394
+ assert result.any? { |line| line.include?("`Admin::Author`") }, "Should wrap namespaced entity in backticks"
395
+ assert result.any? { |line| line.include?("`Post`") && line.include?("`Admin::Author`") }, "Relationship should use backticks for both entities"
396
+ end
397
+
398
+ test "erDiagram should wrap namespaced model names in double quotes" do
399
+ create_model "Post"
400
+ create_module_model "Admin::Author", :post => :references do
401
+ belongs_to :post
402
+ end
403
+
404
+ result = diagram(:mermaid_style => :erdiagram).graph.join("\n")
405
+
406
+ assert result.include?("erDiagram")
407
+ # Double quotes are required for entity names containing ::
408
+ assert result.include?('"Admin::Author"'), "Should wrap namespaced entity in double quotes, got: #{result}"
409
+ # Relationship line should also quote the namespaced entity (order may vary)
410
+ assert result.match?(/("Admin::Author".*--.*Post|Post.*--.*"Admin::Author")/), "Relationship should quote namespaced entity"
411
+ end
412
+
413
+ test "erDiagram should handle multiple namespaced models in relationships" do
414
+ create_module_model "Admin::User" do
415
+ has_many :posts, class_name: "Blog::Post"
416
+ end
417
+ create_module_model "Blog::Post", :admin_user => :references do
418
+ belongs_to :admin_user, class_name: "Admin::User"
419
+ end
420
+
421
+ result = diagram(:mermaid_style => :erdiagram).graph.join("\n")
422
+
423
+ assert result.include?('"Admin::User"'), "Should quote Admin::User"
424
+ assert result.include?('"Blog::Post"'), "Should quote Blog::Post"
425
+ end
426
+
427
+ test "erDiagram should quote namespaced entities in specialization relationships" do
428
+ create_model "Vehicle", :type => :string
429
+ # Create a namespaced subclass (STI)
430
+ create_module_model "Transport::Car", Vehicle
431
+
432
+ # STI requires inheritance: true option
433
+ result = diagram(:mermaid_style => :erdiagram, :inheritance => true).graph.join("\n")
434
+
435
+ # The specialization relationship should quote the namespaced entity
436
+ assert result.include?('"Transport::Car"'), "Should quote namespaced specialized entity"
437
+ # Verify the specialization relationship line also quotes it
438
+ assert result.match?(/Vehicle.*--.*"Transport::Car"/), "Specialization relationship should quote namespaced entity"
439
+ end
388
440
  end
@@ -14,10 +14,6 @@ class RakeTaskTest < ActiveSupport::TestCase
14
14
  Rake.application.options.silent = true
15
15
  end
16
16
 
17
- def teardown
18
- FileUtils.rm "erd.dot" rescue nil
19
- end
20
-
21
17
  define_method :create_app do
22
18
  Object::Quux = Module.new
23
19
  Object::Quux::Application = Class.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-erd
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rolf Timmermans