Skip to content

Commit 6cb5489

Browse files
tmr08cvfonic
authored andcommitted
Fix output for multiline column comments (ctran#779)
Closes ctran#778 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 e9c8e3b commit 6cb5489

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/annotate/annotate_models.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,11 @@ def get_schema_info(klass, header, options = {})
251251
col_type = get_col_type(col)
252252
attrs = get_attributes(col, col_type, klass, options)
253253
col_name = if with_comments?(klass, options) && col.comment
254-
"#{col.name}(#{col.comment})"
254+
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})"
255255
else
256256
col.name
257257
end
258+
258259
if options[:format_rdoc]
259260
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
260261
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
@@ -1096,6 +1096,33 @@ def mock_column(name, type, options = {})
10961096
end
10971097
end
10981098

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

0 commit comments

Comments
 (0)