Creating a model diagram using the railway

Railroad is a great UML tool for Ruby on Rails. It can automatically generate class diagrams of models and controllers.

For models, a class diagram built on rail shows the attributes of each model and the association between one model and another. A sample diagram can be found here. It is very useful for the developer to see the attributes and associations of models. Although attributes and associations reveal the internal states and relationships of models, methods determine their behavior. All of them are desirable in the class diagram. I would like the railway to create a class diagram, which also lists methods for models that will help me find out what each model does. I know that methods are displayed in a diagram created for controllers , but I do not see such an option for a diagram of models. Does anyone know how to do this with the railway? Or is it possible?

Thank!

+3
source share
2 answers

The railway does not add model methods to the diagram. You can monkey fix the rail code to get this feature.

Create a file with a name rail_road_monkey_patch.rbin the directory config/initializersand add the following code.

require 'app_diagram'

# RailRoad models diagram
class ModelsDiagram 

  alias_method_chain :process_class, :methods

  def process_class_with_methods(current_class)
    if current_class.is_a? Class
      na = {:public => [], :protected => [], :private => []}
      na[:public] = current_class.public_instance_methods(false).sort unless @options.hide_public
      na[:protected] = current_class.protected_instance_methods(false).sort unless @options.hide_protected
      na[:private] = current_class.private_instance_methods(false).sort unless @options.hide_private
      @graph.add_node ['model', current_class.name, na]
    end
    process_class_without_methods(current_class)
  end
end

Now you need a rake task to run the railway (this is necessary to ensure that the patch is loaded).

namespace :doc do
namespace :diagram do
  task :models => :environment do
    sh "railroad -i -l -a -m -M | dot -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/models.svg"
  end

  task :controllers => :environment do
    sh "railroad -i -l -C | neato -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/controllers.svg"
  end
end

task :diagrams => %w(diagram:models diagram:controllers)
end

Then rake doc:diagramscreates doc/models.svgand doc/controllers.svg. If you are on Windows, change the rake task accordingly.

Note 1 : The Rake task is taken from Railroadreadme.

Note 2 I have not tested the code.

+2
source

http://railroad.rubyforge.org/"

:

railroad [options] command


Models diagram options

* -a, --all
  Include all models (not only ActiveRecord::Base derived)
+1

Source: https://habr.com/ru/post/1737985/


All Articles