diff --git a/README.md b/README.md index c5e3c889..e1d97721 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/annotate_rb/model_annotator/column_annotation/column_component.rb b/lib/annotate_rb/model_annotator/column_annotation/column_component.rb index 325da302..87ab45aa 100644 --- a/lib/annotate_rb/model_annotator/column_annotation/column_component.rb +++ b/lib/annotate_rb/model_annotator/column_annotation/column_component.rb @@ -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%s", @@ -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( @@ -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", diff --git a/lib/annotate_rb/parser.rb b/lib/annotate_rb/parser.rb index 39328dd9..e1e01f6c 100644 --- a/lib/annotate_rb/parser.rb +++ b/lib/annotate_rb/parser.rb @@ -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 diff --git a/spec/lib/annotate_rb/model_annotator/column_annotation/annotation_builder_spec.rb b/spec/lib/annotate_rb/model_annotator/column_annotation/annotation_builder_spec.rb index 5f14362b..a6e29515 100644 --- a/spec/lib/annotate_rb/model_annotator/column_annotation/annotation_builder_spec.rb +++ b/spec/lib/annotate_rb/model_annotator/column_annotation/annotation_builder_spec.rb @@ -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 @@ -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