Skip to content

Commit 7a96e90

Browse files
dslhctran
authored andcommitted
Make comment annotations work (#518)
* Hook up with-comment option to rake tasks and CL. * with_comment option handles commentless columns. * Style fixes.
1 parent 2a977bf commit 7a96e90

File tree

6 files changed

+55
-64
lines changed

6 files changed

+55
-64
lines changed

.rubocop_todo.yml

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2016-12-17 10:16:05 +0100 using RuboCop version 0.46.0.
3+
# on 2017-10-13 10:01:18 +0200 using RuboCop version 0.46.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -65,11 +65,6 @@ Lint/HandleExceptions:
6565
Exclude:
6666
- 'bin/annotate'
6767

68-
# Offense count: 8
69-
Lint/IneffectiveAccessModifier:
70-
Exclude:
71-
- 'lib/annotate/annotate_routes.rb'
72-
7368
# Offense count: 1
7469
# Cop supports --auto-correct.
7570
# Configuration parameters: EnforcedStyle, SupportedStyles.
@@ -88,62 +83,51 @@ Lint/ShadowingOuterLocalVariable:
8883
Exclude:
8984
- 'Rakefile'
9085

91-
# Offense count: 6
86+
# Offense count: 7
9287
# Cop supports --auto-correct.
9388
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
9489
Lint/UnusedBlockArgument:
9590
Exclude:
9691
- 'bin/annotate'
9792

98-
# Offense count: 1
99-
# Configuration parameters: ContextCreatingMethods.
100-
Lint/UselessAccessModifier:
101-
Exclude:
102-
- 'lib/annotate/annotate_routes.rb'
103-
104-
# Offense count: 17
93+
# Offense count: 18
10594
Metrics/AbcSize:
106-
Max: 159
95+
Max: 142
10796

10897
# Offense count: 3
10998
# Configuration parameters: CountComments.
11099
Metrics/BlockLength:
111-
Max: 140
100+
Max: 142
112101

113102
# Offense count: 2
114103
Metrics/BlockNesting:
115104
Max: 4
116105

117-
# Offense count: 8
106+
# Offense count: 9
118107
Metrics/CyclomaticComplexity:
119-
Max: 42
108+
Max: 36
120109

121-
# Offense count: 350
110+
# Offense count: 380
122111
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
123112
# URISchemes: http, https
124113
Metrics/LineLength:
125-
Max: 543
114+
Max: 276
126115

127-
# Offense count: 24
116+
# Offense count: 26
128117
# Configuration parameters: CountComments.
129118
Metrics/MethodLength:
130-
Max: 79
131-
132-
# Offense count: 1
133-
# Configuration parameters: CountComments.
134-
Metrics/ModuleLength:
135-
Max: 116
119+
Max: 75
136120

137121
# Offense count: 7
138122
Metrics/PerceivedComplexity:
139-
Max: 48
123+
Max: 42
140124

141125
# Offense count: 1
142126
Style/AccessorMethodName:
143127
Exclude:
144128
- 'lib/annotate.rb'
145129

146-
# Offense count: 3
130+
# Offense count: 2
147131
# Cop supports --auto-correct.
148132
Style/AlignArray:
149133
Exclude:
@@ -259,21 +243,14 @@ Style/ExtraSpacing:
259243
- 'spec/integration/rails_4.2.0/Gemfile'
260244
- 'spec/integration/rails_4.2.0/config.ru'
261245

262-
# Offense count: 9
246+
# Offense count: 10
263247
# Configuration parameters: EnforcedStyle, SupportedStyles.
264248
# SupportedStyles: format, sprintf, percent
265249
Style/FormatString:
266250
Exclude:
267251
- 'lib/annotate/annotate_models.rb'
268252
- 'lib/annotate/annotate_routes.rb'
269253

270-
# Offense count: 181
271-
# Cop supports --auto-correct.
272-
# Configuration parameters: EnforcedStyle, SupportedStyles.
273-
# SupportedStyles: when_needed, always
274-
Style/FrozenStringLiteralComment:
275-
Enabled: false
276-
277254
# Offense count: 7
278255
# Configuration parameters: MinBodyLength.
279256
Style/GuardClause:
@@ -356,16 +333,6 @@ Style/NumericLiterals:
356333

357334
# Offense count: 2
358335
# Cop supports --auto-correct.
359-
# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles.
360-
# SupportedStyles: predicate, comparison
361-
Style/NumericPredicate:
362-
Exclude:
363-
- 'spec/**/*'
364-
- 'lib/annotate.rb'
365-
- 'lib/annotate/annotate_models.rb'
366-
367-
# Offense count: 6
368-
# Cop supports --auto-correct.
369336
# Configuration parameters: PreferredDelimiters.
370337
Style/PercentLiteralDelimiters:
371338
Exclude:
@@ -458,16 +425,15 @@ Style/SpaceAroundKeyword:
458425
- 'spec/integration/rails_4.2.0/Gemfile'
459426
- 'spec/integration/standalone/Gemfile'
460427

461-
# Offense count: 6
428+
# Offense count: 4
462429
# Cop supports --auto-correct.
463430
# Configuration parameters: AllowForAlignment.
464431
Style/SpaceAroundOperators:
465432
Exclude:
466433
- 'lib/annotate/annotate_models.rb'
467-
- 'lib/tasks/annotate_models.rake'
468434
- 'lib/tasks/annotate_routes.rake'
469435

470-
# Offense count: 4
436+
# Offense count: 1
471437
# Cop supports --auto-correct.
472438
# Configuration parameters: EnforcedStyle, SupportedStyles.
473439
# SupportedStyles: space, no_space
@@ -517,14 +483,14 @@ Style/SpaceInsideStringInterpolation:
517483
Exclude:
518484
- 'lib/annotate/annotate_models.rb'
519485

520-
# Offense count: 223
486+
# Offense count: 237
521487
# Cop supports --auto-correct.
522488
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
523489
# SupportedStyles: single_quotes, double_quotes
524490
Style/StringLiterals:
525491
Enabled: false
526492

527-
# Offense count: 2
493+
# Offense count: 1
528494
# Cop supports --auto-correct.
529495
# Configuration parameters: EnforcedStyle, SupportedStyles.
530496
# SupportedStyles: single_quotes, double_quotes
@@ -581,7 +547,7 @@ Style/UnneededInterpolation:
581547
Exclude:
582548
- 'bin/annotate'
583549

584-
# Offense count: 8
550+
# Offense count: 4
585551
# Cop supports --auto-correct.
586552
Style/UnneededPercentQ:
587553
Exclude:

bin/annotate

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ OptionParser.new do |opts|
197197
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values|
198198
ENV['ignore_unknown_models'] = 'true'
199199
end
200+
201+
opts.on('--with-comment', "include database comments") do |values|
202+
ENV['with_comment'] = 'true'
203+
end
200204
end.parse!
201205

202206
options = Annotate.setup_options(

lib/annotate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Annotate
3333
:timestamp, :exclude_serializers, :classified_sort,
3434
:show_foreign_keys, :show_complete_foreign_keys,
3535
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
36-
:exclude_sti_subclasses, :ignore_unknown_models
36+
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment
3737
].freeze
3838
OTHER_OPTIONS = [
3939
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,

lib/annotate/annotate_models.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,7 @@ def get_schema_info(klass, header, options = {})
224224
info = "# #{header}\n"
225225
info << get_schema_header_text(klass, options)
226226

227-
max_size = klass.column_names.map(&:size).max || 0
228-
with_comment = options[:with_comment] && klass.columns.first.respond_to?(:comment)
229-
max_size = klass.columns.map{|col| col.name.size + col.comment.size }.max || 0 if with_comment
230-
max_size += 2 if with_comment
231-
max_size += options[:format_rdoc] ? 5 : 1
227+
max_size = max_schema_info_width(klass, options)
232228
md_names_overhead = 6
233229
md_type_allowance = 18
234230
bare_type_allowance = 16
@@ -291,7 +287,7 @@ def get_schema_info(klass, header, options = {})
291287
end
292288
end
293289
end
294-
col_name = if with_comment
290+
col_name = if with_comments?(klass, options) && col.comment
295291
"#{col.name}(#{col.comment})"
296292
else
297293
col.name
@@ -861,6 +857,28 @@ def silence_warnings
861857
ensure
862858
$VERBOSE = old_verbose
863859
end
860+
861+
private
862+
863+
def with_comments?(klass, options)
864+
options[:with_comment] &&
865+
klass.columns.first.respond_to?(:comment) &&
866+
klass.columns.any? { |col| !col.comment.nil? }
867+
end
868+
869+
def max_schema_info_width(klass, options)
870+
if with_comments?(klass, options)
871+
max_size = klass.columns.map do |column|
872+
column.name.size + (column.comment ? column.comment.size : 0)
873+
end.max || 0
874+
max_size += 2
875+
else
876+
max_size = klass.column_names.map(&:size).max
877+
end
878+
max_size += options[:format_rdoc] ? 5 : 1
879+
880+
max_size
881+
end
864882
end
865883

866884
class BadModelFileError < LoadError

lib/tasks/annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ task annotate_models: :environment do
4747
options[:ignore_routes] = ENV.fetch('ignore_routes', nil)
4848
options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '')
4949
options[:hide_default_column_types] = Annotate.fallback(ENV['hide_default_column_types'], '')
50+
options[:with_comment] = Annotate.fallback(ENV['with_comment'], '')
5051

5152
AnnotateModels.do_annotations(options)
5253
end

spec/annotate/annotate_models_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,11 @@ def self.when_called_with(options = {})
888888

889889
describe 'with_comment option' do
890890
mocked_columns_with_comment = [
891-
[:id, :integer, { limit: 8, comment: 'ID' }],
892-
[:active, :boolean, { limit: 1, comment: 'Active' }],
893-
[:name, :string, { limit: 50, comment: 'Name' }],
894-
[:notes, :text, { limit: 55, comment: 'Notes' }]
891+
[:id, :integer, { limit: 8, comment: 'ID' }],
892+
[:active, :boolean, { limit: 1, comment: 'Active' }],
893+
[:name, :string, { limit: 50, comment: 'Name' }],
894+
[:notes, :text, { limit: 55, comment: 'Notes' }],
895+
[:no_comment, :text, { limit: 20, comment: nil }]
895896
]
896897

897898
when_called_with with_comment: 'yes',
@@ -905,6 +906,7 @@ def self.when_called_with(options = {})
905906
# active(Active) :boolean not null
906907
# name(Name) :string(50) not null
907908
# notes(Notes) :text(55) not null
909+
# no_comment :text(20) not null
908910
#
909911
EOS
910912

0 commit comments

Comments
 (0)