Commit b1f9489c by motot Committed by Cuong Tran

Support database comment for rails 5 (#459)

Support Database Comment for Rails 5 Added with_comment options
parent 53941460
...@@ -103,7 +103,7 @@ Lint/UselessAccessModifier: ...@@ -103,7 +103,7 @@ Lint/UselessAccessModifier:
# Offense count: 17 # Offense count: 17
Metrics/AbcSize: Metrics/AbcSize:
Max: 146 Max: 159
# Offense count: 3 # Offense count: 3
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
...@@ -116,7 +116,7 @@ Metrics/BlockNesting: ...@@ -116,7 +116,7 @@ Metrics/BlockNesting:
# Offense count: 8 # Offense count: 8
Metrics/CyclomaticComplexity: Metrics/CyclomaticComplexity:
Max: 37 Max: 42
# Offense count: 350 # Offense count: 350
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
...@@ -127,7 +127,7 @@ Metrics/LineLength: ...@@ -127,7 +127,7 @@ Metrics/LineLength:
# Offense count: 24 # Offense count: 24
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/MethodLength: Metrics/MethodLength:
Max: 71 Max: 79
# Offense count: 1 # Offense count: 1
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
...@@ -136,7 +136,7 @@ Metrics/ModuleLength: ...@@ -136,7 +136,7 @@ Metrics/ModuleLength:
# Offense count: 7 # Offense count: 7
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:
Max: 42 Max: 48
# Offense count: 1 # Offense count: 1
Style/AccessorMethodName: Style/AccessorMethodName:
......
...@@ -208,6 +208,9 @@ module AnnotateModels ...@@ -208,6 +208,9 @@ module AnnotateModels
info << get_schema_header_text(klass, options) info << get_schema_header_text(klass, options)
max_size = klass.column_names.map(&:size).max || 0 max_size = klass.column_names.map(&:size).max || 0
with_comment = options[:with_comment] && klass.columns.first.respond_to?(:comment)
max_size = klass.columns.map{|col| col.name.size + col.comment.size }.max || 0 if with_comment
max_size += 2 if with_comment
max_size += options[:format_rdoc] ? 5 : 1 max_size += options[:format_rdoc] ? 5 : 1
md_names_overhead = 6 md_names_overhead = 6
md_type_allowance = 18 md_type_allowance = 18
...@@ -271,15 +274,19 @@ module AnnotateModels ...@@ -271,15 +274,19 @@ module AnnotateModels
end end
end end
end end
col_name = if with_comment
"#{col.name}(#{col.comment})"
else
col.name
end
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
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
info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n" info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col_name, col_type, attrs.join(", ")).rstrip + "\n"
end end
end end
......
...@@ -44,7 +44,8 @@ if Rails.env.development? ...@@ -44,7 +44,8 @@ if Rails.env.development?
'force' => 'false', 'force' => 'false',
'trace' => 'false', 'trace' => 'false',
'wrapper_open' => nil, 'wrapper_open' => nil,
'wrapper_close' => nil 'wrapper_close' => nil,
'with_comment' => true
) )
end end
......
...@@ -494,7 +494,7 @@ EOS ...@@ -494,7 +494,7 @@ EOS
[:notes, :text, { limit: 55 }] [:notes, :text, { limit: 55 }]
] ]
it 'should work with options = #{options}' do it "should work with options = #{options}" do
with_columns = (options.delete(:with_columns) || default_columns).map do |column| with_columns = (options.delete(:with_columns) || default_columns).map do |column|
mock_column(column[0], column[1], column[2]) mock_column(column[0], column[1], column[2])
end end
...@@ -617,6 +617,71 @@ EOS ...@@ -617,6 +617,71 @@ EOS
# #
EOS EOS
end end
describe 'with_comment option' do
mocked_columns_with_comment = [
[:id, :integer, { limit: 8, comment: 'ID' }],
[:active, :boolean, { limit: 1, comment: 'Active' }],
[:name, :string, { limit: 50, comment: 'Name' }],
[:notes, :text, { limit: 55, comment: 'Notes' }]
]
when_called_with with_comment: 'yes',
with_columns: mocked_columns_with_comment, returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# id(ID) :integer not null, primary key
# active(Active) :boolean not null
# name(Name) :string(50) not null
# notes(Notes) :text(55) not null
#
EOS
it 'should get schema info as RDoc' 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_rdoc: true, with_comment: true)).to eql(<<-EOS.strip_heredoc)
# #{AnnotateModels::PREFIX}
#
# Table name: users
#
# *id(ID)*:: <tt>integer, not null, primary key</tt>
# *name(Name)*:: <tt>string(50), not null</tt>
#--
# #{AnnotateModels::END_MARK}
#++
EOS
end
it 'should get schema info as Markdown' 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
end
end end
describe '#get_model_class' do describe '#get_model_class' do
......
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