Skip to content

Commit a75d23c

Browse files
authored
Merge pull request #219 from voormedia/pr-189
support for 'only_models_include_depth' option
2 parents 2071305 + 0757160 commit a75d23c

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ warn: true
6060
title: sample title
6161
exclude: null
6262
only: null
63+
only_recursion_depth: null
6364
prepend_primary: false
6465
cluster: false
6566
```

examples/erdconfig.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ warn: true
1717
title: sample title
1818
exclude: null
1919
only: null
20+
only_recursion_depth: null
2021
prepend_primary: false
2122
cluster: false

lib/rails_erd.rb

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

lib/rails_erd/cli.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
desc "Filter to only include listed models in diagram."
5050
end
5151

52+
option :only_recursion_depth do
53+
long "--only_recursion_depth=INTEGER"
54+
desc "Recurses into relations specified by --only upto a depth N."
55+
end
56+
5257
option :exclude do
5358
long "--exclude"
5459
desc "Filter to exclude listed models in diagram."

lib/rails_erd/diagram.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ def create
125125
def generate
126126
instance_eval(&callbacks[:setup])
127127

128+
if options.only_recursion_depth.present?
129+
depth = options.only_recursion_depth.to_i
130+
options.only.dup.each do |class_name|
131+
options.only += recurse_into_relationships(@domain.entity_by_name(class_name), depth)
132+
end
133+
options.only.uniq!
134+
end
135+
128136
filtered_entities.each do |entity|
129137
instance_exec entity, filtered_attributes(entity), &callbacks[:each_entity]
130138
end
@@ -138,6 +146,26 @@ def generate
138146
end
139147
end
140148

149+
def recurse_into_relationships(entity, max_level, current_level = 0)
150+
return [] unless entity
151+
return [] if max_level == current_level
152+
153+
relationships = entity.relationships.reject{|r| r.indirect? || r.recursive?}
154+
155+
relationships.map do |relationship|
156+
other_entitiy = if relationship.source == entity
157+
relationship.destination
158+
else
159+
relationship.source
160+
end
161+
if other_entitiy and !other_entitiy.generalized?
162+
[other_entitiy.name] + recurse_into_relationships(other_entitiy, max_level, current_level + 1)
163+
else
164+
[]
165+
end
166+
end.flatten.uniq
167+
end
168+
141169
def save
142170
instance_eval(&callbacks[:save])
143171
end

0 commit comments

Comments
 (0)