module Schemate

Constants

VERSION

Public Class Methods

config() click to toggle source
# File lib/schemate/config.rb, line 7
def self.config
  @config
end
configure() { |config ||= configuration| ... } click to toggle source
# File lib/schemate/config.rb, line 3
def self.configure(&block)
  yield @config ||= Schemate::Configuration.new
end
csv_row(columns, index) click to toggle source
# File lib/schemate.rb, line 45
def csv_row(columns, index)
  %W(
    #{columns.name} #{columns.type} #{columns.limit}
    #{columns.null} #{columns.default} #{index} #{columns.comment}
  )
end
export(format:) click to toggle source
# File lib/schemate.rb, line 14
def export(format:)
  return if format.nil?

  Rails.application.eager_load!
  begin
    export_as(format)
  rescue StandardError => ex
    STDERR.puts "Unable to export : #{ex.message}"
    return
  end
  puts_complete_message(format)
end
export_csv() click to toggle source
# File lib/schemate.rb, line 27
def export_csv
  CSV.open('schemate.csv', 'w') do |file|
    ActiveRecord::Base.descendants.each do |model|
      model_name = model.name
      table_name = model_name.tableize

      next unless tables.include?(table_name)
      file << %W(#{table_name})
      file << %w(FieldName Attitutde Size NULL Default Index Comment)
      model.columns.each do |columns|
        index = 'true' if model.connection.index_exists?(:"#{table_name}", :"#{columns.name}")
        file << csv_row(columns, index)
      end
      file << %w()
    end
  end
end
export_md() click to toggle source
# File lib/schemate.rb, line 52
def export_md
  File.open('schemate.md', 'w') do |file|
    ActiveRecord::Base.descendants.each do |model|
      model_name = model.name
      table_name = model_name.tableize

      next unless tables.include?(table_name)
      file.puts("# Table name: `#{table_name}`\n")
      file.puts('## Columns')
      file.puts('|FieldName |Attitutde |Size |NULL |Default |Index |Comment |')
      file.puts('|---|---|---|---|---|---|---|')
      model.columns.each do |columns|
        index = 'true' if model.connection.index_exists?(:"#{table_name}", :"#{columns.name}")
        file.puts(md_row(columns, index))
      end
      file.puts("\n")
    end
  end
end
md_row(columns, index) click to toggle source
# File lib/schemate.rb, line 72
def md_row(columns, index)
  "|#{columns.name} |#{columns.type} |#{columns.limit} |#{columns.null} |#{columns.default} |#{index} |#{columns.comment} |"
end

Private Class Methods

export_as(format) click to toggle source
# File lib/schemate.rb, line 78
def export_as(format)
  if format == 'csv'
    export_csv
  elsif format == 'md'
    export_md
  end
end
puts_complete_message(format) click to toggle source
# File lib/schemate.rb, line 90
def puts_complete_message(format)
  puts "complete! See schemate.#{format}!"
end
tables() click to toggle source
# File lib/schemate.rb, line 86
def tables
  @tables ||= ActiveRecord::Base.connection.tables
end