Commit 983d36f6 by Guillermo Guerrero Ibarra Committed by Cuong Tran

Fixed some offenses 7. (#428)

parent 7a257965
# This configuration was generated by # This configuration was generated by
# `rubocop --auto-gen-config` # `rubocop --auto-gen-config`
# on 2016-12-15 16:54:50 +0100 using RuboCop version 0.46.0. # on 2016-12-16 13:08:29 +0100 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
...@@ -36,6 +36,14 @@ Lint/AssignmentInCondition: ...@@ -36,6 +36,14 @@ Lint/AssignmentInCondition:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AlignWith, SupportedStyles.
# SupportedStyles: either, start_of_block, start_of_line
Lint/BlockAlignment:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 6 # Offense count: 6
# Cop supports --auto-correct. # Cop supports --auto-correct.
Lint/DeprecatedClassMethods: Lint/DeprecatedClassMethods:
...@@ -70,11 +78,10 @@ Lint/InheritException: ...@@ -70,11 +78,10 @@ Lint/InheritException:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 5 # Offense count: 2
Lint/RescueException: Lint/RescueException:
Exclude: Exclude:
- 'Rakefile' - 'Rakefile'
- 'lib/annotate/annotate_models.rb'
# Offense count: 1 # Offense count: 1
Lint/ShadowingOuterLocalVariable: Lint/ShadowingOuterLocalVariable:
...@@ -111,13 +118,13 @@ Metrics/BlockNesting: ...@@ -111,13 +118,13 @@ Metrics/BlockNesting:
Metrics/CyclomaticComplexity: Metrics/CyclomaticComplexity:
Max: 36 Max: 36
# Offense count: 334 # Offense count: 339
# 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: 543
# Offense count: 24 # Offense count: 23
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/MethodLength: Metrics/MethodLength:
Max: 70 Max: 70
...@@ -131,7 +138,7 @@ Style/AccessorMethodName: ...@@ -131,7 +138,7 @@ Style/AccessorMethodName:
Exclude: Exclude:
- 'lib/annotate.rb' - 'lib/annotate.rb'
# Offense count: 1 # Offense count: 3
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/AlignArray: Style/AlignArray:
Exclude: Exclude:
...@@ -145,17 +152,6 @@ Style/BarePercentLiterals: ...@@ -145,17 +152,6 @@ Style/BarePercentLiterals:
Exclude: Exclude:
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods.
# SupportedStyles: line_count_based, semantic, braces_for_chaining
# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object
# FunctionalMethods: let, let!, subject, watch
# IgnoredMethods: lambda, proc, it
Style/BlockDelimiters:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 3 # Offense count: 3
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
...@@ -171,21 +167,12 @@ Style/CaseEquality: ...@@ -171,21 +167,12 @@ Style/CaseEquality:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 8 # Offense count: 14
# Cop supports --auto-correct.
# Configuration parameters: IndentWhenRelativeTo, SupportedStyles, IndentOneStep, IndentationWidth.
# SupportedStyles: case, end
Style/CaseIndentation:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 15
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: nested, compact # SupportedStyles: nested, compact
Style/ClassAndModuleChildren: Style/ClassAndModuleChildren:
Exclude: Exclude:
- 'lib/annotate/active_record_patch.rb' - 'lib/annotate/active_record_patch.rb'
- 'spec/annotate/annotate_models_spec.rb'
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
- 'spec/integration/rails_2.3_with_bundler/test/test_helper.rb' - 'spec/integration/rails_2.3_with_bundler/test/test_helper.rb'
- 'spec/integration/rails_3.2.2/test/test_helper.rb' - 'spec/integration/rails_3.2.2/test/test_helper.rb'
...@@ -207,20 +194,11 @@ Style/ClassVars: ...@@ -207,20 +194,11 @@ Style/ClassVars:
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: Keywords.
# Keywords: TODO, FIXME, OPTIMIZE, HACK, REVIEW
Style/CommentAnnotation:
Exclude:
- 'spec/annotate/annotate_models_spec.rb'
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, SingleLineConditionsOnly. # Configuration parameters: EnforcedStyle, SupportedStyles, SingleLineConditionsOnly.
# SupportedStyles: assign_to_condition, assign_inside_condition # SupportedStyles: assign_to_condition, assign_inside_condition
Style/ConditionalAssignment: Style/ConditionalAssignment:
Exclude: Exclude:
- 'bin/annotate' - 'bin/annotate'
- 'lib/annotate/annotate_models.rb'
# Offense count: 7 # Offense count: 7
Style/Documentation: Style/Documentation:
...@@ -234,14 +212,6 @@ Style/Documentation: ...@@ -234,14 +212,6 @@ Style/Documentation:
- 'lib/generators/annotate/install_generator.rb' - 'lib/generators/annotate/install_generator.rb'
- 'lib/tasks/migrate.rake' - 'lib/tasks/migrate.rake'
# Offense count: 9
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: leading, trailing
Style/DotPosition:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 2 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/EmptyLines: Style/EmptyLines:
...@@ -265,7 +235,7 @@ Style/EmptyLinesAroundBlockBody: ...@@ -265,7 +235,7 @@ Style/EmptyLinesAroundBlockBody:
- 'spec/integration/rails_4.1.1/db/schema.rb' - 'spec/integration/rails_4.1.1/db/schema.rb'
- 'spec/integration/rails_4.2.0/db/schema.rb' - 'spec/integration/rails_4.2.0/db/schema.rb'
# Offense count: 25 # Offense count: 23
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment. # Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
Style/ExtraSpacing: Style/ExtraSpacing:
...@@ -307,27 +277,19 @@ Style/GuardClause: ...@@ -307,27 +277,19 @@ Style/GuardClause:
- 'lib/tasks/migrate.rake' - 'lib/tasks/migrate.rake'
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
# Offense count: 58 # Offense count: 57
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. # Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Style/HashSyntax: Style/HashSyntax:
Enabled: false Enabled: false
# Offense count: 2 # Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: MaxLineLength.
Style/IfUnlessModifier:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 6
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth. # Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_brackets # SupportedStyles: special_inside_parentheses, consistent, align_brackets
Style/IndentArray: Style/IndentArray:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
# Offense count: 6 # Offense count: 6
...@@ -356,14 +318,6 @@ Style/IndentationWidth: ...@@ -356,14 +318,6 @@ Style/IndentationWidth:
- 'spec/integration/rails_4.2.0/app/models/task.rb' - 'spec/integration/rails_4.2.0/app/models/task.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline
Style/MethodDefParentheses:
Exclude:
- 'spec/annotate/annotate_models_spec.rb'
# Offense count: 1
Style/MethodMissing: Style/MethodMissing:
Exclude: Exclude:
- 'lib/annotate/active_record_patch.rb' - 'lib/annotate/active_record_patch.rb'
...@@ -375,15 +329,7 @@ Style/MultilineBlockChain: ...@@ -375,15 +329,7 @@ Style/MultilineBlockChain:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
- 'spec/spec_helper.rb' - 'spec/spec_helper.rb'
# Offense count: 3 # Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
# SupportedStyles: aligned, indented, indented_relative_to_receiver
Style/MultilineMethodCallIndentation:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 7
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth. # Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
# SupportedStyles: aligned, indented # SupportedStyles: aligned, indented
...@@ -391,35 +337,20 @@ Style/MultilineOperationIndentation: ...@@ -391,35 +337,20 @@ Style/MultilineOperationIndentation:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 11 # Offense count: 3
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/MutableConstant: Style/MutableConstant:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
- 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb'
- 'spec/integration/rails_2.3_with_bundler/config/environment.rb' - 'spec/integration/rails_2.3_with_bundler/config/environment.rb'
# Offense count: 1 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/NegatedIf:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/NestedParenthesizedCalls: Style/NestedParenthesizedCalls:
Exclude: Exclude:
- 'bin/annotate' - 'bin/annotate'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles.
# SupportedStyles: skip_modifier_ifs, always
Style/Next:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 9 # Offense count: 9
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/NumericLiterals: Style/NumericLiterals:
...@@ -435,12 +366,6 @@ Style/NumericPredicate: ...@@ -435,12 +366,6 @@ Style/NumericPredicate:
- 'lib/annotate.rb' - 'lib/annotate.rb'
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/ParallelAssignment:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 6 # Offense count: 6
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters. # Configuration parameters: PreferredDelimiters.
...@@ -462,7 +387,7 @@ Style/RaiseArgs: ...@@ -462,7 +387,7 @@ Style/RaiseArgs:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 2 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/RedundantBegin: Style/RedundantBegin:
Exclude: Exclude:
...@@ -474,19 +399,17 @@ Style/RedundantParentheses: ...@@ -474,19 +399,17 @@ Style/RedundantParentheses:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 2 # Offense count: 1
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AllowMultipleReturnValues. # Configuration parameters: AllowMultipleReturnValues.
Style/RedundantReturn: Style/RedundantReturn:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
# Offense count: 3 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/RedundantSelf: Style/RedundantSelf:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb'
- 'lib/tasks/migrate.rake' - 'lib/tasks/migrate.rake'
# Offense count: 12 # Offense count: 12
...@@ -514,13 +437,12 @@ Style/Semicolon: ...@@ -514,13 +437,12 @@ Style/Semicolon:
- 'bin/annotate' - 'bin/annotate'
- 'spec/integration/rails_2.3_with_bundler/config/initializers/unified_initializer.rb' - 'spec/integration/rails_2.3_with_bundler/config/initializers/unified_initializer.rb'
# Offense count: 13 # Offense count: 4
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: space, no_space # SupportedStyles: space, no_space
Style/SpaceAroundEqualsInParameterDefault: Style/SpaceAroundEqualsInParameterDefault:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
- 'spec/integration/common_validation.rb' - 'spec/integration/common_validation.rb'
...@@ -538,7 +460,7 @@ Style/SpaceAroundKeyword: ...@@ -538,7 +460,7 @@ 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: 11 # Offense count: 6
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment. # Configuration parameters: AllowForAlignment.
Style/SpaceAroundOperators: Style/SpaceAroundOperators:
...@@ -547,7 +469,7 @@ Style/SpaceAroundOperators: ...@@ -547,7 +469,7 @@ Style/SpaceAroundOperators:
- 'lib/tasks/annotate_models.rake' - 'lib/tasks/annotate_models.rake'
- 'lib/tasks/annotate_routes.rake' - 'lib/tasks/annotate_routes.rake'
# Offense count: 6 # Offense count: 4
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: space, no_space # SupportedStyles: space, no_space
...@@ -561,7 +483,7 @@ Style/SpaceBeforeComment: ...@@ -561,7 +483,7 @@ Style/SpaceBeforeComment:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 9 # Offense count: 4
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. # Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
# SupportedStyles: space, no_space # SupportedStyles: space, no_space
...@@ -597,7 +519,7 @@ Style/SpaceInsideStringInterpolation: ...@@ -597,7 +519,7 @@ Style/SpaceInsideStringInterpolation:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 252 # Offense count: 222
# 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
......
...@@ -6,64 +6,64 @@ module AnnotateModels ...@@ -6,64 +6,64 @@ module AnnotateModels
TRUE_RE = /^(true|t|yes|y|1)$/i TRUE_RE = /^(true|t|yes|y|1)$/i
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info" COMPAT_PREFIX = '== Schema Info'.freeze
COMPAT_PREFIX_MD = "## Schema Info" COMPAT_PREFIX_MD = '## Schema Info'.freeze
PREFIX = "== Schema Information" PREFIX = '== Schema Information'.freeze
PREFIX_MD = "## Schema Information" PREFIX_MD = '## Schema Information'.freeze
END_MARK = "== Schema Information End" END_MARK = '== Schema Information End'.freeze
MATCHED_TYPES = %w(test fixture factory serializer scaffold controller helper) MATCHED_TYPES = %w(test fixture factory serializer scaffold controller helper).freeze
# File.join for windows reverse bar compat? # File.join for windows reverse bar compat?
# I dont use windows, can`t test # I dont use windows, can`t test
UNIT_TEST_DIR = File.join("test", "unit") UNIT_TEST_DIR = File.join('test', "unit")
MODEL_TEST_DIR = File.join("test", "models") # since rails 4.0 MODEL_TEST_DIR = File.join('test', "models") # since rails 4.0
SPEC_MODEL_DIR = File.join("spec", "models") SPEC_MODEL_DIR = File.join('spec', "models")
FIXTURE_TEST_DIR = File.join("test", "fixtures") FIXTURE_TEST_DIR = File.join('test', "fixtures")
FIXTURE_SPEC_DIR = File.join("spec", "fixtures") FIXTURE_SPEC_DIR = File.join('spec', "fixtures")
# Other test files # Other test files
CONTROLLER_TEST_DIR = File.join("test", "controllers") CONTROLLER_TEST_DIR = File.join('test', "controllers")
CONTROLLER_SPEC_DIR = File.join("spec", "controllers") CONTROLLER_SPEC_DIR = File.join('spec', "controllers")
REQUEST_SPEC_DIR = File.join("spec", "requests") REQUEST_SPEC_DIR = File.join('spec', "requests")
ROUTING_SPEC_DIR = File.join("spec", "routing") ROUTING_SPEC_DIR = File.join('spec', "routing")
# Object Daddy http://github.com/flogic/object_daddy/tree/master # Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_TEST_DIR = File.join("test", "exemplars") EXEMPLARS_TEST_DIR = File.join('test', "exemplars")
EXEMPLARS_SPEC_DIR = File.join("spec", "exemplars") EXEMPLARS_SPEC_DIR = File.join('spec', "exemplars")
# Machinist http://github.com/notahat/machinist # Machinist http://github.com/notahat/machinist
BLUEPRINTS_TEST_DIR = File.join("test", "blueprints") BLUEPRINTS_TEST_DIR = File.join('test', "blueprints")
BLUEPRINTS_SPEC_DIR = File.join("spec", "blueprints") BLUEPRINTS_SPEC_DIR = File.join('spec', "blueprints")
# Factory Girl http://github.com/thoughtbot/factory_girl # Factory Girl http://github.com/thoughtbot/factory_girl
FACTORY_GIRL_TEST_DIR = File.join("test", "factories") FACTORY_GIRL_TEST_DIR = File.join('test', "factories")
FACTORY_GIRL_SPEC_DIR = File.join("spec", "factories") FACTORY_GIRL_SPEC_DIR = File.join('spec', "factories")
# Fabrication https://github.com/paulelliott/fabrication.git # Fabrication https://github.com/paulelliott/fabrication.git
FABRICATORS_TEST_DIR = File.join("test", "fabricators") FABRICATORS_TEST_DIR = File.join('test', "fabricators")
FABRICATORS_SPEC_DIR = File.join("spec", "fabricators") FABRICATORS_SPEC_DIR = File.join('spec', "fabricators")
# Serializers https://github.com/rails-api/active_model_serializers # Serializers https://github.com/rails-api/active_model_serializers
SERIALIZERS_DIR = File.join("app", "serializers") SERIALIZERS_DIR = File.join('app', "serializers")
SERIALIZERS_TEST_DIR = File.join("test", "serializers") SERIALIZERS_TEST_DIR = File.join('test', "serializers")
SERIALIZERS_SPEC_DIR = File.join("spec", "serializers") SERIALIZERS_SPEC_DIR = File.join('spec', "serializers")
# Controller files # Controller files
CONTROLLER_DIR = File.join("app", "controllers") CONTROLLER_DIR = File.join('app', "controllers")
# Active admin registry files # Active admin registry files
ACTIVEADMIN_DIR = File.join("app", "admin") ACTIVEADMIN_DIR = File.join('app', "admin")
# Helper files # Helper files
HELPER_DIR = File.join("app", "helpers") HELPER_DIR = File.join('app', "helpers")
# Don't show limit (#) on these column types # Don't show limit (#) on these column types
# Example: show "integer" instead of "integer(4)" # Example: show "integer" instead of "integer(4)"
NO_LIMIT_COL_TYPES = %w(integer boolean) NO_LIMIT_COL_TYPES = %w(integer boolean).freeze
# Don't show default value for these column types # Don't show default value for these column types
NO_DEFAULT_COL_TYPES = %w(json jsonb hstore) NO_DEFAULT_COL_TYPES = %w(json jsonb hstore).freeze
class << self class << self
def annotate_pattern(options = {}) def annotate_pattern(options = {})
...@@ -87,79 +87,79 @@ module AnnotateModels ...@@ -87,79 +87,79 @@ module AnnotateModels
def test_files(root_directory) def test_files(root_directory)
[ [
File.join(root_directory, UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"), File.join(root_directory, UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
File.join(root_directory, MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"), File.join(root_directory, MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"),
File.join(root_directory, SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb") File.join(root_directory, SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb")
] ]
end end
def fixture_files(root_directory) def fixture_files(root_directory)
[ [
File.join(root_directory, FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"), File.join(root_directory, FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"),
File.join(root_directory, FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"), File.join(root_directory, FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"),
File.join(root_directory, FIXTURE_TEST_DIR, "%PLURALIZED_MODEL_NAME%.yml"), File.join(root_directory, FIXTURE_TEST_DIR, "%PLURALIZED_MODEL_NAME%.yml"),
File.join(root_directory, FIXTURE_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.yml") File.join(root_directory, FIXTURE_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.yml")
] ]
end end
def scaffold_files(root_directory) def scaffold_files(root_directory)
[ [
File.join(root_directory, CONTROLLER_TEST_DIR, "%PLURALIZED_MODEL_NAME%_controller_test.rb"), File.join(root_directory, CONTROLLER_TEST_DIR, "%PLURALIZED_MODEL_NAME%_controller_test.rb"),
File.join(root_directory, CONTROLLER_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_controller_spec.rb"), File.join(root_directory, CONTROLLER_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_controller_spec.rb"),
File.join(root_directory, REQUEST_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_spec.rb"), File.join(root_directory, REQUEST_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_spec.rb"),
File.join(root_directory, ROUTING_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_routing_spec.rb") File.join(root_directory, ROUTING_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_routing_spec.rb")
] ]
end end
def factory_files(root_directory) def factory_files(root_directory)
[ [
File.join(root_directory, EXEMPLARS_TEST_DIR, "%MODEL_NAME%_exemplar.rb"), File.join(root_directory, EXEMPLARS_TEST_DIR, "%MODEL_NAME%_exemplar.rb"),
File.join(root_directory, EXEMPLARS_SPEC_DIR, "%MODEL_NAME%_exemplar.rb"), File.join(root_directory, EXEMPLARS_SPEC_DIR, "%MODEL_NAME%_exemplar.rb"),
File.join(root_directory, BLUEPRINTS_TEST_DIR, "%MODEL_NAME%_blueprint.rb"), File.join(root_directory, BLUEPRINTS_TEST_DIR, "%MODEL_NAME%_blueprint.rb"),
File.join(root_directory, BLUEPRINTS_SPEC_DIR, "%MODEL_NAME%_blueprint.rb"), File.join(root_directory, BLUEPRINTS_SPEC_DIR, "%MODEL_NAME%_blueprint.rb"),
File.join(root_directory, FACTORY_GIRL_TEST_DIR, "%MODEL_NAME%_factory.rb"), # (old style) File.join(root_directory, FACTORY_GIRL_TEST_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
File.join(root_directory, FACTORY_GIRL_SPEC_DIR, "%MODEL_NAME%_factory.rb"), # (old style) File.join(root_directory, FACTORY_GIRL_SPEC_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
File.join(root_directory, FACTORY_GIRL_TEST_DIR, "%TABLE_NAME%.rb"), # (new style) File.join(root_directory, FACTORY_GIRL_TEST_DIR, "%TABLE_NAME%.rb"), # (new style)
File.join(root_directory, FACTORY_GIRL_SPEC_DIR, "%TABLE_NAME%.rb"), # (new style) File.join(root_directory, FACTORY_GIRL_SPEC_DIR, "%TABLE_NAME%.rb"), # (new style)
File.join(root_directory, FABRICATORS_TEST_DIR, "%MODEL_NAME%_fabricator.rb"), File.join(root_directory, FABRICATORS_TEST_DIR, "%MODEL_NAME%_fabricator.rb"),
File.join(root_directory, FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb") File.join(root_directory, FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb")
] ]
end end
def serialize_files(root_directory) def serialize_files(root_directory)
[ [
File.join(root_directory, SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"), File.join(root_directory, SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"),
File.join(root_directory, SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"), File.join(root_directory, SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"),
File.join(root_directory, SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb") File.join(root_directory, SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb")
] ]
end end
def files_by_pattern(root_directory, pattern_type) def files_by_pattern(root_directory, pattern_type)
case pattern_type case pattern_type
when 'test' then test_files(root_directory) when 'test' then test_files(root_directory)
when 'fixture' then fixture_files(root_directory) when 'fixture' then fixture_files(root_directory)
when 'scaffold' then scaffold_files(root_directory) when 'scaffold' then scaffold_files(root_directory)
when 'factory' then factory_files(root_directory) when 'factory' then factory_files(root_directory)
when 'serializer' then serialize_files(root_directory) when 'serializer' then serialize_files(root_directory)
when 'controller' when 'controller'
[File.join(root_directory, CONTROLLER_DIR, "%PLURALIZED_MODEL_NAME%_controller.rb")] [File.join(root_directory, CONTROLLER_DIR, "%PLURALIZED_MODEL_NAME%_controller.rb")]
when 'admin' when 'admin'
[File.join(root_directory, ACTIVEADMIN_DIR, "%MODEL_NAME%.rb")] [File.join(root_directory, ACTIVEADMIN_DIR, "%MODEL_NAME%.rb")]
when 'helper' when 'helper'
[File.join(root_directory, HELPER_DIR, "%PLURALIZED_MODEL_NAME%_helper.rb")] [File.join(root_directory, HELPER_DIR, "%PLURALIZED_MODEL_NAME%_helper.rb")]
else else
[] []
end end
end end
def get_patterns(pattern_types=[]) def get_patterns(pattern_types = [])
current_patterns = [] current_patterns = []
root_dir.each do |root_directory| root_dir.each do |root_directory|
Array(pattern_types).each do |pattern_type| Array(pattern_types).each do |pattern_type|
current_patterns += files_by_pattern(root_directory, pattern_type) current_patterns += files_by_pattern(root_directory, pattern_type)
end end
end end
current_patterns.map{ |p| p.sub(/^[\/]*/, '') } current_patterns.map { |p| p.sub(/^[\/]*/, '') }
end end
# Simple quoting for the default column value # Simple quoting for the default column value
...@@ -171,7 +171,7 @@ module AnnotateModels ...@@ -171,7 +171,7 @@ module AnnotateModels
when Float, Integer then value.to_s when Float, Integer then value.to_s
# BigDecimals need to be output in a non-normalized form and quoted. # BigDecimals need to be output in a non-normalized form and quoted.
when BigDecimal then value.to_s('F') when BigDecimal then value.to_s('F')
when Array then value.map {|v| quote(v)} when Array then value.map { |v| quote(v) }
else else
value.inspect value.inspect
end end
...@@ -208,8 +208,8 @@ module AnnotateModels ...@@ -208,8 +208,8 @@ module AnnotateModels
bare_type_allowance = 16 bare_type_allowance = 16
if options[:format_markdown] if options[:format_markdown]
info<< sprintf( "# %-#{max_size + md_names_overhead}.#{max_size + md_names_overhead}s | %-#{md_type_allowance}.#{md_type_allowance}s | %s\n", 'Name', 'Type', 'Attributes' ) info << sprintf( "# %-#{max_size + md_names_overhead}.#{max_size + md_names_overhead}s | %-#{md_type_allowance}.#{md_type_allowance}s | %s\n", 'Name', 'Type', 'Attributes' )
info<< "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n" info << "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
end end
cols = if ignore_columns = options[:ignore_columns] cols = if ignore_columns = options[:ignore_columns]
...@@ -294,13 +294,13 @@ module AnnotateModels ...@@ -294,13 +294,13 @@ module AnnotateModels
info << "#\n" info << "#\n"
info << "# ### Columns\n" info << "# ### Columns\n"
else else
info<< "# Table name: #{klass.table_name}\n" info << "# Table name: #{klass.table_name}\n"
end end
info << "#\n" info << "#\n"
end end
def get_schema_footer_text(_klass, options = {}) def get_schema_footer_text(_klass, options = {})
info = "" info = ''
if options[:format_rdoc] if options[:format_rdoc]
info << "#--\n" info << "#--\n"
info << "# #{END_MARK}\n" info << "# #{END_MARK}\n"
...@@ -310,23 +310,23 @@ module AnnotateModels ...@@ -310,23 +310,23 @@ module AnnotateModels
end end
end end
def get_index_info(klass, options={}) def get_index_info(klass, options = {})
if options[:format_markdown] index_info = if options[:format_markdown]
index_info = "#\n# ### Indexes\n#\n" "#\n# ### Indexes\n#\n"
else else
index_info = "#\n# Indexes\n#\n" "#\n# Indexes\n#\n"
end end
indexes = retrieve_indexes_from_table(klass) indexes = retrieve_indexes_from_table(klass)
return '' if indexes.empty? return '' if indexes.empty?
max_size = indexes.collect{|index| index.name.size}.max + 1 max_size = indexes.collect{|index| index.name.size}.max + 1
indexes.sort_by(&:name).each do |index| indexes.sort_by(&:name).each do |index|
if options[:format_markdown] index_info << if options[:format_markdown]
index_info << sprintf("# * `%s`%s:\n# * **`%s`**\n", index.name, index.unique ? " (_unique_)" : "", Array(index.columns).join("`**\n# * **`")) sprintf("# * `%s`%s:\n# * **`%s`**\n", index.name, index.unique ? " (_unique_)" : "", Array(index.columns).join("`**\n# * **`"))
else else
index_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{Array(index.columns).join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n" sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{Array(index.columns).join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n"
end end
end end
index_info index_info
...@@ -354,12 +354,12 @@ module AnnotateModels ...@@ -354,12 +354,12 @@ module AnnotateModels
excludes.include?(col_type) excludes.include?(col_type)
end end
def get_foreign_key_info(klass, options={}) def get_foreign_key_info(klass, options = {})
if options[:format_markdown] fk_info = if options[:format_markdown]
fk_info = "#\n# ### Foreign Keys\n#\n" "#\n# ### Foreign Keys\n#\n"
else else
fk_info = "#\n# Foreign Keys\n#\n" "#\n# Foreign Keys\n#\n"
end end
return '' unless klass.connection.respond_to?(:supports_foreign_keys?) && return '' unless klass.connection.respond_to?(:supports_foreign_keys?) &&
klass.connection.supports_foreign_keys? && klass.connection.respond_to?(:foreign_keys) klass.connection.supports_foreign_keys? && klass.connection.respond_to?(:foreign_keys)
...@@ -376,20 +376,21 @@ module AnnotateModels ...@@ -376,20 +376,21 @@ module AnnotateModels
constraints_info += "ON DELETE => #{fk.on_delete} " if fk.on_delete constraints_info += "ON DELETE => #{fk.on_delete} " if fk.on_delete
constraints_info += "ON UPDATE => #{fk.on_update} " if fk.on_update constraints_info += "ON UPDATE => #{fk.on_update} " if fk.on_update
constraints_info.strip! constraints_info.strip!
if options[:format_markdown]
fk_info << sprintf("# * `%s`%s:\n# * **`%s`**\n", format_name.call(fk), constraints_info.blank? ? '' : " (_#{constraints_info}_)", ref_info) fk_info << if options[:format_markdown]
else sprintf("# * `%s`%s:\n# * **`%s`**\n", format_name.call(fk), constraints_info.blank? ? '' : " (_#{constraints_info}_)", ref_info)
fk_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", format_name.call(fk), "(#{ref_info})", constraints_info).rstrip + "\n" else
end sprintf("# %-#{max_size}.#{max_size}s %s %s", format_name.call(fk), "(#{ref_info})", constraints_info).rstrip + "\n"
end
end end
fk_info fk_info
end end
# Add a schema block to a file. If the file already contains # Add a schema block to a file. If the file already contains
# a schema info block (a comment starting with "== Schema Information"), check if it # a schema info block (a comment starting with "== Schema Information"),
# matches the block that is already there. If so, leave it be. If not, remove the old # check if it matches the block that is already there. If so, leave it be.
# info block and write a new one. # If not, remove the old info block and write a new one.
# #
# == Returns: # == Returns:
# true or false depending on whether the file was modified. # true or false depending on whether the file was modified.
...@@ -399,7 +400,7 @@ module AnnotateModels ...@@ -399,7 +400,7 @@ module AnnotateModels
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file, # :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
# :before, :top, :after or :bottom. Default is :before. # :before, :top, :after or :bottom. Default is :before.
# #
def annotate_one_file(file_name, info_block, position, options={}) def annotate_one_file(file_name, info_block, position, options = {})
if File.exist?(file_name) if File.exist?(file_name)
old_content = File.read(file_name) old_content = File.read(file_name)
return false if old_content =~ /# -\*- SkipSchemaAnnotations.*\n/ return false if old_content =~ /# -\*- SkipSchemaAnnotations.*\n/
...@@ -413,8 +414,8 @@ module AnnotateModels ...@@ -413,8 +414,8 @@ module AnnotateModels
old_columns = old_header && old_header.scan(column_pattern).sort old_columns = old_header && old_header.scan(column_pattern).sort
new_columns = new_header && new_header.scan(column_pattern).sort new_columns = new_header && new_header.scan(column_pattern).sort
magic_comment_matcher= Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)|(^#\s*frozen_string_literal:.+\n)|(^# -\*- frozen_string_literal\s*:.+-\*-\n)/) magic_comment_matcher = Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)|(^#\s*frozen_string_literal:.+\n)|(^# -\*- frozen_string_literal\s*:.+-\*-\n)/)
magic_comments= old_content.scan(magic_comment_matcher).flatten.compact magic_comments = old_content.scan(magic_comment_matcher).flatten.compact
if old_columns == new_columns && !options[:force] if old_columns == new_columns && !options[:force]
return false return false
...@@ -435,11 +436,11 @@ module AnnotateModels ...@@ -435,11 +436,11 @@ module AnnotateModels
old_content.sub!(magic_comment_matcher, '') old_content.sub!(magic_comment_matcher, '')
old_content.sub!(annotate_pattern(options), '') old_content.sub!(annotate_pattern(options), '')
if %w(after bottom).include?(options[position].to_s) new_content = if %w(after bottom).include?(options[position].to_s)
new_content = magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block) magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)
else else
new_content = magic_comments.join + wrapped_info_block + "\n" + old_content magic_comments.join + wrapped_info_block + "\n" + old_content
end end
end end
File.open(file_name, 'wb') { |f| f.puts new_content } File.open(file_name, 'wb') { |f| f.puts new_content }
...@@ -450,10 +451,10 @@ module AnnotateModels ...@@ -450,10 +451,10 @@ module AnnotateModels
end end
end end
def remove_annotation_of_file(file_name, options={}) def remove_annotation_of_file(file_name, options = {})
if File.exist?(file_name) if File.exist?(file_name)
content = File.read(file_name) content = File.read(file_name)
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : "" wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ''
content.sub!(/(#{wrapper_open})?#{annotate_pattern(options)}/, '') content.sub!(/(#{wrapper_open})?#{annotate_pattern(options)}/, '')
File.open(file_name, 'wb') { |f| f.puts content } File.open(file_name, 'wb') { |f| f.puts content }
...@@ -494,7 +495,7 @@ module AnnotateModels ...@@ -494,7 +495,7 @@ module AnnotateModels
# == Returns: # == Returns:
# an array of file names that were annotated. # an array of file names that were annotated.
# #
def annotate(klass, file, header, options={}) def annotate(klass, file, header, options = {})
begin begin
klass.reset_column_information klass.reset_column_information
info = get_schema_info(klass, header, options) info = get_schema_info(klass, header, options)
...@@ -517,17 +518,16 @@ module AnnotateModels ...@@ -517,17 +518,16 @@ module AnnotateModels
position_key = 'position_in_class'.to_sym position_key = 'position_in_class'.to_sym
end end
unless options[exclusion_key] next if options[exclusion_key]
self.get_patterns(key). get_patterns(key)
map { |f| resolve_filename(f, model_name, table_name) }. .map { |f| resolve_filename(f, model_name, table_name) }
each { |f| .each do |f|
if annotate_one_file(f, info, position_key, options_with_position(options, position_key)) if annotate_one_file(f, info, position_key, options_with_position(options, position_key))
annotated << f annotated << f
end end
} end
end
end end
rescue Exception => e rescue StandardError => e
puts "Unable to annotate #{file}: #{e.message}" puts "Unable to annotate #{file}: #{e.message}"
puts "\t" + e.backtrace.join("\n\t") if options[:trace] puts "\t" + e.backtrace.join("\n\t") if options[:trace]
end end
...@@ -546,8 +546,8 @@ module AnnotateModels ...@@ -546,8 +546,8 @@ module AnnotateModels
# in the model_dir directory. # in the model_dir directory.
def get_model_files(options) def get_model_files(options)
models = [] models = []
if !options[:is_rake] unless options[:is_rake]
models = ARGV.dup.reject{|m| m.match(/^(.*)=/)} models = ARGV.dup.reject { |m| m.match(/^(.*)=/) }
end end
if models.empty? if models.empty?
...@@ -598,21 +598,18 @@ module AnnotateModels ...@@ -598,21 +598,18 @@ module AnnotateModels
# Retrieve loaded model class by path to the file where it's supposed to be defined. # Retrieve loaded model class by path to the file where it's supposed to be defined.
def get_loaded_model(model_path) def get_loaded_model(model_path)
begin ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize(model_path))
ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize(model_path)) rescue
rescue # Revert to the old way but it is not really robust
# Revert to the old way but it is not really robust ObjectSpace.each_object(::Class)
ObjectSpace.each_object(::Class). .select do |c|
select do |c| Class === c && # note: we use === to avoid a bug in activesupport 2.3.14 OptionMerger vs. is_a?
Class === c && # note: we use === to avoid a bug in activesupport 2.3.14 OptionMerger vs. is_a? c.ancestors.respond_to?(:include?) && # to fix FactoryGirl bug, see https://github.com/ctran/annotate_models/pull/82
c.ancestors.respond_to?(:include?) && # to fix FactoryGirl bug, see https://github.com/ctran/annotate_models/pull/82 c.ancestors.include?(ActiveRecord::Base)
c.ancestors.include?(ActiveRecord::Base) end.detect { |c| ActiveSupport::Inflector.underscore(c.to_s) == model_path }
end. end
detect { |c| ActiveSupport::Inflector.underscore(c.to_s) == model_path }
end def parse_options(options = {})
end
def parse_options(options={})
self.model_dir = options[:model_dir] if options[:model_dir] self.model_dir = options[:model_dir] if options[:model_dir]
self.root_dir = options[:root_dir] if options[:root_dir] self.root_dir = options[:root_dir] if options[:root_dir]
end end
...@@ -621,7 +618,7 @@ module AnnotateModels ...@@ -621,7 +618,7 @@ module AnnotateModels
# ActiveRecord models. If we can find the class, and # ActiveRecord models. If we can find the class, and
# if its a subclass of ActiveRecord::Base, # if its a subclass of ActiveRecord::Base,
# then pass it to the associated block # then pass it to the associated block
def do_annotations(options={}) def do_annotations(options = {})
parse_options(options) parse_options(options)
header = options[:format_markdown] ? PREFIX_MD.dup : PREFIX.dup header = options[:format_markdown] ? PREFIX_MD.dup : PREFIX.dup
...@@ -651,21 +648,20 @@ module AnnotateModels ...@@ -651,21 +648,20 @@ module AnnotateModels
(!options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name)) && (!options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name)) &&
!klass.abstract_class? && !klass.abstract_class? &&
klass.table_exists? klass.table_exists?
if do_annotate
annotated.concat(annotate(klass, file, header, options)) annotated.concat(annotate(klass, file, header, options)) if do_annotate
end
rescue BadModelFileError => e rescue BadModelFileError => e
unless options[:ignore_unknown_models] unless options[:ignore_unknown_models]
puts "Unable to annotate #{file}: #{e.message}" puts "Unable to annotate #{file}: #{e.message}"
puts "\t" + e.backtrace.join("\n\t") if options[:trace] puts "\t" + e.backtrace.join("\n\t") if options[:trace]
end end
rescue Exception => e rescue StandardError => e
puts "Unable to annotate #{file}: #{e.message}" puts "Unable to annotate #{file}: #{e.message}"
puts "\t" + e.backtrace.join("\n\t") if options[:trace] puts "\t" + e.backtrace.join("\n\t") if options[:trace]
end end
end end
def remove_annotations(options={}) def remove_annotations(options = {})
parse_options(options) parse_options(options)
deannotated = [] deannotated = []
...@@ -680,9 +676,9 @@ module AnnotateModels ...@@ -680,9 +676,9 @@ module AnnotateModels
model_file_name = file model_file_name = file
deannotated_klass = true if remove_annotation_of_file(model_file_name, options) deannotated_klass = true if remove_annotation_of_file(model_file_name, options)
get_patterns(matched_types(options)). get_patterns(matched_types(options))
map { |f| resolve_filename(f, model_name, table_name) }. .map { |f| resolve_filename(f, model_name, table_name) }
each do |f| .each do |f|
if File.exist?(f) if File.exist?(f)
remove_annotation_of_file(f, options) remove_annotation_of_file(f, options)
deannotated_klass = true deannotated_klass = true
...@@ -690,7 +686,7 @@ module AnnotateModels ...@@ -690,7 +686,7 @@ module AnnotateModels
end end
end end
deannotated << klass if deannotated_klass deannotated << klass if deannotated_klass
rescue Exception => e rescue StandardError => e
puts "Unable to deannotate #{File.join(file)}: #{e.message}" puts "Unable to deannotate #{File.join(file)}: #{e.message}"
puts "\t" + e.backtrace.join("\n\t") if options[:trace] puts "\t" + e.backtrace.join("\n\t") if options[:trace]
end end
...@@ -699,10 +695,10 @@ module AnnotateModels ...@@ -699,10 +695,10 @@ module AnnotateModels
end end
def resolve_filename(filename_template, model_name, table_name) def resolve_filename(filename_template, model_name, table_name)
filename_template. filename_template
gsub('%MODEL_NAME%', model_name). .gsub('%MODEL_NAME%', model_name)
gsub('%PLURALIZED_MODEL_NAME%', model_name.pluralize). .gsub('%PLURALIZED_MODEL_NAME%', model_name.pluralize)
gsub('%TABLE_NAME%', table_name || model_name.pluralize) .gsub('%TABLE_NAME%', table_name || model_name.pluralize)
end end
def classified_sort(cols) def classified_sort(cols)
...@@ -722,14 +718,15 @@ module AnnotateModels ...@@ -722,14 +718,15 @@ module AnnotateModels
rest_cols << c rest_cols << c
end end
end end
[rest_cols, timestamps, associations].each {|a| a.sort_by!(&:name) } [rest_cols, timestamps, associations].each { |a| a.sort_by!(&:name) }
return ([id] << rest_cols << timestamps << associations).flatten.compact ([id] << rest_cols << timestamps << associations).flatten.compact
end end
# Ignore warnings for the duration of the block () # Ignore warnings for the duration of the block ()
def silence_warnings def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil old_verbose = $VERBOSE
$VERBOSE = nil
yield yield
ensure ensure
$VERBOSE = old_verbose $VERBOSE = old_verbose
......
...@@ -567,14 +567,7 @@ EOS ...@@ -567,14 +567,7 @@ EOS
describe '#get_model_class' do describe '#get_model_class' do
require 'tmpdir' require 'tmpdir'
module ::ActiveRecord # TODO: use 'files' gem instead
class Base
def self.has_many _name
end
end
end
# todo: use 'files' gem instead
def create(file, body = 'hi') def create(file, body = 'hi')
file_path = File.join(AnnotateModels.model_dir[0], file) file_path = File.join(AnnotateModels.model_dir[0], file)
FileUtils.mkdir_p(File.dirname(file_path)) FileUtils.mkdir_p(File.dirname(file_path))
......
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