class RailsERD::CLI

Attributes

options[R]
path[R]

Public Class Methods

new(path, options) click to toggle source
# File lib/rails_erd/cli.rb, line 160
def initialize(path, options)
  @path, @options = path, options
  require "rails_erd/diagram/graphviz"
end
start() click to toggle source
# File lib/rails_erd/cli.rb, line 142
def start
  path = Choice.rest.first || Dir.pwd
  options = Choice.choices.each_with_object({}) do |(key, value), opts|
    if key.start_with? "no_"
      opts[key.gsub("no_", "").to_sym] = !value
    elsif value.to_s.include? ","
      opts[key.to_sym] = value.split(",").map(&:to_s)
    else
      opts[key.to_sym] = value
    end
  end
  if options[:config_file] && options[:config_file] != ''
    RailsERD.options = RailsERD.default_options.merge(Config.load(options[:config_file]))
  end
  new(path, options).start
end

Public Instance Methods

start() click to toggle source
# File lib/rails_erd/cli.rb, line 165
def start
  load_application
  create_diagram
rescue Exception => e
  $stderr.puts "Failed: #{e.class}: #{e.message}"
  $stderr.puts e.backtrace.map { |t| "    from #{t}" } if options[:debug]
end

Private Instance Methods

create_diagram() click to toggle source
# File lib/rails_erd/cli.rb, line 199
def create_diagram
  $stderr.puts "Generating entity-relationship diagram for #{ActiveRecord::Base.descendants.length} models..."
  file = RailsERD::Diagram::Graphviz.create(options)
  $stderr.puts "Diagram saved to '#{file}'."
  `open #{file}` if options[:open]
end
load_application() click to toggle source
# File lib/rails_erd/cli.rb, line 175
    def load_application
      $stderr.puts "Loading application in '#{File.basename(path)}'..."
      environment_path = "#{path}/config/environment.rb"
      require environment_path

      if defined? Rails
        Rails.application.eager_load!
        Rails.application.config.eager_load_namespaces.each(&:eager_load!) if Rails.application.config.respond_to?(:eager_load_namespaces)
      end
    rescue ::LoadError
      error_message = <<~EOS
        Tried to load your application environment from '#{environment_path}' but the file was not present.
        This means that your models might not get loaded fully when the diagram gets built. This can
        make your entity diagram incomplete.

        However, if you are using ActiveRecord without Rails just make sure your models get
        loaded eagerly before we generate the ERD (for example, explicitly require your application
        bootstrap file before calling rails-erd from your Rakefile). We will continue without loading the environment file,
        and recommend you check your diagram for missing models after it gets generated.
      EOS
      puts error_message
    rescue TypeError
    end