Module: MigrationComments::AnnotateModels::ClassMethods

Defined in:
lib/migration_comments/annotate_models.rb

Instance Method Summary collapse

Instance Method Details

#commented_info(klass, info) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/migration_comments/annotate_models.rb', line 17

def commented_info(klass, info)
  table_name = klass.table_name
  adapter = klass.connection
  table_comment = adapter.retrieve_table_comment(table_name)
  column_comments = adapter.retrieve_column_comments(table_name)
  lines = []
  info.each_line{|l| lines << l.chomp}
  column_regex = /^#\s+(\w+)\s+:\w+/
  len = lines.select{|l| l =~ column_regex}.map{|l| l.length}.max
  lines.each do |line|
    if line =~ /# Table name: |# table \+\w+\+ /
      line << " # #{table_comment}" if table_comment
    elsif line =~ column_regex
      comment = column_comments[$1.to_sym]
      line << " " * (len - line.length) << " # #{comment}" if comment
    end
  end
  lines.join($/) + $/
end

#get_schema_info(*args) ⇒ Object



10
11
12
13
14
15
# File 'lib/migration_comments/annotate_models.rb', line 10

def get_schema_info(*args)
  klass = args[0]
  klass.reset_column_information
  info = super(*args)
  commented_info(klass, info)
end