Class: ModelsDiagram

Inherits:
AppDiagram show all
Defined in:
lib/railroad/models_diagram.rb

Overview

RailRoad models diagram

Instance Method Summary collapse

Methods inherited from AppDiagram

#print, #process

Constructor Details

#initialize(options = OptionsStruct.new) ⇒ ModelsDiagram

Returns a new instance of ModelsDiagram.



12
13
14
15
16
17
18
# File 'lib/railroad/models_diagram.rb', line 12

def initialize(options = OptionsStruct.new)
  #options.exclude.map! {|e| "app/models/" + e}
  super options 
  @graph.diagram_type = 'Models'
  # Processed habtm associations
  @habtm = []
end

Instance Method Details

#generateObject

Process model files



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/railroad/models_diagram.rb', line 21

def generate
  STDERR.print "Generating models diagram\n" if @options.verbose
  get_files.each do |f|
    begin
      process_class extract_class_name(f).constantize
    rescue Exception
      STDERR.print "Warning: exception #{$!} raised while trying to load model class #{f}\n"
    end

  end
end

#get_files(prefix = '') ⇒ Object



33
34
35
36
37
38
# File 'lib/railroad/models_diagram.rb', line 33

def get_files(prefix ='')
  files = !@options.specify.empty? ? Dir.glob(@options.specify) : Dir.glob(prefix << "app/models/**/*.rb")
  files += Dir.glob("vendor/plugins/**/app/models/*.rb") if @options.plugins_models
  files -= Dir.glob(@options.exclude)
  files
end

#process_association(class_name, assoc) ⇒ Object

Process a model association



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/railroad/models_diagram.rb', line 114

def process_association(class_name, assoc)

  STDERR.print "\t\tProcessing model association #{assoc.name.to_s}\n" if @options.verbose

  # Skip "belongs_to" associations
  return if assoc.macro.to_s == 'belongs_to' && !@options.show_belongs_to

  # Only non standard association names needs a label
  
  # from patch #12384
  # if assoc.class_name == assoc.name.to_s.singularize.camelize
  assoc_class_name = (assoc.class_name.respond_to? 'underscore') ? assoc.class_name.underscore.singularize.camelize : assoc.class_name 
  if assoc_class_name == assoc.name.to_s.singularize.camelize
    assoc_name = ''
  else
    assoc_name = assoc.name.to_s
  end 

  if ['has_one', 'belongs_to'].include? assoc.macro.to_s
    assoc_type = 'one-one'
  elsif assoc.macro.to_s == 'has_many' && (! assoc.options[:through])
    assoc_type = 'one-many'
  else # habtm or has_many, :through
    return if @habtm.include? [assoc.class_name, class_name, assoc_name]
    assoc_type = 'many-many'
    @habtm << [class_name, assoc.class_name, assoc_name]
  end  
  # from patch #12384    
  # @graph.add_edge [assoc_type, class_name, assoc.class_name, assoc_name]
  @graph.add_edge [assoc_type, class_name, assoc_class_name, assoc_name]    
end

#process_class(current_class) ⇒ Object

Process a model class



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/railroad/models_diagram.rb', line 41

def process_class(current_class)

  STDERR.print "\tProcessing #{current_class}\n" if @options.verbose

  generated = false
      
  # Is current_clas derived from ActiveRecord::Base?
  if current_class.respond_to?'reflect_on_all_associations'


    node_attribs = []
    if @options.brief || current_class.abstract_class?
      node_type = 'model-brief'
    else 
      node_type = 'model'

      # Collect model's content columns

      content_columns = current_class.content_columns
	
      if @options.hide_magic 
        # From patch #13351
        # http://wiki.rubyonrails.org/rails/pages/MagicFieldNames
        magic_fields = [
        "created_at", "created_on", "updated_at", "updated_on",
        "lock_version", "type", "id", "position", "parent_id", "lft", 
        "rgt", "quote", "template"
        ]
        magic_fields << current_class.table_name + "_count" if current_class.respond_to? 'table_name' 
        content_columns = current_class.content_columns.select {|c| ! magic_fields.include? c.name}
      else
        content_columns = current_class.content_columns
      end
      
      content_columns.each do |a|
        content_column = a.name
        content_column += ' :' + a.type.to_s unless @options.hide_types
        node_attribs << content_column
      end
    end
    @graph.add_node [node_type, current_class.name, node_attribs]
    generated = true
    # Process class associations
    associations = current_class.reflect_on_all_associations
    if @options.inheritance && ! @options.transitive
      superclass_associations = current_class.superclass.reflect_on_all_associations
      
      associations = associations.select{|a| ! superclass_associations.include? a} 
      # This doesn't works!
      # associations -= current_class.superclass.reflect_on_all_associations
    end
    associations.each do |a|
      process_association current_class.name, a
    end
  elsif @options.all && (current_class.is_a? Class)
    # Not ActiveRecord::Base model
    node_type = @options.brief ? 'class-brief' : 'class'
    @graph.add_node [node_type, current_class.name]
    generated = true
  elsif @options.modules && (current_class.is_a? Module)
      @graph.add_node ['module', current_class.name]
  end

  # Only consider meaningful inheritance relations for generated classes
  if @options.inheritance && generated && 
     (current_class.superclass != ActiveRecord::Base) &&
     (current_class.superclass != Object)
    @graph.add_edge ['is-a', current_class.superclass.name, current_class.name]
  end      

end