Skip to content

Commit 2071305

Browse files
authored
Merge pull request #205 from bstegmaier/cluster_by_namespace
Implement clustering by namespace
2 parents 77c26bf + f7ba283 commit 2071305

File tree

7 files changed

+30
-5
lines changed

7 files changed

+30
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ title: sample title
6161
exclude: null
6262
only: null
6363
prepend_primary: false
64+
cluster: false
6465
```
6566

6667

examples/erdconfig.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ title: sample title
1818
exclude: null
1919
only: null
2020
prepend_primary: false
21+
cluster: false

lib/rails_erd.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def default_options
5151
:title, true,
5252
:exclude, nil,
5353
:only, nil,
54-
:prepend_primary, false
54+
:prepend_primary, false,
55+
:cluster, false
5556
]
5657
end
5758
end

lib/rails_erd/cli.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
desc "Ensure primary key is at start of attribute list"
6565
end
6666

67+
option :cluster do
68+
long "--cluster"
69+
desc "Display models in subgraphs based on their namespace."
70+
end
71+
6772
separator ""
6873
separator "Output options:"
6974

lib/rails_erd/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ def normalize_value(key, value)
6363
# [<string>]
6464
when :only, :exclude
6565
Array(value).join(",").split(",").map { |v| v.strip }
66+
6667
# true | false
67-
when :disconnected, :indirect, :inheritance, :markup, :polymorphism, :warn
68+
when :disconnected, :indirect, :inheritance, :markup, :polymorphism,
69+
:warn, :cluster
6870
!!value
6971

7072
# nil | <string>

lib/rails_erd/diagram/graphviz.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,14 @@ def relationship_style(relationship)
210210
end
211211

212212
each_entity do |entity, attributes|
213-
draw_node entity.name, entity_options(entity, attributes)
213+
if options[:cluster] && entity.namespace
214+
cluster_name = "cluster_#{entity.namespace}"
215+
cluster = graph.get_graph(cluster_name) ||
216+
graph.add_graph(cluster_name, label: entity.namespace)
217+
draw_cluster_node cluster, entity.name, entity_options(entity, attributes)
218+
else
219+
draw_node entity.name, entity_options(entity, attributes)
220+
end
214221
end
215222

216223
each_specialization do |specialization|
@@ -233,15 +240,19 @@ def relationship_style(relationship)
233240
private
234241

235242
def node_exists?(name)
236-
!!graph.get_node(escape_name(name))
243+
!!graph.search_node(escape_name(name))
237244
end
238245

239246
def draw_node(name, options)
240247
graph.add_nodes escape_name(name), options
241248
end
242249

250+
def draw_cluster_node(cluster, name, options)
251+
cluster.add_nodes escape_name(name), options
252+
end
253+
243254
def draw_edge(from, to, options)
244-
graph.add_edges graph.get_node(escape_name(from)), graph.get_node(escape_name(to)), options if node_exists?(from) and node_exists?(to)
255+
graph.add_edges graph.search_node(escape_name(from)), graph.search_node(escape_name(to)), options if node_exists?(from) and node_exists?(to)
245256
end
246257

247258
def escape_name(name)

lib/rails_erd/domain/entity.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def children
9292
@children ||= domain.specializations_by_entity_name(name).map(&:specialized)
9393
end
9494

95+
def namespace
96+
name.scan(/(.*)::.*/).dig(0,0)
97+
end
98+
9599
def to_s # @private :nodoc:
96100
name
97101
end

0 commit comments

Comments
 (0)