Class: RailsERD::Custom

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_erd/custom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, file_name, level = nil) ⇒ Custom

Returns a new instance of Custom.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails_erd/custom.rb', line 35

def initialize( class_name, file_name, level=nil )
  
  @file_name = file_name
  @class_name = class_name
  @classes = []
  @classes << class_name
  @level = level
  process
  generate
    
end

Instance Attribute Details

#classesObject

Returns the value of attribute classes.



30
31
32
# File 'lib/rails_erd/custom.rb', line 30

def classes
  @classes
end

#reflection_keysObject

Returns the value of attribute reflection_keys.



30
31
32
# File 'lib/rails_erd/custom.rb', line 30

def reflection_keys
  @reflection_keys
end

Instance Method Details

#check_model_validity(model) ⇒ Object



92
93
94
95
96
# File 'lib/rails_erd/custom.rb', line 92

def check_model_validity(model)
    model.abstract_class? or model.table_exists? or raise "table #{model.table_name} does not exist"
  rescue => e
    warn "Ignoring invalid model #{model.name} (#{e.message})"
end

#children_walk(reflections, cur_level) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rails_erd/custom.rb', line 67

def children_walk( reflections, cur_level )
  if @level == cur_level
    return
  end
  keys = reflections.keys
  for k in keys
    p "***processing: #{k}"
    ref = reflections[ k ]
    class_name = ref.klass.name.to_s
    p class_name
    if !@classes.include? class_name
      
      p "Adding #{class_name}"
      @classes << class_name     
      
      begin
        model = class_name.constantize
        children_walk( model.reflections, cur_level + 1 )    
      rescue => e
        warn "Cannot retrieve model: #{class_name}"
      end
    end  
  end
end

#generateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails_erd/custom.rb', line 47

def generate
  puts "************************ classes length: #{@classes.length}"
  options = {
    :filename=>@file_name,
    :filetype=>"pdf",
    :attributes=>["foreign_keys", "primary_keys", "content"],
    :only=>@classes
  }
  begin
    byebug
    file = RailsERD::Diagram::Graphviz.create(options)
  rescue
    puts "---------------------------------Graph viz creation error"
  end
end

#processObject



62
63
64
# File 'lib/rails_erd/custom.rb', line 62

def process
  children_walk(  @class_name.constantize.reflections, 0 )
end