Commit 7a96e901 by Doug Hammond Committed by Cuong Tran

Make comment annotations work (#518)

* Hook up with-comment option to rake tasks and CL. * with_comment option handles commentless columns. * Style fixes.
parent 2a977bfc
# This configuration was generated by # This configuration was generated by
# `rubocop --auto-gen-config` # `rubocop --auto-gen-config`
# on 2016-12-17 10:16:05 +0100 using RuboCop version 0.46.0. # on 2017-10-13 10:01:18 +0200 using RuboCop version 0.46.0.
# The point is for the user to remove these configuration records # The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new # Note that changes in the inspected code, or installation of new
...@@ -65,11 +65,6 @@ Lint/HandleExceptions: ...@@ -65,11 +65,6 @@ Lint/HandleExceptions:
Exclude: Exclude:
- 'bin/annotate' - 'bin/annotate'
# Offense count: 8
Lint/IneffectiveAccessModifier:
Exclude:
- 'lib/annotate/annotate_routes.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
...@@ -88,62 +83,51 @@ Lint/ShadowingOuterLocalVariable: ...@@ -88,62 +83,51 @@ Lint/ShadowingOuterLocalVariable:
Exclude: Exclude:
- 'Rakefile' - 'Rakefile'
# Offense count: 6 # Offense count: 7
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments. # Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
Lint/UnusedBlockArgument: Lint/UnusedBlockArgument:
Exclude: Exclude:
- 'bin/annotate' - 'bin/annotate'
# Offense count: 1 # Offense count: 18
# Configuration parameters: ContextCreatingMethods.
Lint/UselessAccessModifier:
Exclude:
- 'lib/annotate/annotate_routes.rb'
# Offense count: 17
Metrics/AbcSize: Metrics/AbcSize:
Max: 159 Max: 142
# Offense count: 3 # Offense count: 3
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/BlockLength: Metrics/BlockLength:
Max: 140 Max: 142
# Offense count: 2 # Offense count: 2
Metrics/BlockNesting: Metrics/BlockNesting:
Max: 4 Max: 4
# Offense count: 8 # Offense count: 9
Metrics/CyclomaticComplexity: Metrics/CyclomaticComplexity:
Max: 42 Max: 36
# Offense count: 350 # Offense count: 380
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https # URISchemes: http, https
Metrics/LineLength: Metrics/LineLength:
Max: 543 Max: 276
# Offense count: 24 # Offense count: 26
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/MethodLength: Metrics/MethodLength:
Max: 79 Max: 75
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 116
# Offense count: 7 # Offense count: 7
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:
Max: 48 Max: 42
# Offense count: 1 # Offense count: 1
Style/AccessorMethodName: Style/AccessorMethodName:
Exclude: Exclude:
- 'lib/annotate.rb' - 'lib/annotate.rb'
# Offense count: 3 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/AlignArray: Style/AlignArray:
Exclude: Exclude:
...@@ -259,7 +243,7 @@ Style/ExtraSpacing: ...@@ -259,7 +243,7 @@ Style/ExtraSpacing:
- 'spec/integration/rails_4.2.0/Gemfile' - 'spec/integration/rails_4.2.0/Gemfile'
- 'spec/integration/rails_4.2.0/config.ru' - 'spec/integration/rails_4.2.0/config.ru'
# Offense count: 9 # Offense count: 10
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: format, sprintf, percent # SupportedStyles: format, sprintf, percent
Style/FormatString: Style/FormatString:
...@@ -267,13 +251,6 @@ Style/FormatString: ...@@ -267,13 +251,6 @@ Style/FormatString:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
# Offense count: 181
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: when_needed, always
Style/FrozenStringLiteralComment:
Enabled: false
# Offense count: 7 # Offense count: 7
# Configuration parameters: MinBodyLength. # Configuration parameters: MinBodyLength.
Style/GuardClause: Style/GuardClause:
...@@ -356,16 +333,6 @@ Style/NumericLiterals: ...@@ -356,16 +333,6 @@ Style/NumericLiterals:
# Offense count: 2 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles.
# SupportedStyles: predicate, comparison
Style/NumericPredicate:
Exclude:
- 'spec/**/*'
- 'lib/annotate.rb'
- 'lib/annotate/annotate_models.rb'
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters. # Configuration parameters: PreferredDelimiters.
Style/PercentLiteralDelimiters: Style/PercentLiteralDelimiters:
Exclude: Exclude:
...@@ -458,16 +425,15 @@ Style/SpaceAroundKeyword: ...@@ -458,16 +425,15 @@ Style/SpaceAroundKeyword:
- 'spec/integration/rails_4.2.0/Gemfile' - 'spec/integration/rails_4.2.0/Gemfile'
- 'spec/integration/standalone/Gemfile' - 'spec/integration/standalone/Gemfile'
# Offense count: 6 # Offense count: 4
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment. # Configuration parameters: AllowForAlignment.
Style/SpaceAroundOperators: Style/SpaceAroundOperators:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'lib/tasks/annotate_models.rake'
- 'lib/tasks/annotate_routes.rake' - 'lib/tasks/annotate_routes.rake'
# Offense count: 4 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: space, no_space # SupportedStyles: space, no_space
...@@ -517,14 +483,14 @@ Style/SpaceInsideStringInterpolation: ...@@ -517,14 +483,14 @@ Style/SpaceInsideStringInterpolation:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 223 # Offense count: 237
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. # Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes # SupportedStyles: single_quotes, double_quotes
Style/StringLiterals: Style/StringLiterals:
Enabled: false Enabled: false
# Offense count: 2 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: single_quotes, double_quotes # SupportedStyles: single_quotes, double_quotes
...@@ -581,7 +547,7 @@ Style/UnneededInterpolation: ...@@ -581,7 +547,7 @@ Style/UnneededInterpolation:
Exclude: Exclude:
- 'bin/annotate' - 'bin/annotate'
# Offense count: 8 # Offense count: 4
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/UnneededPercentQ: Style/UnneededPercentQ:
Exclude: Exclude:
......
...@@ -197,6 +197,10 @@ OptionParser.new do |opts| ...@@ -197,6 +197,10 @@ OptionParser.new do |opts|
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values| opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values|
ENV['ignore_unknown_models'] = 'true' ENV['ignore_unknown_models'] = 'true'
end end
opts.on('--with-comment', "include database comments") do |values|
ENV['with_comment'] = 'true'
end
end.parse! end.parse!
options = Annotate.setup_options( options = Annotate.setup_options(
......
...@@ -33,7 +33,7 @@ module Annotate ...@@ -33,7 +33,7 @@ module Annotate
:timestamp, :exclude_serializers, :classified_sort, :timestamp, :exclude_serializers, :classified_sort,
:show_foreign_keys, :show_complete_foreign_keys, :show_foreign_keys, :show_complete_foreign_keys,
:exclude_scaffolds, :exclude_controllers, :exclude_helpers, :exclude_scaffolds, :exclude_controllers, :exclude_helpers,
:exclude_sti_subclasses, :ignore_unknown_models :exclude_sti_subclasses, :ignore_unknown_models, :with_comment
].freeze ].freeze
OTHER_OPTIONS = [ OTHER_OPTIONS = [
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,
......
...@@ -224,11 +224,7 @@ module AnnotateModels ...@@ -224,11 +224,7 @@ module AnnotateModels
info = "# #{header}\n" info = "# #{header}\n"
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 = max_schema_info_width(klass, options)
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
md_names_overhead = 6 md_names_overhead = 6
md_type_allowance = 18 md_type_allowance = 18
bare_type_allowance = 16 bare_type_allowance = 16
...@@ -291,7 +287,7 @@ module AnnotateModels ...@@ -291,7 +287,7 @@ module AnnotateModels
end end
end end
end end
col_name = if with_comment col_name = if with_comments?(klass, options) && col.comment
"#{col.name}(#{col.comment})" "#{col.name}(#{col.comment})"
else else
col.name col.name
...@@ -861,6 +857,28 @@ module AnnotateModels ...@@ -861,6 +857,28 @@ module AnnotateModels
ensure ensure
$VERBOSE = old_verbose $VERBOSE = old_verbose
end end
private
def with_comments?(klass, options)
options[:with_comment] &&
klass.columns.first.respond_to?(:comment) &&
klass.columns.any? { |col| !col.comment.nil? }
end
def max_schema_info_width(klass, options)
if with_comments?(klass, options)
max_size = klass.columns.map do |column|
column.name.size + (column.comment ? column.comment.size : 0)
end.max || 0
max_size += 2
else
max_size = klass.column_names.map(&:size).max
end
max_size += options[:format_rdoc] ? 5 : 1
max_size
end
end end
class BadModelFileError < LoadError class BadModelFileError < LoadError
......
...@@ -47,6 +47,7 @@ task annotate_models: :environment do ...@@ -47,6 +47,7 @@ task annotate_models: :environment do
options[:ignore_routes] = ENV.fetch('ignore_routes', nil) options[:ignore_routes] = ENV.fetch('ignore_routes', nil)
options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '') options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '')
options[:hide_default_column_types] = Annotate.fallback(ENV['hide_default_column_types'], '') options[:hide_default_column_types] = Annotate.fallback(ENV['hide_default_column_types'], '')
options[:with_comment] = Annotate.fallback(ENV['with_comment'], '')
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
end end
......
...@@ -888,10 +888,11 @@ EOS ...@@ -888,10 +888,11 @@ EOS
describe 'with_comment option' do describe 'with_comment option' do
mocked_columns_with_comment = [ mocked_columns_with_comment = [
[:id, :integer, { limit: 8, comment: 'ID' }], [:id, :integer, { limit: 8, comment: 'ID' }],
[:active, :boolean, { limit: 1, comment: 'Active' }], [:active, :boolean, { limit: 1, comment: 'Active' }],
[:name, :string, { limit: 50, comment: 'Name' }], [:name, :string, { limit: 50, comment: 'Name' }],
[:notes, :text, { limit: 55, comment: 'Notes' }] [:notes, :text, { limit: 55, comment: 'Notes' }],
[:no_comment, :text, { limit: 20, comment: nil }]
] ]
when_called_with with_comment: 'yes', when_called_with with_comment: 'yes',
...@@ -905,6 +906,7 @@ EOS ...@@ -905,6 +906,7 @@ EOS
# active(Active) :boolean not null # active(Active) :boolean not null
# name(Name) :string(50) not null # name(Name) :string(50) not null
# notes(Notes) :text(55) not null # notes(Notes) :text(55) not null
# no_comment :text(20) not null
# #
EOS EOS
......
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