Skip to content

Commit

Permalink
Merge pull request #219 from voormedia/pr-189
Browse files Browse the repository at this point in the history
support for 'only_models_include_depth' option
  • Loading branch information
kerrizor authored Aug 10, 2016
2 parents 2071305 + 0757160 commit a75d23c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ warn: true
title: sample title
exclude: null
only: null
only_recursion_depth: null
prepend_primary: false
cluster: false
```
Expand Down
1 change: 1 addition & 0 deletions examples/erdconfig.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ warn: true
title: sample title
exclude: null
only: null
only_recursion_depth: null
prepend_primary: false
cluster: false
3 changes: 2 additions & 1 deletion lib/rails_erd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def default_options
:title, true,
:exclude, nil,
:only, nil,
:only_recursion_depth, nil,
:prepend_primary, false,
:cluster, false
:cluster, false,
]
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/rails_erd/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
desc "Filter to only include listed models in diagram."
end

option :only_recursion_depth do
long "--only_recursion_depth=INTEGER"
desc "Recurses into relations specified by --only upto a depth N."
end

option :exclude do
long "--exclude"
desc "Filter to exclude listed models in diagram."
Expand Down
28 changes: 28 additions & 0 deletions lib/rails_erd/diagram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ def create
def generate
instance_eval(&callbacks[:setup])

if options.only_recursion_depth.present?
depth = options.only_recursion_depth.to_i
options.only.dup.each do |class_name|
options.only += recurse_into_relationships(@domain.entity_by_name(class_name), depth)
end
options.only.uniq!
end

filtered_entities.each do |entity|
instance_exec entity, filtered_attributes(entity), &callbacks[:each_entity]
end
Expand All @@ -138,6 +146,26 @@ def generate
end
end

def recurse_into_relationships(entity, max_level, current_level = 0)
return [] unless entity
return [] if max_level == current_level

relationships = entity.relationships.reject{|r| r.indirect? || r.recursive?}

relationships.map do |relationship|
other_entitiy = if relationship.source == entity
relationship.destination
else
relationship.source
end
if other_entitiy and !other_entitiy.generalized?
[other_entitiy.name] + recurse_into_relationships(other_entitiy, max_level, current_level + 1)
else
[]
end
end.flatten.uniq
end

def save
instance_eval(&callbacks[:save])
end
Expand Down

0 comments on commit a75d23c

Please sign in to comment.