diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index aa519ba7f..5c06215e0 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -408,7 +408,7 @@ Lint/ShadowingOuterLocalVariable: # Offense count: 20 Metrics/AbcSize: - Max: 138 + Max: 139 # Offense count: 28 # Configuration parameters: CountComments, ExcludedMethods. diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index b306d0b84..fc8cd86d3 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -305,7 +305,7 @@ def get_schema_info(klass, header, options = {}) if options[:format_rdoc] info << sprintf("# %-#{max_size}.#{max_size}s%s", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n" elsif options[:format_markdown] - name_remainder = max_size - col_name.length + name_remainder = max_size - col_name.length - non_ascii_length(col_name) type_remainder = (md_type_allowance - 2) - col_type.length info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n" else @@ -932,6 +932,10 @@ def mb_chars_ljust(string, length) string[0..length-1] end end + + def non_ascii_length(string) + string.to_s.chars.reject(&:ascii_only?).length + end end class BadModelFileError < LoadError diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index f95a9e290..ae85d20cb 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -1046,6 +1046,28 @@ def self.when_called_with(options = {}) EOS end + it 'should get schema info as Markdown with multibyte comment' do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer, comment: 'ID'), + mock_column(:name, :string, limit: 50, comment: 'NAME') + ]) + expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(<<-EOS.strip_heredoc) + # #{AnnotateModels::PREFIX} + # + # Table name: `users` + # + # ### Columns + # + # Name | Type | Attributes + # --------------------- | ------------------ | --------------------------- + # **`id(ID)`** | `integer` | `not null, primary key` + # **`name(NAME)`** | `string(50)` | `not null` + # + EOS + end + it 'should get schema info as Markdown' do klass = mock_class(:users, :id,