Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Annotate model options:
--without-comment include database comments in model annotations
--with-column-comments include column comments in model annotations
--without-column-comments exclude column comments in model annotations
--position-of-column-comments VALUE
--position-of-column-comments [with_name|rightmost_column]
set the position, in the annotation block, of the column comment
--with-table-comments include table comments in model annotations
--without-table-comments exclude table comments in model annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ def initialize(column:, max_name_size:, type:, attributes:, position_of_column_c
def name
case position_of_column_comment
when :with_name
"#{column.name}(#{column.comment.gsub(/\n/, '\\n')})"
"#{column.name}(#{escaped_column_comment})"
else
column.name
end
end

def escaped_column_comment
column.comment.to_s.gsub(/\n/, '\\n')
end

def to_rdoc
# standard:disable Lint/FormatParameterMismatch
format("# %-#{max_name_size}.#{max_name_size}s<tt>%s</tt>",
Expand Down Expand Up @@ -56,7 +60,7 @@ def to_markdown
name_remainder = max_name_size - name.length - non_ascii_length(name)
type_remainder = (MD_TYPE_ALLOWANCE - 2) - type.length
attributes_remainder = max_attributes_size + 1 - joined_attributes.length
comment_rightmost = (position_of_column_comment != :rightmost_column) ? "" : " | `#{@column.comment}`"
comment_rightmost = (position_of_column_comment != :rightmost_column) ? "" : " | `#{escaped_column_comment}`"

# standard:disable Lint/FormatParameterMismatch
format(
Expand All @@ -72,7 +76,7 @@ def to_markdown
end

def to_default
comment_rightmost = (position_of_column_comment == :rightmost_column) ? @column.comment : ""
comment_rightmost = (position_of_column_comment == :rightmost_column) ? escaped_column_comment : ""
joined_attributes = attributes.join(", ")
format(
"# %s:%s %s %s",
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate_rb/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def add_model_options_to_parser(option_parser)
@options[:with_column_comments] = false
end

option_parser.on("--position-of-column-comments VALUE",
option_parser.on("--position-of-column-comments [with_name|rightmost_column]",
"set the position, in the annotation block, of the column comment") do |value|
@options[:position_of_column_comments] = value.to_sym
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@

context "when position of column comment is set to `rightmost_column`" do
let(:position_of_column_comment) { :rightmost_column }
let(:expected_result) { "# notes :text(55) not null Notes.\nMay include things like notes." }
let(:expected_result) { "# notes :text(55) not null Notes.\\nMay include things like notes." }
it { is_expected.to eq(expected_result) }
end
end
Expand Down Expand Up @@ -661,6 +661,31 @@
is_expected.to eq(expected_result)
end
end

context "when the column has a multi-line comment" do
let(:column) { mock_column("id", :text, comment: "Identifier.\nPrimary key for record.") }
let(:expected_result) do
<<~COLUMN.strip
# **`id(Identifier.\\nPrimary key for record.)`** | `text` | `not null`
COLUMN
end

it "returns the column annotation" do
is_expected.to eq(expected_result)
end

context "when position of column comment is set to `rightmost_column`" do
let(:position_of_column_comment) { :rightmost_column }

let(:expected_result) do
<<~COLUMN.strip
# **`id`** | `text` | `not null` | `Identifier.\\nPrimary key for record.`
COLUMN
end

it { is_expected.to eq(expected_result) }
end
end
end
end
end
Expand Down
Loading