diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 000000000..ce531d377 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,13 @@ +Describe your problem here. + +## Commands + +``` +$ show your commands here. +``` + +## Version + +- annotate version +- rails version +- ruby version diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5828f8aee..578f1b345 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2017-10-13 10:01:18 +0200 using RuboCop version 0.46.0. +# on 2019-03-07 18:25:02 -0800 using RuboCop version 0.65.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -8,7 +8,7 @@ # Offense count: 9 # Configuration parameters: Include. -# Include: **/Gemfile, **/gems.rb +# Include: **/*.gemfile, **/Gemfile, **/gems.rb Bundler/DuplicatedGem: Exclude: - 'spec/integration/rails_2.3_with_bundler/Gemfile' @@ -22,13 +22,326 @@ Bundler/DuplicatedGem: - 'spec/integration/standalone/Gemfile' # Offense count: 2 -# Configuration parameters: Include. -# Include: **/Gemfile, **/gems.rb +# Cop supports --auto-correct. +# Configuration parameters: TreatCommentsAsGroupSeparators, Include. +# Include: **/*.gemfile, **/Gemfile, **/gems.rb Bundler/OrderedGems: Exclude: - 'spec/integration/rails_2.3_with_bundler/Gemfile' - 'spec/integration/rails_3.2_with_asset_pipeline/Gemfile' +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: TreatCommentsAsGroupSeparators, Include. +# Include: **/*.gemspec +Gemspec/OrderedDependencies: + Exclude: + - 'annotate.gemspec' + +# Offense count: 1 +# Configuration parameters: Include. +# Include: **/*.gemspec +Gemspec/RequiredRubyVersion: + Exclude: + - 'annotate.gemspec' + +# Offense count: 2 +# Cop supports --auto-correct. +Layout/AlignArray: + Exclude: + - 'spec/annotate/annotate_models_spec.rb' + +# Offense count: 103 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. +# SupportedHashRocketStyles: key, separator, table +# SupportedColonStyles: key, separator, table +# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit +Layout/AlignHash: + Exclude: + - 'lib/generators/annotate/templates/auto_annotate_models.rake' + - 'spec/annotate/annotate_models_spec.rb' + - 'spec/integration/rails_2.3_with_bundler/config/initializers/unified_initializer.rb' + - 'spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake' + - 'spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake' + - 'spec/integration/standalone/config/init.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleAlignWith. +# SupportedStylesAlignWith: either, start_of_block, start_of_line +Layout/BlockAlignment: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 45 +# Cop supports --auto-correct. +Layout/ClosingHeredocIndentation: + Exclude: + - 'spec/annotate/annotate_models_spec.rb' + - 'spec/integration/rails_2.3_with_bundler.rb' + - 'spec/integration/rails_3.2.2.rb' + - 'spec/integration/rails_3.2.8.rb' + - 'spec/integration/rails_3.2_autoloading_factory_girl.rb' + - 'spec/integration/rails_3.2_custom_inflections.rb' + - 'spec/integration/rails_3.2_with_asset_pipeline.rb' + - 'spec/integration/standalone.rb' + +# Offense count: 14 +# Cop supports --auto-correct. +Layout/EmptyLineAfterGuardClause: + Exclude: + - 'Rakefile' + - 'lib/annotate.rb' + - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes.rb' + - 'spec/integration/integration_spec.rb' + +# Offense count: 10 +# Cop supports --auto-correct. +Layout/EmptyLineAfterMagicComment: + Exclude: + - 'annotate.gemspec' + - 'spec/annotate/annotate_models_spec.rb' + - 'spec/integration/rails_3.2.2/db/schema.rb' + - 'spec/integration/rails_3.2.8/db/schema.rb' + - 'spec/integration/rails_3.2_autoloading_factory_girl/db/schema.rb' + - 'spec/integration/rails_3.2_custom_inflections/db/schema.rb' + - 'spec/integration/rails_3.2_with_asset_pipeline/db/schema.rb' + - 'spec/integration/rails_4.1.1/db/schema.rb' + - 'spec/integration/rails_4.2.0/db/schema.rb' + - 'spec/integration/standalone/db/schema.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Layout/EmptyLines: + Exclude: + - 'spec/integration/rails_4.1.1/Gemfile' + - 'spec/integration/rails_4.2.0/Gemfile' + +# Offense count: 1 +# Cop supports --auto-correct. +Layout/EmptyLinesAroundAccessModifier: + Exclude: + - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Layout/EmptyLinesAroundArguments: + Exclude: + - 'spec/annotate/annotate_routes_spec.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty_lines, no_empty_lines +Layout/EmptyLinesAroundBlockBody: + Exclude: + - 'spec/integration/rails_2.3_with_bundler/db/schema.rb' + - 'spec/integration/rails_4.1.1/db/schema.rb' + - 'spec/integration/rails_4.2.0/db/schema.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Layout/EmptyLinesAroundExceptionHandlingKeywords: + Exclude: + - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + +# Offense count: 24 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'Guardfile' + - 'lib/annotate/annotate_models.rb' + - 'lib/tasks/annotate_routes.rake' + - 'spec/integration/rails_2.3_with_bundler/script/console' + - 'spec/integration/rails_3.2.2/script/rails' + - 'spec/integration/rails_3.2.8/script/rails' + - 'spec/integration/rails_3.2_autoloading_factory_girl/script/rails' + - 'spec/integration/rails_3.2_custom_inflections/script/rails' + - 'spec/integration/rails_3.2_with_asset_pipeline/script/rails' + - 'spec/integration/rails_4.1.1/Gemfile' + - 'spec/integration/rails_4.1.1/config.ru' + - 'spec/integration/rails_4.2.0/Gemfile' + - 'spec/integration/rails_4.2.0/config.ru' + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: IndentationWidth. +# SupportedStyles: special_inside_parentheses, consistent, align_braces +Layout/IndentHash: + EnforcedStyle: consistent + +# Offense count: 54 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent +Layout/IndentHeredoc: + Exclude: + - 'spec/annotate/annotate_models_spec.rb' + - 'spec/integration/integration_spec.rb' + - 'spec/integration/rails_2.3_with_bundler.rb' + - 'spec/integration/rails_3.2.2.rb' + - 'spec/integration/rails_3.2.8.rb' + - 'spec/integration/rails_3.2_autoloading_factory_girl.rb' + - 'spec/integration/rails_3.2_custom_inflections.rb' + - 'spec/integration/rails_3.2_with_asset_pipeline.rb' + - 'spec/integration/standalone.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: normal, rails +Layout/IndentationConsistency: + Exclude: + - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: Width, IgnoredPatterns. +Layout/IndentationWidth: + Exclude: + - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + - 'spec/integration/rails_4.1.1/app/models/sub1/sub2/sub3/event.rb' + - 'spec/integration/rails_4.1.1/app/models/task.rb' + - 'spec/integration/rails_4.2.0/app/models/sub1/sub2/sub3/event.rb' + - 'spec/integration/rails_4.2.0/app/models/task.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: aligned, indented, indented_relative_to_receiver +Layout/MultilineMethodCallIndentation: + Exclude: + - 'spec/annotate/annotate_routes_spec.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: aligned, indented +Layout/MultilineOperationIndentation: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceAroundEqualsInParameterDefault: + Exclude: + - 'lib/annotate/annotate_routes.rb' + - 'spec/integration/common_validation.rb' + +# Offense count: 9 +# Cop supports --auto-correct. +Layout/SpaceAroundKeyword: + Exclude: + - 'spec/integration/rails_2.3_with_bundler/Gemfile' + - 'spec/integration/rails_3.2.2/Gemfile' + - 'spec/integration/rails_3.2.8/Gemfile' + - 'spec/integration/rails_3.2_autoloading_factory_girl/Gemfile' + - 'spec/integration/rails_3.2_custom_inflections/Gemfile' + - 'spec/integration/rails_3.2_with_asset_pipeline/Gemfile' + - 'spec/integration/rails_4.1.1/Gemfile' + - 'spec/integration/rails_4.2.0/Gemfile' + - 'spec/integration/standalone/Gemfile' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment. +Layout/SpaceAroundOperators: + Exclude: + - 'lib/annotate/annotate_models.rb' + - 'lib/tasks/annotate_routes.rake' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceBeforeBlockBraces: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Layout/SpaceBeforeComment: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBrackets. +# SupportedStyles: space, no_space, compact +# SupportedStylesForEmptyBrackets: space, no_space +Layout/SpaceInsideArrayLiteralBrackets: + Exclude: + - 'spec/integration/rails_2.3_with_bundler/config/environment.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideBlockBraces: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. +# SupportedStyles: space, no_space, compact +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideHashLiteralBraces: + Exclude: + - 'lib/tasks/annotate_models.rake' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceInsideParens: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceInsideStringInterpolation: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: IndentationWidth. +Layout/Tab: + Exclude: + - 'spec/integration/rails_4.1.1/app/models/sub1/sub2/sub3/event.rb' + - 'spec/integration/rails_4.1.1/app/models/task.rb' + - 'spec/integration/rails_4.2.0/app/models/sub1/sub2/sub3/event.rb' + - 'spec/integration/rails_4.2.0/app/models/task.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: final_newline, final_blank_line +Layout/TrailingBlankLines: + Exclude: + - 'spec/integration/rails_4.1.1/app/models/task_observer.rb' + - 'spec/integration/rails_4.1.1/config/initializers/cookies_serializer.rb' + - 'spec/integration/rails_4.2.0/app/models/task_observer.rb' + - 'spec/integration/rails_4.2.0/config/initializers/cookies_serializer.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: AllowInHeredoc. +Layout/TrailingWhitespace: + Exclude: + - 'spec/annotate/annotate_routes_spec.rb' + - 'spec/integration/rails_2.3_with_bundler/db/schema.rb' + # Offense count: 3 # Configuration parameters: AllowSafeAssignment. Lint/AssignmentInCondition: @@ -36,13 +349,11 @@ Lint/AssignmentInCondition: - 'lib/annotate/annotate_models.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' -# Offense count: 1 +# Offense count: 2 # Cop supports --auto-correct. -# Configuration parameters: AlignWith, SupportedStyles. -# SupportedStyles: either, start_of_block, start_of_line -Lint/BlockAlignment: +Lint/BigDecimalNew: Exclude: - - 'lib/annotate/annotate_models.rb' + - 'spec/annotate/annotate_models_spec.rb' # Offense count: 6 # Cop supports --auto-correct. @@ -67,17 +378,35 @@ Lint/HandleExceptions: # Offense count: 1 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. +# Configuration parameters: EnforcedStyle. # SupportedStyles: runtime_error, standard_error Lint/InheritException: Exclude: - 'lib/annotate/annotate_models.rb' +# Offense count: 3 +# Configuration parameters: MaximumRangeSize. +Lint/MissingCopEnableDirective: + Exclude: + - 'lib/annotate.rb' + - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes.rb' + # Offense count: 2 Lint/RescueException: Exclude: - 'Rakefile' +# Offense count: 5 +# Cop supports --auto-correct. +Lint/ScriptPermission: + Exclude: + - 'spec/integration/rails_3.2.2/Rakefile' + - 'spec/integration/rails_3.2.8/Rakefile' + - 'spec/integration/rails_3.2_autoloading_factory_girl/Rakefile' + - 'spec/integration/rails_3.2_custom_inflections/Rakefile' + - 'spec/integration/rails_3.2_with_asset_pipeline/Rakefile' + # Offense count: 1 Lint/ShadowingOuterLocalVariable: Exclude: @@ -92,50 +421,78 @@ Lint/UnusedBlockArgument: # Offense count: 18 Metrics/AbcSize: - Max: 142 + Max: 139 -# Offense count: 3 -# Configuration parameters: CountComments. +# Offense count: 29 +# Configuration parameters: CountComments, ExcludedMethods. +# ExcludedMethods: refine Metrics/BlockLength: - Max: 142 + Max: 1140 -# Offense count: 2 +# Offense count: 1 +# Configuration parameters: CountBlocks. Metrics/BlockNesting: Max: 4 -# Offense count: 9 +# Offense count: 10 Metrics/CyclomaticComplexity: Max: 36 -# Offense count: 380 -# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. -# URISchemes: http, https -Metrics/LineLength: - Max: 276 - -# Offense count: 26 -# Configuration parameters: CountComments. +# Offense count: 29 +# Configuration parameters: CountComments, ExcludedMethods. Metrics/MethodLength: Max: 75 -# Offense count: 7 +# Offense count: 8 Metrics/PerceivedComplexity: Max: 42 # Offense count: 1 -Style/AccessorMethodName: +Naming/AccessorMethodName: Exclude: - 'lib/annotate.rb' +# Offense count: 76 +# Configuration parameters: Blacklist. +# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$)) +Naming/HeredocDelimiterNaming: + Exclude: + - 'spec/annotate/annotate_models_spec.rb' + - 'spec/annotate/annotate_routes_spec.rb' + # Offense count: 2 +# Configuration parameters: EnforcedStyleForLeadingUnderscores. +# SupportedStylesForLeadingUnderscores: disallowed, required, optional +Naming/MemoizedInstanceVariableName: + Exclude: + - 'lib/annotate/annotate_routes.rb' + - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + +# Offense count: 1 +# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. +# AllowedNames: io, id, to, by, on, in, at, ip, db +Naming/UncommunicativeMethodParamName: + Exclude: + - 'Rakefile' + +# Offense count: 4 # Cop supports --auto-correct. -Style/AlignArray: +Performance/RegexpMatch: Exclude: - - 'spec/annotate/annotate_models_spec.rb' + - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes.rb' + - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + +# Offense count: 1 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: inline, group +Style/AccessModifierDeclarations: + Exclude: + - 'lib/annotate/annotate_models.rb' # Offense count: 2 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. +# Configuration parameters: EnforcedStyle. # SupportedStyles: percent_q, bare_percent Style/BarePercentLiterals: Exclude: @@ -143,7 +500,7 @@ Style/BarePercentLiterals: # Offense count: 3 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. +# Configuration parameters: EnforcedStyle. # SupportedStyles: braces, no_braces, context_dependent Style/BracesAroundHashParameters: Exclude: @@ -157,7 +514,8 @@ Style/CaseEquality: - 'lib/annotate/annotate_models.rb' # Offense count: 14 -# Configuration parameters: EnforcedStyle, SupportedStyles. +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, EnforcedStyle. # SupportedStyles: nested, compact Style/ClassAndModuleChildren: Exclude: @@ -183,12 +541,18 @@ Style/ClassVars: # Offense count: 1 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, SingleLineConditionsOnly. +# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions. # SupportedStyles: assign_to_condition, assign_inside_condition Style/ConditionalAssignment: Exclude: - 'bin/annotate' +# Offense count: 1 +# Cop supports --auto-correct. +Style/Dir: + Exclude: + - 'bin/annotate' + # Offense count: 7 Style/Documentation: Exclude: @@ -201,102 +565,96 @@ Style/Documentation: - 'lib/generators/annotate/install_generator.rb' - 'lib/tasks/annotate_models_migrate.rake' -# Offense count: 2 -# Cop supports --auto-correct. -Style/EmptyLines: - Exclude: - - 'spec/integration/rails_4.1.1/Gemfile' - - 'spec/integration/rails_4.2.0/Gemfile' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/EmptyLinesAroundAccessModifier: - Exclude: - - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - -# Offense count: 6 +# Offense count: 10 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: empty_lines, no_empty_lines -Style/EmptyLinesAroundBlockBody: +Style/Encoding: Exclude: - - 'spec/integration/rails_2.3_with_bundler/db/schema.rb' + - 'annotate.gemspec' + - 'spec/annotate/annotate_models_spec.rb' + - 'spec/integration/rails_3.2.2/db/schema.rb' + - 'spec/integration/rails_3.2.8/db/schema.rb' + - 'spec/integration/rails_3.2_autoloading_factory_girl/db/schema.rb' + - 'spec/integration/rails_3.2_custom_inflections/db/schema.rb' + - 'spec/integration/rails_3.2_with_asset_pipeline/db/schema.rb' - 'spec/integration/rails_4.1.1/db/schema.rb' - 'spec/integration/rails_4.2.0/db/schema.rb' + - 'spec/integration/standalone/db/schema.rb' -# Offense count: 23 +# Offense count: 48 # Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment. -Style/ExtraSpacing: - Exclude: - - 'Guardfile' - - 'lib/annotate/annotate_models.rb' - - 'lib/tasks/annotate_routes.rake' - - 'spec/integration/rails_2.3_with_bundler/script/console' - - 'spec/integration/rails_3.2.2/script/rails' - - 'spec/integration/rails_3.2.8/script/rails' - - 'spec/integration/rails_3.2_autoloading_factory_girl/script/rails' - - 'spec/integration/rails_3.2_custom_inflections/script/rails' - - 'spec/integration/rails_3.2_with_asset_pipeline/script/rails' - - 'spec/integration/rails_4.1.1/Gemfile' - - 'spec/integration/rails_4.1.1/config.ru' - - 'spec/integration/rails_4.2.0/Gemfile' - - 'spec/integration/rails_4.2.0/config.ru' +Style/ExpandPathArguments: + Enabled: false # Offense count: 10 -# Configuration parameters: EnforcedStyle, SupportedStyles. +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. # SupportedStyles: format, sprintf, percent Style/FormatString: Exclude: - 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_routes.rb' -# Offense count: 7 +# Offense count: 23 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: annotated, template, unannotated +Style/FormatStringToken: + Exclude: + - 'lib/annotate/annotate_models.rb' + +# Offense count: 184 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: when_needed, always, never +Style/FrozenStringLiteralComment: + Enabled: false + +# Offense count: 5 # Configuration parameters: MinBodyLength. Style/GuardClause: Exclude: - - 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_routes.rb' - 'lib/tasks/annotate_models_migrate.rake' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' # Offense count: 57 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys Style/HashSyntax: Enabled: false -# Offense count: 6 +# Offense count: 11 # Cop supports --auto-correct. -# Configuration parameters: SupportedStyles, IndentationWidth. -# SupportedStyles: special_inside_parentheses, consistent, align_braces -Style/IndentHash: - EnforcedStyle: consistent +Style/IfUnlessModifier: + Exclude: + - 'Rakefile' + - 'bin/annotate' + - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes.rb' # Offense count: 1 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: normal, rails -Style/IndentationConsistency: +# Configuration parameters: InverseMethods, InverseBlocks. +Style/InverseMethods: Exclude: - - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + - 'Rakefile' -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: Width. -Style/IndentationWidth: +# Offense count: 1 +Style/MethodMissingSuper: Exclude: - - 'spec/integration/rails_4.1.1/app/models/sub1/sub2/sub3/event.rb' - - 'spec/integration/rails_4.1.1/app/models/task.rb' - - 'spec/integration/rails_4.2.0/app/models/sub1/sub2/sub3/event.rb' - - 'spec/integration/rails_4.2.0/app/models/task.rb' + - 'lib/annotate/active_record_patch.rb' # Offense count: 1 -Style/MethodMissing: +Style/MissingRespondToMissing: Exclude: - 'lib/annotate/active_record_patch.rb' +# Offense count: 3 +Style/MixinUsage: + Exclude: + - 'Rakefile' + - 'spec/integration/integration_spec.rb' + # Offense count: 3 Style/MultilineBlockChain: Exclude: @@ -306,37 +664,60 @@ Style/MultilineBlockChain: # Offense count: 5 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth. -# SupportedStyles: aligned, indented -Style/MultilineOperationIndentation: - Exclude: - - 'lib/annotate/annotate_models.rb' - -# Offense count: 3 -# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: literals, strict Style/MutableConstant: Exclude: + - 'lib/annotate.rb' + - 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_routes.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - 'spec/integration/rails_2.3_with_bundler/config/environment.rb' # Offense count: 1 # Cop supports --auto-correct. +# Configuration parameters: Whitelist. +# Whitelist: be, be_a, be_an, be_between, be_falsey, be_kind_of, be_instance_of, be_truthy, be_within, eq, eql, end_with, include, match, raise_error, respond_to, start_with Style/NestedParenthesizedCalls: Exclude: - 'bin/annotate' # Offense count: 9 # Cop supports --auto-correct. +# Configuration parameters: Strict. Style/NumericLiterals: MinDigits: 15 # Offense count: 2 # Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, EnforcedStyle, IgnoredMethods. +# SupportedStyles: predicate, comparison +Style/NumericPredicate: + Exclude: + - 'spec/**/*' + - 'lib/annotate.rb' + - 'lib/annotate/annotate_models.rb' + +# Offense count: 24 +# Cop supports --auto-correct. # Configuration parameters: PreferredDelimiters. Style/PercentLiteralDelimiters: Exclude: - 'annotate.gemspec' + - 'bin/annotate' + - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes.rb' + - 'lib/tasks/annotate_models_migrate.rake' + - 'spec/annotate/annotate_models_spec.rb' + - 'spec/integration/rails_3.2.2/config/application.rb' + - 'spec/integration/rails_3.2.8/config/application.rb' + - 'spec/integration/rails_3.2_autoloading_factory_girl/config/application.rb' + - 'spec/integration/rails_3.2_custom_inflections/config/application.rb' + - 'spec/integration/rails_3.2_with_asset_pipeline/config/application.rb' + - 'spec/integration/rails_4.1.1/app/models/task.rb' + - 'spec/integration/rails_4.2.0/app/models/task.rb' + - 'spec/spec_helper.rb' + - 'spec/tasks/annotate_models_migrate_spec.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -346,17 +727,18 @@ Style/PerlBackrefs: # Offense count: 1 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. +# Configuration parameters: EnforcedStyle. # SupportedStyles: compact, exploded Style/RaiseArgs: Exclude: - 'lib/annotate/annotate_models.rb' -# Offense count: 1 +# Offense count: 3 # Cop supports --auto-correct. Style/RedundantBegin: Exclude: - 'lib/annotate/annotate_models.rb' + - 'spec/annotate/annotate_models_spec.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -377,9 +759,9 @@ Style/RedundantSelf: Exclude: - 'lib/tasks/annotate_models_migrate.rake' -# Offense count: 12 +# Offense count: 13 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes. +# Configuration parameters: EnforcedStyle, AllowInnerSlashes. # SupportedStyles: slashes, percent_r, mixed Style/RegexpLiteral: Exclude: @@ -394,152 +776,83 @@ Style/RescueModifier: - 'lib/annotate/annotate_models.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: AllowAsExpressionSeparator. -Style/Semicolon: - Exclude: - - 'bin/annotate' - - 'spec/integration/rails_2.3_with_bundler/config/initializers/unified_initializer.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: space, no_space -Style/SpaceAroundEqualsInParameterDefault: - Exclude: - - 'lib/annotate/annotate_routes.rb' - - 'spec/integration/common_validation.rb' - -# Offense count: 9 -# Cop supports --auto-correct. -Style/SpaceAroundKeyword: - Exclude: - - 'spec/integration/rails_2.3_with_bundler/Gemfile' - - 'spec/integration/rails_3.2.2/Gemfile' - - 'spec/integration/rails_3.2.8/Gemfile' - - 'spec/integration/rails_3.2_autoloading_factory_girl/Gemfile' - - 'spec/integration/rails_3.2_custom_inflections/Gemfile' - - 'spec/integration/rails_3.2_with_asset_pipeline/Gemfile' - - 'spec/integration/rails_4.1.1/Gemfile' - - 'spec/integration/rails_4.2.0/Gemfile' - - 'spec/integration/standalone/Gemfile' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment. -Style/SpaceAroundOperators: - Exclude: - - 'lib/annotate/annotate_models.rb' - - 'lib/tasks/annotate_routes.rake' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: space, no_space -Style/SpaceBeforeBlockBraces: - Exclude: - - 'lib/annotate/annotate_models.rb' - # Offense count: 1 # Cop supports --auto-correct. -Style/SpaceBeforeComment: +# Configuration parameters: EnforcedStyle. +# SupportedStyles: implicit, explicit +Style/RescueStandardError: Exclude: - - 'lib/annotate/annotate_models.rb' + - 'lib/annotate.rb' -# Offense count: 4 +# Offense count: 2 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. -# SupportedStyles: space, no_space -Style/SpaceInsideBlockBraces: +# Configuration parameters: ConvertCodeThatCanStartToReturnNil, Whitelist. +# Whitelist: present?, blank?, presence, try, try! +Style/SafeNavigation: Exclude: - 'lib/annotate/annotate_models.rb' # Offense count: 2 # Cop supports --auto-correct. -Style/SpaceInsideBrackets: - Exclude: - - 'spec/integration/rails_2.3_with_bundler/config/environment.rb' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles. -# SupportedStyles: space, no_space, compact -Style/SpaceInsideHashLiteralBraces: - Exclude: - - 'lib/tasks/annotate_models.rake' - -# Offense count: 4 -# Cop supports --auto-correct. -Style/SpaceInsideParens: +# Configuration parameters: AllowAsExpressionSeparator. +Style/Semicolon: Exclude: - - 'lib/annotate/annotate_models.rb' + - 'bin/annotate' + - 'spec/integration/rails_2.3_with_bundler/config/initializers/unified_initializer.rb' -# Offense count: 2 +# Offense count: 18 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: space, no_space -Style/SpaceInsideStringInterpolation: +Style/StderrPuts: Exclude: + - 'Rakefile' + - 'lib/annotate.rb' - 'lib/annotate/annotate_models.rb' + - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' -# Offense count: 237 +# Offense count: 240 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. # SupportedStyles: single_quotes, double_quotes Style/StringLiterals: Enabled: false # Offense count: 1 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. +# Configuration parameters: EnforcedStyle. # SupportedStyles: single_quotes, double_quotes Style/StringLiteralsInInterpolation: Exclude: - 'lib/annotate/annotate_models.rb' +# Offense count: 10 +# Cop supports --auto-correct. +# Configuration parameters: MinSize. +# SupportedStyles: percent, brackets +Style/SymbolArray: + EnforcedStyle: brackets + # Offense count: 1 # Cop supports --auto-correct. Style/SymbolLiteral: Exclude: - 'spec/annotate/annotate_models_spec.rb' -# Offense count: 6 -# Cop supports --auto-correct. -Style/Tab: - Exclude: - - 'spec/integration/rails_4.1.1/app/models/sub1/sub2/sub3/event.rb' - - 'spec/integration/rails_4.1.1/app/models/task.rb' - - 'spec/integration/rails_4.2.0/app/models/sub1/sub2/sub3/event.rb' - - 'spec/integration/rails_4.2.0/app/models/task.rb' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: final_newline, final_blank_line -Style/TrailingBlankLines: - Exclude: - - 'spec/integration/rails_4.1.1/app/models/task_observer.rb' - - 'spec/integration/rails_4.1.1/config/initializers/cookies_serializer.rb' - - 'spec/integration/rails_4.2.0/app/models/task_observer.rb' - - 'spec/integration/rails_4.2.0/config/initializers/cookies_serializer.rb' - -# Offense count: 3 +# Offense count: 1 # Cop supports --auto-correct. -# Configuration parameters: EnforcedStyleForMultiline, SupportedStyles. -# SupportedStyles: comma, consistent_comma, no_comma -Style/TrailingCommaInLiteral: +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInArrayLiteral: Exclude: - 'spec/annotate/annotate_models_spec.rb' - - 'spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake' - - 'spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake' # Offense count: 2 # Cop supports --auto-correct. -Style/TrailingWhitespace: +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInHashLiteral: Exclude: - - 'spec/annotate/annotate_routes_spec.rb' - - 'spec/integration/rails_2.3_with_bundler/db/schema.rb' + - 'spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake' + - 'spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake' # Offense count: 2 # Cop supports --auto-correct. @@ -553,3 +866,9 @@ Style/UnneededPercentQ: Exclude: - 'annotate.gemspec' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' + +# Offense count: 424 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Max: 276 diff --git a/.travis.yml b/.travis.yml index dd62bb17b..623154090 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ rvm: - 2.2.7 - 2.3.4 - 2.4.1 +- 2.6.0 - ruby-head matrix: allow_failures: diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 2db610007..516131d91 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,6 @@ +== 2.7.5 +See https://github.com/ctran/annotate_models/releases/tag/v2.7.5 + == 2.7.3 See https://github.com/ctran/annotate_models/releases/tag/v2.7.3 diff --git a/Gemfile b/Gemfile index 8b466be60..8e80a7a1f 100644 --- a/Gemfile +++ b/Gemfile @@ -19,7 +19,7 @@ group :development, :test do gem 'guard-rspec', require: false gem 'rspec', require: false - gem 'rubocop', '~> 0.46.0', require: false unless RUBY_VERSION =~ /^1.8/ + gem 'rubocop', '~> 0.67.2', require: false unless RUBY_VERSION =~ /^1.8/ gem 'simplecov', require: false gem 'terminal-notifier-guard', require: false diff --git a/README.rdoc b/README.rdoc index 5e4582df0..3e3111fb1 100644 --- a/README.rdoc +++ b/README.rdoc @@ -203,6 +203,7 @@ you can do so with a simple environment variable, instead of editing the -f [bare|rdoc|markdown], Render Schema Infomation as plain/RDoc/Markdown --format --force Force new annotations even if there are no changes. + --frozen Do not allow to change annotations. Exits non-zero if there are going to be changes to files. --timestamp Include timestamp in (routes) annotation --trace If unable to annotate a file, print the full stack trace, not just the exception message. -I, --ignore-columns REGEX don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'` diff --git a/annotate.gemspec b/annotate.gemspec index 97e325eb2..2cdeed8c9 100644 --- a/annotate.gemspec +++ b/annotate.gemspec @@ -44,5 +44,5 @@ Gem::Specification.new do |s| s.specification_version = 4 if s.respond_to? :specification_version s.add_runtime_dependency(%q, ['>= 10.4', '< 13.0']) - s.add_runtime_dependency(%q, ['>= 3.2', '< 6.0']) + s.add_runtime_dependency(%q, ['>= 3.2', '< 7.0']) end diff --git a/bin/annotate b/bin/annotate index 0bf4db646..2f27d5d73 100755 --- a/bin/annotate +++ b/bin/annotate @@ -170,6 +170,10 @@ OptionParser.new do |opts| ENV['force'] = 'yes' end + opts.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do + ENV['frozen'] = 'yes' + end + opts.on('--timestamp', 'Include timestamp in (routes) annotation') do ENV['timestamp'] = 'true' end diff --git a/lib/annotate.rb b/lib/annotate.rb index 4a0ebb5c0..27e585773 100644 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -29,8 +29,8 @@ module Annotate FLAG_OPTIONS = [ :show_indexes, :simple_indexes, :include_version, :exclude_tests, :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, - :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace, - :timestamp, :exclude_serializers, :classified_sort, + :format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen, + :trace, :timestamp, :exclude_serializers, :classified_sort, :show_foreign_keys, :show_complete_foreign_keys, :exclude_scaffolds, :exclude_controllers, :exclude_helpers, :exclude_sti_subclasses, :ignore_unknown_models, :with_comment diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index eb78def63..6a46d0f9f 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -62,7 +62,7 @@ module AnnotateModels # Don't show limit (#) on these column types # Example: show "integer" instead of "integer(4)" - NO_LIMIT_COL_TYPES = %w(integer boolean).freeze + NO_LIMIT_COL_TYPES = %w(integer bigint boolean).freeze # Don't show default value for these column types NO_DEFAULT_COL_TYPES = %w(json jsonb hstore).freeze @@ -301,7 +301,7 @@ def get_schema_info(klass, header, options = {}) 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" 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 << format_default(col_name, max_size, col_type, bare_type_allowance, attrs) end end @@ -362,7 +362,7 @@ def get_index_info(klass, options = {}) end def get_col_type(col) - if col.respond_to?(:bigint?) && col.bigint? + if (col.respond_to?(:bigint?) && col.bigint?) || /\Abigint\b/ =~ col.sql_type 'bigint' else (col.type || col.sql_type).to_s @@ -498,53 +498,56 @@ def get_foreign_key_info(klass, options = {}) # :before, :top, :after or :bottom. Default is :before. # def annotate_one_file(file_name, info_block, position, options = {}) - if File.exist?(file_name) - old_content = File.read(file_name) - return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/ + return false unless File.exist?(file_name) + old_content = File.read(file_name) + return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/ - # Ignore the Schema version line because it changes with each migration - header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/ - old_header = old_content.match(header_pattern).to_s - new_header = info_block.match(header_pattern).to_s + # Ignore the Schema version line because it changes with each migration + header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/ + old_header = old_content.match(header_pattern).to_s + new_header = info_block.match(header_pattern).to_s - column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/ - old_columns = old_header && old_header.scan(column_pattern).sort - new_columns = new_header && new_header.scan(column_pattern).sort + column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/ + old_columns = old_header && old_header.scan(column_pattern).sort + new_columns = new_header && new_header.scan(column_pattern).sort - magic_comments_block = magic_comments_as_string(old_content) + return false if old_columns == new_columns && !options[:force] - if old_columns == new_columns && !options[:force] - return false - else - # Replace inline the old schema info with the new schema info - new_content = old_content.sub(annotate_pattern(options), info_block + "\n") + abort "annotate error. #{file_name} needs to be updated, but annotate was run with `--frozen`." if options[:frozen] - if new_content.end_with?(info_block + "\n") - new_content = old_content.sub(annotate_pattern(options), "\n" + info_block) - end + # Replace inline the old schema info with the new schema info + wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : "" + wrapper_close = options[:wrapper_close] ? "# #{options[:wrapper_close]}\n" : "" + wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}" - wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : "" - wrapper_close = options[:wrapper_close] ? "# #{options[:wrapper_close]}\n" : "" - wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}" - # if there *was* no old schema info (no substitution happened) or :force was passed, - # we simply need to insert it in correct position - if new_content == old_content || options[:force] - old_content.gsub!(magic_comment_matcher, '') - old_content.sub!(annotate_pattern(options), '') - - new_content = if %w(after bottom).include?(options[position].to_s) - magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block) - else - magic_comments_block + wrapped_info_block + "\n" + old_content - end - end + old_annotation = old_content.match(annotate_pattern(options)).to_s - File.open(file_name, 'wb') { |f| f.puts new_content } - return true - end + # if there *was* no old schema info or :force was passed, we simply + # need to insert it in correct position + if old_annotation.empty? || options[:force] + magic_comments_block = magic_comments_as_string(old_content) + old_content.gsub!(magic_comment_matcher, '') + old_content.sub!(annotate_pattern(options), '') + + new_content = if %w(after bottom).include?(options[position].to_s) + magic_comments_block + (old_content.rstrip + "\n\n" + wrapped_info_block) + elsif magic_comments_block.empty? + magic_comments_block + wrapped_info_block + "\n" + old_content + else + magic_comments_block + "\n" + wrapped_info_block + "\n" + old_content + end else - false + # replace the old annotation with the new one + + # keep the surrounding whitespace the same + space_match = old_annotation.match(/\A(?\s*).*?\n(?\s*)\z/m) + new_annotation = space_match[:start] + wrapped_info_block + space_match[:end] + + new_content = old_content.sub(annotate_pattern(options), new_annotation) end + + File.open(file_name, 'wb') { |f| f.puts new_content } + true end def magic_comment_matcher @@ -555,7 +558,7 @@ def magic_comments_as_string(content) magic_comments = content.scan(magic_comment_matcher).flatten.compact if magic_comments.any? - magic_comments.join + "\n" + magic_comments.join else '' end @@ -885,7 +888,7 @@ def with_comments?(klass, options) 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) + column.name.size + (column.comment ? width(column.comment) : 0) end.max || 0 max_size += 2 else @@ -895,6 +898,20 @@ def max_schema_info_width(klass, options) max_size end + + def format_default(col_name, max_size, col_type, bare_type_allowance, attrs) + sprintf("# %s:%s %s", mb_chars_ljust(col_name, max_size), mb_chars_ljust(col_type, bare_type_allowance), attrs.join(", ")).rstrip + "\n" + end + + def width(string) + string.chars.inject(0) { |acc, elem| acc + (elem.bytesize == 1 ? 1 : 2) } + end + + def mb_chars_ljust(string, length) + string = string.to_s + padding = length - width(string) + string + (' ' * padding) + end end class BadModelFileError < LoadError diff --git a/lib/annotate/version.rb b/lib/annotate/version.rb index 39df253f0..0e7a17628 100644 --- a/lib/annotate/version.rb +++ b/lib/annotate/version.rb @@ -1,5 +1,5 @@ module Annotate def self.version - '2.7.3' + '2.7.5' end end diff --git a/lib/generators/annotate/templates/auto_annotate_models.rake b/lib/generators/annotate/templates/auto_annotate_models.rake index 72b5844c7..eea2aa859 100644 --- a/lib/generators/annotate/templates/auto_annotate_models.rake +++ b/lib/generators/annotate/templates/auto_annotate_models.rake @@ -42,11 +42,12 @@ if Rails.env.development? 'format_markdown' => 'false', 'sort' => 'false', 'force' => 'false', + 'frozen' => 'false', 'classified_sort' => 'true', 'trace' => 'false', 'wrapper_open' => nil, 'wrapper_close' => nil, - 'with_comment' => true + 'with_comment' => 'true' ) end diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index 495501233..1d4ab21bc 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -39,6 +39,7 @@ task annotate_models: :environment do options[:format_markdown] = Annotate.true?(ENV['format_markdown']) options[:sort] = Annotate.true?(ENV['sort']) options[:force] = Annotate.true?(ENV['force']) + options[:frozen] = Annotate.true?(ENV['frozen']) options[:classified_sort] = Annotate.true?(ENV['classified_sort']) options[:trace] = Annotate.true?(ENV['trace']) options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper']) @@ -47,7 +48,8 @@ task annotate_models: :environment do options[:ignore_routes] = ENV.fetch('ignore_routes', nil) 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[:with_comment] = Annotate.fallback(ENV['with_comment'], '') + options[:with_comment] = Annotate.true?(ENV['with_comment']) + options[:ignore_unknown_models] = Annotate.true?(ENV.fetch('ignore_unknown_models', 'false')) AnnotateModels.do_annotations(options) end diff --git a/lib/tasks/annotate_models_migrate.rake b/lib/tasks/annotate_models_migrate.rake index f5a259b22..c8dbc2f0c 100644 --- a/lib/tasks/annotate_models_migrate.rake +++ b/lib/tasks/annotate_models_migrate.rake @@ -4,21 +4,17 @@ # Append annotations to Rake tasks for ActiveRecord, so annotate automatically gets # run after doing db:migrate. -namespace :db do - [:migrate, :rollback].each do |cmd| - task cmd do - Rake::Task['set_annotation_options'].invoke +%w(db:migrate db:migrate:up db:migrate:down db:migrate:reset db:migrate:redo db:rollback).each do |task| + Rake::Task[task].enhance do + Rake::Task[Rake.application.top_level_tasks.last].enhance do + annotation_options_task = if Rake::Task.task_defined?('app:set_annotation_options') + 'app:set_annotation_options' + else + 'set_annotation_options' + end + Rake::Task[annotation_options_task].invoke Annotate::Migration.update_annotations end - - namespace cmd do - [:change, :up, :down, :reset, :redo].each do |t| - task t do - Rake::Task['set_annotation_options'].invoke - Annotate::Migration.update_annotations - end - end - end end end @@ -46,6 +42,8 @@ module Annotate def self.update_routes if Rake::Task.task_defined?("annotate_routes") Rake::Task["annotate_routes"].invoke + elsif Rake::Task.task_defined?("app:annotate_routes") + Rake::Task["app:annotate_routes"].invoke end end end diff --git a/spec/annotate/annotate_models_spec.rb b/spec/annotate/annotate_models_spec.rb index c1baace01..21dff3966 100644 --- a/spec/annotate/annotate_models_spec.rb +++ b/spec/annotate/annotate_models_spec.rb @@ -5,7 +5,7 @@ require 'active_support/core_ext/string' require 'files' -describe AnnotateModels do +describe AnnotateModels do # rubocop:disable Metrics/BlockLength def mock_index(name, params = {}) double('IndexKeyDefinition', name: name, @@ -52,7 +52,8 @@ def mock_column(name, type, options = {}) default_options = { limit: nil, null: false, - default: nil + default: nil, + sql_type: type } stubs = default_options.dup @@ -184,6 +185,7 @@ def mock_column(name, type, options = {}) mock_column(:id, :integer), mock_column(:integer, :integer, unsigned?: true), mock_column(:bigint, :integer, unsigned?: true, bigint?: true), + mock_column(:bigint, :bigint, unsigned?: true), mock_column(:float, :float, unsigned?: true), mock_column(:decimal, :decimal, unsigned?: true, precision: 10, scale: 2), ]) @@ -196,6 +198,7 @@ def mock_column(name, type, options = {}) # id :integer not null # integer :integer unsigned, not null # bigint :bigint unsigned, not null +# bigint :bigint unsigned, not null # float :float unsigned, not null # decimal :decimal(10, 2) unsigned, not null # @@ -933,6 +936,29 @@ def self.when_called_with(options = {}) # notes(Notes) :text(55) not null # no_comment :text(20) not null # + EOS + + mocked_columns_with_multibyte_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' }], + [:no_comment, :text, { limit: 20, comment: nil }] + ] + + when_called_with with_comment: 'yes', + with_columns: mocked_columns_with_multibyte_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 + # no_comment :text(20) not null + # EOS it 'should get schema info as RDoc' do @@ -1671,7 +1697,7 @@ class User < ActiveRecord::Base end end - it 'adds an empty line between magic comments and model file content (position :after)' do + it 'does not change whitespace between magic comments and model file content (position :after)' do content = "class User < ActiveRecord::Base\nend\n" magic_comments_list_each do |magic_comment| model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}" @@ -1679,7 +1705,7 @@ class User < ActiveRecord::Base annotate_one_file position: :after schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info') - expect(File.read(model_file_name)).to eq("#{magic_comment}\n\n#{content}\n#{schema_info}") + expect(File.read(model_file_name)).to eq("#{magic_comment}\n#{content}\n#{schema_info}") end end @@ -1742,6 +1768,25 @@ class User < ActiveRecord::Base expect(error_output).to include("/user.rb:2:in `'") end end + + describe 'frozen option' do + it "should abort without existing annotation when frozen: true " do + expect { annotate_one_file frozen: true }.to raise_error SystemExit, /user.rb needs to be updated, but annotate was run with `--frozen`./ + end + + it "should abort with different annotation when frozen: true " do + annotate_one_file + another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer)]), '== Schema Info') + @schema_info = another_schema_info + + expect { annotate_one_file frozen: true }.to raise_error SystemExit, /user.rb needs to be updated, but annotate was run with `--frozen`./ + end + + it "should NOT abort with same annotation when frozen: true " do + annotate_one_file + expect { annotate_one_file frozen: true }.not_to raise_error + end + end end describe '.annotate_model_file' do diff --git a/spec/integration/rails_2.3_with_bundler/config/boot.rb b/spec/integration/rails_2.3_with_bundler/config/boot.rb index 190b37add..1be033ee9 100644 --- a/spec/integration/rails_2.3_with_bundler/config/boot.rb +++ b/spec/integration/rails_2.3_with_bundler/config/boot.rb @@ -61,8 +61,8 @@ def load_rails_gem else gem 'rails' end - rescue Gem::LoadError => load_error - if load_error.message =~ /Could not find RubyGem rails/ + rescue Gem::LoadError => e + if e.message =~ /Could not find RubyGem rails/ $stderr.puts "Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed." exit 1 else diff --git a/spec/tasks/annotate_models_migrate_spec.rb b/spec/tasks/annotate_models_migrate_spec.rb new file mode 100644 index 000000000..8c30e1c93 --- /dev/null +++ b/spec/tasks/annotate_models_migrate_spec.rb @@ -0,0 +1,72 @@ +require_relative '../spec_helper' + +describe 'ActiveRecord migration rake task hooks' do + before do + Rake.application = Rake::Application.new + + # Stub migration tasks + %w(db:migrate db:migrate:up db:migrate:down db:migrate:reset db:rollback).each do |task| + Rake::Task.define_task(task) + end + Rake::Task.define_task('db:migrate:redo') do + Rake::Task['db:rollback'].invoke + Rake::Task['db:migrate'].invoke + end + + Rake::Task.define_task('set_annotation_options') + Rake.load_rakefile('tasks/annotate_models_migrate.rake') + + Rake.application.instance_variable_set(:@top_level_tasks, [subject]) + end + + describe 'db:migrate' do + it 'should update annotations' do + expect(Annotate::Migration).to receive(:update_annotations) + Rake.application.top_level + end + end + + describe 'db:migrate:up' do + it 'should update annotations' do + expect(Annotate::Migration).to receive(:update_annotations) + Rake.application.top_level + end + end + + describe 'db:migrate:down' do + it 'should update annotations' do + expect(Annotate::Migration).to receive(:update_annotations) + Rake.application.top_level + end + end + + describe 'db:migrate:reset' do + it 'should update annotations' do + expect(Annotate::Migration).to receive(:update_annotations) + Rake.application.top_level + end + end + + describe 'db:rollback' do + it 'should update annotations' do + expect(Annotate::Migration).to receive(:update_annotations) + Rake.application.top_level + end + end + + describe 'db:migrate:redo' do + it 'should update annotations after all migration tasks' do + allow(Annotate::Migration).to receive(:update_annotations) + + # Confirm that update_annotations isn't called when the original redo task finishes + Rake::Task[subject].enhance do + expect(Annotate::Migration).not_to have_received(:update_annotations) + end + + Rake.application.top_level + + # Hooked 3 times by db:rollback, db:migrate, and db:migrate:redo tasks + expect(Annotate::Migration).to have_received(:update_annotations).exactly(3).times + end + end +end