Commit 693abdd2 by hasimoto1009 Committed by Cuong Tran

Fix shifted when format_markdown option enabled and used non-ascii (#650)

parent 5da92c47
...@@ -408,7 +408,7 @@ Lint/ShadowingOuterLocalVariable: ...@@ -408,7 +408,7 @@ Lint/ShadowingOuterLocalVariable:
# Offense count: 20 # Offense count: 20
Metrics/AbcSize: Metrics/AbcSize:
Max: 138 Max: 139
# Offense count: 28 # Offense count: 28
# Configuration parameters: CountComments, ExcludedMethods. # Configuration parameters: CountComments, ExcludedMethods.
......
...@@ -305,7 +305,7 @@ module AnnotateModels ...@@ -305,7 +305,7 @@ module AnnotateModels
if options[:format_rdoc] if options[:format_rdoc]
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n" info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
elsif options[:format_markdown] 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 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" info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
else else
...@@ -932,6 +932,10 @@ module AnnotateModels ...@@ -932,6 +932,10 @@ module AnnotateModels
string[0..length-1] string[0..length-1]
end end
end end
def non_ascii_length(string)
string.to_s.chars.reject(&:ascii_only?).length
end
end end
class BadModelFileError < LoadError class BadModelFileError < LoadError
......
...@@ -1046,6 +1046,28 @@ EOS ...@@ -1046,6 +1046,28 @@ EOS
EOS EOS
end 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 it 'should get schema info as Markdown' do
klass = mock_class(:users, klass = mock_class(:users,
:id, :id,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment