Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

displaying ERD by modules #156

Closed
ostrochovsky opened this issue Jul 8, 2015 · 6 comments
Closed

displaying ERD by modules #156

ostrochovsky opened this issue Jul 8, 2015 · 6 comments
Milestone

Comments

@ostrochovsky
Copy link

Hello,

I am building an application divided into several modules (models, views, controllers - all in separate modules/subdirectories). It could be very nice to have option to generate model diagram grouped by modules - entities from each modules in its own "box", but still interconnected correctly, and optionally, filter what to generate also by modules (for having separate sub-ERD for each module separately).

Graphviz has this option and implements it by subgraphing, examples:
http://www.graphviz.org/content/fdpclust
http://www.graphviz.org/content/cluster

Do you plan to add such feature in near future? If no, I could try to do some extension, if you would give me some hints for "best practise", how to incorporate it into your great gem, and provide it to you and the community.

Thanks in advance.

Jano

@kerrizor
Copy link
Collaborator

kerrizor commented Jul 8, 2015

rails-erd is primarily focused on graphing the ActiveRecord relationships between models, but there's no reason it couldn't expand to display controller and/or view relationships! First step, I think, would be to figure out what sort of data structure would best represent these connections, and get that working - are there existing hooks to relate these object to each other, if they're not ActiveRecord related (ie has_many and 'belongs_to`)

@ostrochovsky
Copy link
Author

I was not exact enough, currently controler and view are not very
interesting for me, but ActiveRecord models separated between different
modules are...

for example:

I have some modules and some models in each:

app/models/accounts.rb
app/models/accounts/user.rb
...
app/models/game.rb
app/models/game/server.rb
...

there are relations between models in different modules, e.g.:

class Game::Server < ActiveRecord::Base
belongs_to :user, class_name: "Accounts::User"
end

I would like to have Game models (classes) grouped in subgraph called Game,
and Accounts models (classes) grouped in subgraph called Accounts, etc.

how could I save also Graphviz .dot source? I could for example make
conversion script, when it will find ActiveRecord class name in the form
Module::Model, it will include it into subgraph and generate final output
then... or maybe you'll see more elegant/efficient/smart solution...

thanks

Jano

On Wed, Jul 8, 2015 at 4:36 PM, Kerri Miller notifications@github.com
wrote:

rails-erd is primarily focused on graphing the ActiveRecord relationships
between models, but there's no reason it couldn't expand to display
controller and/or view relationships! First step, I think, would be to
figure out what sort of data structure would best represent these
connections, and get that working - are there existing hooks to relate
these object to each other, if they're not ActiveRecord related (ie
has_many and 'belongs_to`)


Reply to this email directly or view it on GitHub
#156 (comment)
.

@kerrizor
Copy link
Collaborator

kerrizor commented Jul 9, 2015

AH! Apologies for not understanding. Yes, I think this would be a good to offer, perhaps as a command line option.

@kerrizor kerrizor added this to the 2.0 milestone Jul 9, 2015
@ostrochovsky
Copy link
Author

Hello,

I created a Ruby script snippet (part of bigger solution beyond the scope of rails-erd) to accomplish this task:

system "bin/rake erd filetype=dot"
system 'dot erd.dot -Tpdf > erd.pdf'
erd_dot_contents = File.read('erd.dot')
models_array = erd_dot_contents.split(/(\s|<|>|\"m_|\")+/)
models_array = models_array.find_all{|item| item =~ /::/ }
modules_array = []
models_array.each_with_index do |item,index|
        item = item.split('::')
        modules_array.push(item[0])
end
modules_array.uniq!.sort!
modules_array.each do |module_name|
        #erd_dot_contents.gsub!(//,"")
        erd_dot_contents.gsub!(/^node\[( shape)/,"node[ style=filled, color=yellow,\\1")
        erd_dot_contents.gsub!(/^(edge\[)/,"splines=ortho;\n\\1")
        erd_dot_contents.gsub!(/(^\"m_#{module_name}::.*?)(((^\"m_)(?!#{module_name}))|((^\s+\"m_)(.*)))/m,"subgraph cluster_#{module_name} { fontcolor=red; fontsize=20; label=#{module_name}; style=filled; fillcolor=lawngreen; pencolor=transparent; \\1}\n\\2")
end
File.open('erd_modular.dot', "w") { |file| file.puts erd_dot_contents }
system 'dot erd_modular.dot -Tpdf > erd_modular.pdf'

Here is how model looks like before and after modification of source dot-file by this snippet:

erd
erd_modular

Feel free to use (public domain).

Caveats (ugly points):

  1. Some subgraphs/clusters are too close to each other and some nodes are not centered well inside the cluster and are just on cluster boundaries. It is not pretty, it should be placed more aesthetically, I googled a lot and tried to fix it, but with no success, maybe it's the limitation of my knowledge of Graphviz, or limitation of Graphviz itself. Advices from more experienced graphwizzards are welcome. Partial (tested) workaround could be generate module labels with variable-length line, e.g. ----------------------------- Accounts -----------------------------, it helps to widen clusters and visually seperate clusters and their nodes from other clusters and their nodes.
  2. Formats, styles and colors "hardcoded" in the script, it would be better to configure it outside the script (command-line, configuration file).

I am OK with current state of this implementation, but anybody is welcome to improve it further, or maybe incorporate modular output as command-line option in rails-erd. Or drop me a line here, if you'll find some bugs or will have further improvement ideas.

Jan Ostrochovsky

@mtyeh411
Copy link

+1

@kerrizor
Copy link
Collaborator

AWESOME! <3 for sharing this.. I'll see if I can work this into 1.5 (which wow, is looking like a big release!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants