Skip to content

Commit b1f9489

Browse files
mototctran
authored andcommitted
Support database comment for rails 5 (#459)
Support Database Comment for Rails 5 Added with_comment options
1 parent 5394146 commit b1f9489

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

.rubocop_todo.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Lint/UselessAccessModifier:
103103

104104
# Offense count: 17
105105
Metrics/AbcSize:
106-
Max: 146
106+
Max: 159
107107

108108
# Offense count: 3
109109
# Configuration parameters: CountComments.
@@ -116,7 +116,7 @@ Metrics/BlockNesting:
116116

117117
# Offense count: 8
118118
Metrics/CyclomaticComplexity:
119-
Max: 37
119+
Max: 42
120120

121121
# Offense count: 350
122122
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
@@ -127,7 +127,7 @@ Metrics/LineLength:
127127
# Offense count: 24
128128
# Configuration parameters: CountComments.
129129
Metrics/MethodLength:
130-
Max: 71
130+
Max: 79
131131

132132
# Offense count: 1
133133
# Configuration parameters: CountComments.
@@ -136,7 +136,7 @@ Metrics/ModuleLength:
136136

137137
# Offense count: 7
138138
Metrics/PerceivedComplexity:
139-
Max: 42
139+
Max: 48
140140

141141
# Offense count: 1
142142
Style/AccessorMethodName:

lib/annotate/annotate_models.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ def get_schema_info(klass, header, options = {})
208208
info << get_schema_header_text(klass, options)
209209

210210
max_size = klass.column_names.map(&:size).max || 0
211+
with_comment = options[:with_comment] && klass.columns.first.respond_to?(:comment)
212+
max_size = klass.columns.map{|col| col.name.size + col.comment.size }.max || 0 if with_comment
213+
max_size += 2 if with_comment
211214
max_size += options[:format_rdoc] ? 5 : 1
212215
md_names_overhead = 6
213216
md_type_allowance = 18
@@ -271,15 +274,19 @@ def get_schema_info(klass, header, options = {})
271274
end
272275
end
273276
end
274-
277+
col_name = if with_comment
278+
"#{col.name}(#{col.comment})"
279+
else
280+
col.name
281+
end
275282
if options[:format_rdoc]
276-
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
283+
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
277284
elsif options[:format_markdown]
278-
name_remainder = max_size - col.name.length
285+
name_remainder = max_size - col_name.length
279286
type_remainder = (md_type_allowance - 2) - col_type.length
280-
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col.name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
287+
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
281288
else
282-
info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
289+
info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col_name, col_type, attrs.join(", ")).rstrip + "\n"
283290
end
284291
end
285292

lib/generators/annotate/templates/auto_annotate_models.rake

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ if Rails.env.development?
4444
'force' => 'false',
4545
'trace' => 'false',
4646
'wrapper_open' => nil,
47-
'wrapper_close' => nil
47+
'wrapper_close' => nil,
48+
'with_comment' => true
4849
)
4950
end
5051

spec/annotate/annotate_models_spec.rb

+66-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def self.when_called_with(options = {})
494494
[:notes, :text, { limit: 55 }]
495495
]
496496

497-
it 'should work with options = #{options}' do
497+
it "should work with options = #{options}" do
498498
with_columns = (options.delete(:with_columns) || default_columns).map do |column|
499499
mock_column(column[0], column[1], column[2])
500500
end
@@ -617,6 +617,71 @@ def self.when_called_with(options = {})
617617
#
618618
EOS
619619
end
620+
621+
describe 'with_comment option' do
622+
mocked_columns_with_comment = [
623+
[:id, :integer, { limit: 8, comment: 'ID' }],
624+
[:active, :boolean, { limit: 1, comment: 'Active' }],
625+
[:name, :string, { limit: 50, comment: 'Name' }],
626+
[:notes, :text, { limit: 55, comment: 'Notes' }]
627+
]
628+
629+
when_called_with with_comment: 'yes',
630+
with_columns: mocked_columns_with_comment, returns:
631+
<<-EOS.strip_heredoc
632+
# Schema Info
633+
#
634+
# Table name: users
635+
#
636+
# id(ID) :integer not null, primary key
637+
# active(Active) :boolean not null
638+
# name(Name) :string(50) not null
639+
# notes(Notes) :text(55) not null
640+
#
641+
EOS
642+
643+
it 'should get schema info as RDoc' do
644+
klass = mock_class(:users,
645+
:id,
646+
[
647+
mock_column(:id, :integer, comment: 'ID'),
648+
mock_column(:name, :string, limit: 50, comment: 'Name')
649+
])
650+
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)).to eql(<<-EOS.strip_heredoc)
651+
# #{AnnotateModels::PREFIX}
652+
#
653+
# Table name: users
654+
#
655+
# *id(ID)*:: <tt>integer, not null, primary key</tt>
656+
# *name(Name)*:: <tt>string(50), not null</tt>
657+
#--
658+
# #{AnnotateModels::END_MARK}
659+
#++
660+
EOS
661+
end
662+
663+
it 'should get schema info as Markdown' do
664+
klass = mock_class(:users,
665+
:id,
666+
[
667+
mock_column(:id, :integer, comment: 'ID'),
668+
mock_column(:name, :string, limit: 50, comment: 'Name')
669+
])
670+
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(<<-EOS.strip_heredoc)
671+
# #{AnnotateModels::PREFIX}
672+
#
673+
# Table name: `users`
674+
#
675+
# ### Columns
676+
#
677+
# Name | Type | Attributes
678+
# ----------------- | ------------------ | ---------------------------
679+
# **`id(ID)`** | `integer` | `not null, primary key`
680+
# **`name(Name)`** | `string(50)` | `not null`
681+
#
682+
EOS
683+
end
684+
end
620685
end
621686

622687
describe '#get_model_class' do

0 commit comments

Comments
 (0)