class RailsErdD3

Constants

ASSOCIATIONS

Public Class Methods

create() click to toggle source
# File lib/rails_erd_d3.rb, line 73
def self.create
  file = File.new("erd.html", "w")
  file.puts(
    "<!DOCTYPE HTML>"\
    "<html>"\
      "#{get_head}"\
      "<body>"\
        "#{get_nav}"\
        "<div id='erd'>"\
        "</div>"\
        "#{get_d3}"\
        "#{get_modals}"\
        "#{get_js}"\
      "</body>"\
    "</html>"
  )
  file.close

  puts "File erd.html was successfully created!"
end
get_all_models() click to toggle source
# File lib/rails_erd_d3.rb, line 19
def self.get_all_models
  case get_rails_version
  when 4
    klass = ActiveRecord::Base
  when 5
    klass = ApplicationRecord
  end

  Rails.application.eager_load!
  klass.connection
  @@models = ObjectSpace.each_object(Class).select { |o| o.ancestors.include? klass } || []
  @@models = @@models - [klass] if @@models.include? klass
end
get_data() click to toggle source
# File lib/rails_erd_d3.rb, line 33
def self.get_data
  get_all_models

  models_list = {}
  @@models.each_with_index do |model, index|
    models_list[model.table_name] = index
  end

  puts "Generating data..."

  nodes = []
  links = []
  @@models.each do |model|
    nodes << { table_name: model.table_name, label: model.name, r: 30 }

    puts " - #{model.name}"
    model.reflections.each do |name, reflection|
      next if reflection.options[:polymorphic]

      begin
        association = reflection.macro.to_s + (reflection.options[:through] ? "_through" : "")

        links << {
          source: models_list[model.table_name],
          target: models_list[reflection.table_name],
          color:  ASSOCIATIONS.index(association)
        }
      rescue
        message = "   # Please ensure that association #{name} sets properly!"
        puts "\e[31m#{message}\e[0m"
        next
      end
    end
  end
  puts "Data were successfully generated."

  data = { nodes: nodes, links: links }
  data.to_json
end
get_rails_version() click to toggle source
# File lib/rails_erd_d3.rb, line 15
def self.get_rails_version
  Rails::VERSION::MAJOR
end

Private Class Methods

get_d3() click to toggle source
# File lib/rails_erd_d3.rb, line 104
def self.get_d3
  puts "Generating JS..."

  content = ERB.new(
    read_file("templates/d3.html.erb")
  ).result(binding)

  puts "JS was successfully generated."
  content
end
get_head() click to toggle source
# File lib/rails_erd_d3.rb, line 96
def self.get_head
  read_file("templates/head.html")
end
get_js() click to toggle source
# File lib/rails_erd_d3.rb, line 115
def self.get_js
  read_file("templates/js.html")
end
get_modals() click to toggle source
# File lib/rails_erd_d3.rb, line 119
def self.get_modals
  puts "Generating modals..."

  content = ERB.new(
    read_file("templates/modals.html.erb")
  ).result(binding)

  puts "Modals were successfully generated."
  content
end
get_nav() click to toggle source
# File lib/rails_erd_d3.rb, line 100
def self.get_nav
  read_file("templates/nav.html")
end
read_file(path) click to toggle source
# File lib/rails_erd_d3.rb, line 130
def self.read_file(path)
  File.read(
    File.expand_path(path, File.dirname(__FILE__))
  )
end