Module: Active::Dbml

Defined in:
lib/active/dbml.rb,
lib/active/dbml/engine.rb,
lib/active/dbml/version.rb,
app/jobs/active/dbml/application_job.rb,
app/helpers/active/dbml/application_helper.rb,
app/mailers/active/dbml/application_mailer.rb,
app/controllers/active/dbml/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper Classes: ApplicationController, ApplicationJob, ApplicationMailer, Engine

Constant Summary collapse

VERSION =
"0.2.5"

Class Method Summary collapse

Class Method Details

.export_dbml(export_path) ⇒ Object



6
7
8
9
10
11
# File 'lib/active/dbml.rb', line 6

def self.export_dbml(export_path)
  dbml_file = File.new(export_path, 'w+')
  models = get_model_list
  dbml = generate_dbml(models)
  dbml_file.puts(dbml)
end

.generate_dbml(models) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active/dbml.rb', line 13

def self.generate_dbml(models)
  begin
    dbml_output = []
    # 出力済みのEnumを追跡
    enums_generated = Set.new
  
    models.each do |model|
      if ActiveRecord::Base.connection.table_exists?(model.table_name)
        # 抽象クラスはスキップ
        next if model.abstract_class?

        primary_key = get_primary_key(model)
        foreign_keys = get_foreign_keys(model)
  
        dbml_output << "Table #{model.table_name} {"
  
        # Enumを取得
        defined_enums = model.defined_enums.keys
  
        # カラム情報を生成
        generate_columns(dbml_output, model, primary_key, foreign_keys, defined_enums)
  
        # Index情報の取得
        generate_indexes(dbml_output, model)
  
        # テーブルコメントの取得
        generate_table_comment(dbml_output, model)
  
        # テーブルを閉じる
        dbml_output << "}\n"
  
        # Enumの詳細を取得
        generate_enums(dbml_output, model, enums_generated)
      end
    end
  
    dbml_output.join("\n")
  rescue => exception
    Rails.logger.error exception
  end
end