voormedia / rails-erd

Generate Entity-Relationship Diagrams for Rails applications
http://voormedia.github.io/rails-erd/
MIT License
3.97k stars 363 forks source link

2.0 idea - mountable JS/SVG graphs #226

Open kerrizor opened 7 years ago

kerrizor commented 7 years ago

WHAT IF.. in addition to a command line artifact generator, we had a development env only route that would generate a clickable JS/SVG graph in the browser?

scarroll32 commented 2 years ago

What about outputting in Mermaid markdown format? 🤔

unchidev commented 1 year ago

I just thought the same thing myself. And I made an experimental prototype of it.

image

rails-mermaid-erd https://github.com/koedame/rails-mermaid_erd

Mermaid is excellent and has already produced something very good as an experience. As for me, I would be very happy if rails-erd goes in this direction.

ayuto-takasaki-LITALICO commented 1 year ago

Thank you very much for implementing support for mermaid notation. https://github.com/voormedia/rails-erd/pull/405

I have tried to use it, but I found one problem. When I use the only option in .erdconfig to limit the models to be output, the relationship line is drawn for models that are not included in the output.

only: Model1,Model2

After some investigation, I found that the problem can be fixed by defining the following method in lib/rails_erd/diagram/mermaid.rb and adding a condition in each_relationship.

      def node_exists?(name)
        # I'm not sure if this is the proper way to judge, but it works as long as you don't use a weird model name.
        graph.include?("\tclass `#{name}`")
      end
      each_relationship do |relationship|
        from, to = relationship.source, relationship.destination
        next unless node_exists?(from) && node_exists?(to)

        graph << "\t`#{from.name}` #{relation_arrow(relationship)} `#{to.name}`"

        from.children.each do |child|
          next unless node_exists?(child)

          graph << "\t`#{child.name}` #{relation_arrow(relationship)} `#{to.name}`"
        end

        to.children.each do |child|
          next unless node_exists?(child)

          graph << "\t`#{from.name}` #{relation_arrow(relationship)} `#{child.name}`"
        end
      end

I'm not sure how to implement the node_exists? method in a cool way, so I'll just leave it as a comment on the issue.

Thanks for maintaining a great gem.

(This text was translated by DeepL)