Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output for multiline column comments #779

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,11 @@ def get_schema_info(klass, header, options = {})
col_type = get_col_type(col)
attrs = get_attributes(col, col_type, klass, options)
col_name = if with_comments?(klass, options) && col.comment
tmr08c marked this conversation as resolved.
Show resolved Hide resolved
"#{col.name}(#{col.comment})"
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to find something more general and elegant than gsub, but didn't find a good option. I'd be interested if there are other characters that would be concerning (\t?) that would make sense to check for and escape as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there's a library for sanitizing whitespace? It might be worth looking into.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked a bit, but didn't find anything that stuck out. String#dump seemed promising, but I believe it escaped UTF characters that were expected to work.

I could make this more generic and escape any \ with \\ if that seems safer.

else
col.name
end

if options[:format_rdoc]
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
elsif options[:format_yard]
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,33 @@ def mock_column(name, type, options = {})
end
end

context 'when columns have multiline comments' do
let :columns do
[
mock_column(:id, :integer, limit: 8, comment: 'ID'),
mock_column(:notes, :text, limit: 55, comment: "Notes.\nMay include things like notes."),
mock_column(:no_comment, :text, limit: 20, comment: nil)
]
end

let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id(ID) :integer not null, primary key
# notes(Notes.\\nMay include things like notes.):text(55) not null
# no_comment :text(20) not null
#
EOS
end

it 'works with option "with_comment"' do
is_expected.to eq expected_result
end
end

context 'when geometry columns are included' do
let :columns do
[
Expand Down