views:

101

answers:

2

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

For models, a railroad-generated class diagram shows attributes of each model and the associations between one model and another. A sample diagram can be found here. It is very useful for a developer to see attributes and associations of models. While attributes and associations reveal the inner states and relationships of models, methods specify their behaviours. They are all desirable in a class diagram. I would like railroad to generate a class diagram that also lists methods for models, which will help me to know what each model does. I know methods are displayed in a diagram that is generated for controllers, but I don't see such an option for a diagram of models. Does someone know how to do that with railroad? Or is that possible?

Thanks!

+1  A: 

Taken from "http://railroad.rubyforge.org/"

Usage:

railroad [options] command


Models diagram options

* -a, --all
  Include all models (not only ActiveRecord::Base derived)
Hock
Thanks Hock. That option gives me all the models. But what I want is to have the models show methods.
Bryan
+2  A: 

Railroad does not add the model methods to the diagram. You can monkey patch the railroad code to get this feature.

Create a file called rail_road_monkey_patch.rb in config/initializers directory and 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 railroad(you need this to ensure 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:diagrams produces doc/models.svg and doc/controllers.svg. If you are on Windows, alter the rake task appropriately.

Note 1: Rake task is taken from the Railroad readme.

Note 2 I haven't tested the code.

KandadaBoggu
I have updated the answer to add the rake task, take a look.
KandadaBoggu
Thanks. Let me see how it works.
Bryan