Module: MigrationComments::ActiveRecord::SchemaDumper
- Includes:
- SchemaFormatter
- Defined in:
- lib/migration_comments/active_record/schema_dumper.rb
Class Method Summary
collapse
Instance Method Summary
collapse
#render_comment, #render_kv_pair, #render_value
Class Method Details
.included(base) ⇒ Object
4
5
6
7
8
|
# File 'lib/migration_comments/active_record/schema_dumper.rb', line 4
def self.included(base)
base.class_eval do
alias_method_chain :table, :migration_comments
end
end
|
Instance Method Details
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
54
|
# File 'lib/migration_comments/active_record/schema_dumper.rb', line 19
def (table, stream)
table_name = table.inspect.gsub('"', '')
= @connection.(table_name)
column_comments = @connection.retrieve_column_comments(table_name)
= StringIO.new
lines = []
table_line = 0
col_names = {}
error = false
while (line = stream.gets)
content = line.chomp
if content =~ /^# Could not dump table "#{table_name}"/
error = true
elsif content =~ /create_table\s/
table_line = lines.size
elsif content =~ /t\.\w+\s+"(\w+)"/
col_names[lines.size] = $1.to_sym
end
lines << content
end
len = col_names.keys.map{|index| lines[index]}.map(&:length).max + 2 unless col_names.empty?
lines.each_with_index do |line, index|
if error
elsif table_line == index && .present?
block_init = " do |t|"
line.chomp!(block_init) << ", " << () << block_init
elsif col_names[index]
= column_comments[col_names[index]]
line << ',' << ' ' * (len - line.length) << () unless .blank?
end
.puts line
end
.rewind
end
|
10
11
12
13
14
15
16
17
|
# File 'lib/migration_comments/active_record/schema_dumper.rb', line 10
def (table, stream)
tbl_stream = StringIO.new
(table, tbl_stream)
tbl_stream.rewind
= (table, tbl_stream)
tbl_stream.close
stream.print .read
end
|