Class: DrawErd::Diagram

Inherits:
Object
  • Object
show all
Defined in:
app/models/draw_erd/diagram.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Diagram

Returns a new instance of Diagram.



27
28
29
30
# File 'app/models/draw_erd/diagram.rb', line 27

def initialize(path)
  @path = File.expand_path(path, Rails.root)
  FileUtils.mkdir_p(@path)
end

Class Method Details

.reset_migrationsObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/draw_erd/diagram.rb', line 13

def reset_migrations
  paths = ActiveRecord::Migrator.migrations_paths
  paths.each do |path|
    Dir.foreach(path) do |file|
      if match_data = /^(\d{3,})_(.+)\.rb$/.match(file)
        version = match_data[1].to_i
        ActiveRecord::Migrator.run(:down, paths, version)
        ActiveRecord::Migrator.run(:up, paths, version)
      end
    end
  end
end

.schemasObject



6
7
8
9
10
11
# File 'app/models/draw_erd/diagram.rb', line 6

def schemas
  schemas = ActiveRecord::Base.connection.tables
  schemas.delete('schema_migrations')
  schemas.map! {|schema| schema.singularize.camelize}
  schemas.sort
end

Instance Method Details

#create(title, domains = [], attributes = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/draw_erd/diagram.rb', line 32

def create(title, domains=[], attributes=false)
  only = domains.map {|domain| domain.to_sym}
  options = {
    filetype: :png,
    attributes: attributes,
    title: false,
    only: only,
    filename: File.join(@path, title.to_s)
  }

  Rails.application.eager_load!
  RailsERD::Diagram::Graphviz.create(options)
rescue => e
  Rails.logger.error(e)
end