Skip to content

Commit a749982

Browse files
committed
add table comment annotation
use hash parameter for fixing rubocop error 'Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [6/5]'
1 parent 7d211dd commit a749982

File tree

2 files changed

+94
-33
lines changed

2 files changed

+94
-33
lines changed

lib/annotate/annotate_models.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,16 @@ def get_schema_info(klass, header, options = {})
182182
end
183183

184184
def get_schema_header_text(klass, options = {})
185+
table_comment = with_table_comments?(klass, options) ? "(#{klass.connection.table_comment(klass.table_name)})" : ''
186+
table_name = klass.table_name.to_s + table_comment
187+
185188
info = "#\n"
186189
if options[:format_markdown]
187-
info << "# Table name: `#{klass.table_name}`\n"
190+
info << "# Table name: `#{table_name}`\n"
188191
info << "#\n"
189192
info << "# ### Columns\n"
190193
else
191-
info << "# Table name: #{klass.table_name}\n"
194+
info << "# Table name: #{table_name}\n"
192195
end
193196
info << "#\n"
194197
end
@@ -762,6 +765,12 @@ def with_comments?(klass, options)
762765
klass.columns.any? { |col| !col.comment.nil? }
763766
end
764767

768+
def with_table_comments?(klass, options)
769+
options[:with_comment] &&
770+
klass.connection.respond_to?(:table_comment) &&
771+
klass.connection.table_comment(klass.table_name).present?
772+
end
773+
765774
def max_schema_info_width(klass, options)
766775
cols = columns(klass, options)
767776

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,26 @@ def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints
4141
on_update: constraints[:on_update])
4242
end
4343

44-
def mock_connection(indexes = [], foreign_keys = [])
45-
double('Conn',
46-
indexes: indexes,
47-
foreign_keys: foreign_keys,
48-
supports_foreign_keys?: true)
44+
def mock_connection(options)
45+
default_options = {
46+
indexes: [],
47+
foreign_keys: [],
48+
supports_foreign_keys?: true,
49+
table_comment: nil
50+
}
51+
52+
double('Conn', default_options.merge(options))
4953
end
5054

51-
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [])
55+
def mock_class(table_name, primary_key, columns, connection_options: {})
5256
options = {
53-
connection: mock_connection(indexes, foreign_keys),
54-
table_exists?: true,
55-
table_name: table_name,
56-
primary_key: primary_key,
57-
column_names: columns.map { |col| col.name.to_s },
58-
columns: columns,
59-
column_defaults: Hash[columns.map { |col| [col.name, col.default] }],
57+
connection: mock_connection(connection_options),
58+
table_exists?: true,
59+
table_name: table_name,
60+
primary_key: primary_key,
61+
column_names: columns.map { |col| col.name.to_s },
62+
columns: columns,
63+
column_defaults: Hash[columns.map { |col| [col.name, col.default] }],
6064
table_name_prefix: ''
6165
}
6266

@@ -217,7 +221,14 @@ def mock_column(name, type, options = {})
217221
end
218222

219223
let :klass do
220-
mock_class(:users, primary_key, columns, indexes, foreign_keys)
224+
mock_class(:users,
225+
primary_key,
226+
columns,
227+
connection_options: {
228+
indexes: indexes,
229+
foreign_keys: foreign_keys,
230+
table_comment: table_comment
231+
})
221232
end
222233

223234
let :indexes do
@@ -228,6 +239,10 @@ def mock_column(name, type, options = {})
228239
[]
229240
end
230241

242+
let :table_comment do
243+
nil
244+
end
245+
231246
context 'when option is not present' do
232247
let :options do
233248
{}
@@ -400,7 +415,13 @@ def mock_column(name, type, options = {})
400415
end
401416

402417
let :klass do
403-
mock_class(:posts, primary_key, columns, indexes, foreign_keys).tap do |mock_klass|
418+
mock_class(:posts,
419+
primary_key,
420+
columns,
421+
connection_options: {
422+
indexes: indexes,
423+
foreign_keys: foreign_keys
424+
}).tap do |mock_klass|
404425
allow(mock_klass).to receive(:translation_class).and_return(translation_klass)
405426
end
406427
end
@@ -1061,6 +1082,33 @@ def mock_column(name, type, options = {})
10611082
{ with_comment: 'yes' }
10621083
end
10631084

1085+
context 'when table have comments' do
1086+
let :table_comment do
1087+
'users table comment'
1088+
end
1089+
1090+
let :columns do
1091+
[
1092+
mock_column(:id, :integer, limit: 8),
1093+
]
1094+
end
1095+
1096+
let :expected_result do
1097+
<<~EOS
1098+
# Schema Info
1099+
#
1100+
# Table name: users(users table comment)
1101+
#
1102+
# id :integer not null, primary key
1103+
#
1104+
EOS
1105+
end
1106+
1107+
it 'works with option "with_comment"' do
1108+
is_expected.to eq expected_result
1109+
end
1110+
end
1111+
10641112
context 'when columns have comments' do
10651113
let :columns do
10661114
[
@@ -2566,14 +2614,16 @@ def annotate_one_file(options = {})
25662614
mock_column(:id, :integer),
25672615
mock_column(:foreign_thing_id, :integer)
25682616
],
2569-
[],
2570-
[
2571-
mock_foreign_key('fk_rails_cf2568e89e',
2572-
'foreign_thing_id',
2573-
'foreign_things',
2574-
'id',
2575-
on_delete: :cascade)
2576-
])
2617+
connection_options: {
2618+
indexes: [],
2619+
foreign_keys: [
2620+
mock_foreign_key('fk_rails_cf2568e89e',
2621+
'foreign_thing_id',
2622+
'foreign_things',
2623+
'id',
2624+
on_delete: :cascade)
2625+
]
2626+
})
25772627
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_foreign_keys: true)
25782628
annotate_one_file
25792629
end
@@ -2585,14 +2635,16 @@ def annotate_one_file(options = {})
25852635
mock_column(:id, :integer),
25862636
mock_column(:foreign_thing_id, :integer)
25872637
],
2588-
[],
2589-
[
2590-
mock_foreign_key('fk_rails_cf2568e89e',
2591-
'foreign_thing_id',
2592-
'foreign_things',
2593-
'id',
2594-
on_delete: :restrict)
2595-
])
2638+
connection_options: {
2639+
indexes: [],
2640+
foreign_keys: [
2641+
mock_foreign_key('fk_rails_cf2568e89e',
2642+
'foreign_thing_id',
2643+
'foreign_things',
2644+
'id',
2645+
on_delete: :restrict)
2646+
]
2647+
})
25962648
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_foreign_keys: true)
25972649
annotate_one_file
25982650
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")

0 commit comments

Comments
 (0)