Skip to content

Commit 84c21eb

Browse files
committed
Fix output for multiline column comments
If a column comment includes the newline character, the newline character would be "printed" into the annotation block resulting in a line break and an uncommented line. For example, for the following table: ``` create_table "users", force: :cascade do |t| t.string "name", comment: "This is a comment.\nWith two lines!" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end ``` annotating the model with the `--with-comment` flag will result in: ``` \# == Schema Information \# \# Table name: users \# \# id :bigint not null, primary key \# name(This is a comment. With two lines!) :string \# created_at :datetime not null \# updated_at :datetime not null \# ``` This uncommented line would result in invalid Ruby and cause the file to no longer be valid. This fix replaces the newline character with an escaped version, so the output will look more like: ``` \# == Schema Information \# \# Table name: users \# \# id :bigint not null, primary key \# name(This is a comment.\nWith two lines!):string \# created_at :datetime not null \# updated_at :datetime not null \# ```
1 parent 5745034 commit 84c21eb

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/annotate/annotate_models.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,14 @@ def get_schema_info(klass, header, options = {})
290290
end
291291
end
292292
end
293+
294+
# Generate the column name optionally including column comments
293295
col_name = if with_comments?(klass, options) && col.comment
294-
"#{col.name}(#{col.comment})"
296+
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})"
295297
else
296298
col.name
297299
end
300+
298301
if options[:format_rdoc]
299302
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
300303
elsif options[:format_yard]

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,33 @@ def mock_column(name, type, options = {})
10951095
end
10961096
end
10971097

1098+
context 'when columns have multiline comments' do
1099+
let :columns do
1100+
[
1101+
mock_column(:id, :integer, limit: 8, comment: 'ID'),
1102+
mock_column(:notes, :text, limit: 55, comment: "Notes.\nMay include things like notes."),
1103+
mock_column(:no_comment, :text, limit: 20, comment: nil)
1104+
]
1105+
end
1106+
1107+
let :expected_result do
1108+
<<~EOS
1109+
# Schema Info
1110+
#
1111+
# Table name: users
1112+
#
1113+
# id(ID) :integer not null, primary key
1114+
# notes(Notes.\\nMay include things like notes.):text(55) not null
1115+
# no_comment :text(20) not null
1116+
#
1117+
EOS
1118+
end
1119+
1120+
it 'works with option "with_comment"' do
1121+
is_expected.to eq expected_result
1122+
end
1123+
end
1124+
10981125
context 'when geometry columns are included' do
10991126
let :columns do
11001127
[

0 commit comments

Comments
 (0)