Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
annotate
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
annotate
Commits
983d36f6
Commit
983d36f6
authored
Dec 17, 2016
by
Guillermo Guerrero Ibarra
Committed by
Cuong Tran
Dec 16, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed some offenses 7. (#428)
parent
7a257965
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
209 deletions
+121
-209
.rubocop_todo.yml
.rubocop_todo.yml
+27
-105
annotate_models.rb
lib/annotate/annotate_models.rb
+93
-96
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+1
-8
No files found.
.rubocop_todo.yml
View file @
983d36f6
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-12-1
5 16:54:50
+0100 using RuboCop version 0.46.0.
# on 2016-12-1
6 13:08:29
+0100 using RuboCop version 0.46.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
...
...
@@ -36,6 +36,14 @@ Lint/AssignmentInCondition:
-
'
lib/annotate/annotate_models.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
# Cop supports --auto-correct.
Lint/DeprecatedClassMethods
:
...
...
@@ -70,11 +78,10 @@ Lint/InheritException:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count:
5
# Offense count:
2
Lint/RescueException
:
Exclude
:
-
'
Rakefile'
-
'
lib/annotate/annotate_models.rb'
# Offense count: 1
Lint/ShadowingOuterLocalVariable
:
...
...
@@ -111,13 +118,13 @@ Metrics/BlockNesting:
Metrics/CyclomaticComplexity
:
Max
:
36
# Offense count: 33
4
# Offense count: 33
9
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength
:
Max
:
543
# Offense count: 2
4
# Offense count: 2
3
# Configuration parameters: CountComments.
Metrics/MethodLength
:
Max
:
70
...
...
@@ -131,7 +138,7 @@ Style/AccessorMethodName:
Exclude
:
-
'
lib/annotate.rb'
# Offense count:
1
# Offense count:
3
# Cop supports --auto-correct.
Style/AlignArray
:
Exclude
:
...
...
@@ -145,17 +152,6 @@ Style/BarePercentLiterals:
Exclude
:
-
'
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
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
...
...
@@ -171,21 +167,12 @@ Style/CaseEquality:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count: 8
# Cop supports --auto-correct.
# Configuration parameters: IndentWhenRelativeTo, SupportedStyles, IndentOneStep, IndentationWidth.
# SupportedStyles: case, end
Style/CaseIndentation
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count: 15
# Offense count: 14
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren
:
Exclude
:
-
'
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/test/test_helper.rb'
-
'
spec/integration/rails_3.2.2/test/test_helper.rb'
...
...
@@ -207,20 +194,11 @@ Style/ClassVars:
# Offense count: 1
# 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.
# SupportedStyles: assign_to_condition, assign_inside_condition
Style/ConditionalAssignment
:
Exclude
:
-
'
bin/annotate'
-
'
lib/annotate/annotate_models.rb'
# Offense count: 7
Style/Documentation
:
...
...
@@ -234,14 +212,6 @@ Style/Documentation:
-
'
lib/generators/annotate/install_generator.rb'
-
'
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
# Cop supports --auto-correct.
Style/EmptyLines
:
...
...
@@ -265,7 +235,7 @@ Style/EmptyLinesAroundBlockBody:
-
'
spec/integration/rails_4.1.1/db/schema.rb'
-
'
spec/integration/rails_4.2.0/db/schema.rb'
# Offense count: 2
5
# Offense count: 2
3
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
Style/ExtraSpacing
:
...
...
@@ -307,27 +277,19 @@ Style/GuardClause:
-
'
lib/tasks/migrate.rake'
-
'
spec/integration/rails_2.3_with_bundler/config/boot.rb'
# Offense count: 5
8
# Offense count: 5
7
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Style/HashSyntax
:
Enabled
:
false
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: MaxLineLength.
Style/IfUnlessModifier
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count: 6
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
Style/IndentArray
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
-
'
lib/annotate/annotate_routes.rb'
# Offense count: 6
...
...
@@ -356,14 +318,6 @@ Style/IndentationWidth:
-
'
spec/integration/rails_4.2.0/app/models/task.rb'
# 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
:
Exclude
:
-
'
lib/annotate/active_record_patch.rb'
...
...
@@ -375,15 +329,7 @@ Style/MultilineBlockChain:
-
'
lib/annotate/annotate_models.rb'
-
'
spec/spec_helper.rb'
# Offense count: 3
# 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
# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
# SupportedStyles: aligned, indented
...
...
@@ -391,35 +337,20 @@ Style/MultilineOperationIndentation:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count:
11
# Offense count:
3
# Cop supports --auto-correct.
Style/MutableConstant
:
Exclude
:
-
'
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.
Style/NegatedIf
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/NestedParenthesizedCalls
:
Exclude
:
-
'
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
# Cop supports --auto-correct.
Style/NumericLiterals
:
...
...
@@ -435,12 +366,6 @@ Style/NumericPredicate:
-
'
lib/annotate.rb'
-
'
lib/annotate/annotate_models.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/ParallelAssignment
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters.
...
...
@@ -462,7 +387,7 @@ Style/RaiseArgs:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count:
2
# Offense count:
1
# Cop supports --auto-correct.
Style/RedundantBegin
:
Exclude
:
...
...
@@ -474,19 +399,17 @@ Style/RedundantParentheses:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count:
2
# Offense count:
1
# Cop supports --auto-correct.
# Configuration parameters: AllowMultipleReturnValues.
Style/RedundantReturn
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
-
'
lib/annotate/annotate_routes.rb'
# Offense count:
3
# Offense count:
2
# Cop supports --auto-correct.
Style/RedundantSelf
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
-
'
lib/tasks/migrate.rake'
# Offense count: 12
...
...
@@ -514,13 +437,12 @@ Style/Semicolon:
-
'
bin/annotate'
-
'
spec/integration/rails_2.3_with_bundler/config/initializers/unified_initializer.rb'
# Offense count:
13
# Offense count:
4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: space, no_space
Style/SpaceAroundEqualsInParameterDefault
:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
-
'
lib/annotate/annotate_routes.rb'
-
'
spec/integration/common_validation.rb'
...
...
@@ -538,7 +460,7 @@ Style/SpaceAroundKeyword:
-
'
spec/integration/rails_4.2.0/Gemfile'
-
'
spec/integration/standalone/Gemfile'
# Offense count:
11
# Offense count:
6
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment.
Style/SpaceAroundOperators
:
...
...
@@ -547,7 +469,7 @@ Style/SpaceAroundOperators:
-
'
lib/tasks/annotate_models.rake'
-
'
lib/tasks/annotate_routes.rake'
# Offense count:
6
# Offense count:
4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: space, no_space
...
...
@@ -561,7 +483,7 @@ Style/SpaceBeforeComment:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count:
9
# Offense count:
4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
# SupportedStyles: space, no_space
...
...
@@ -597,7 +519,7 @@ Style/SpaceInsideStringInterpolation:
Exclude
:
-
'
lib/annotate/annotate_models.rb'
# Offense count: 2
5
2
# Offense count: 2
2
2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
...
...
lib/annotate/annotate_models.rb
View file @
983d36f6
...
...
@@ -6,64 +6,64 @@ module AnnotateModels
TRUE_RE
=
/^(true|t|yes|y|1)$/i
# Annotate Models plugin use this header
COMPAT_PREFIX
=
"== Schema Info"
COMPAT_PREFIX_MD
=
"## Schema Info"
PREFIX
=
"== Schema Information"
PREFIX_MD
=
"## Schema Information"
END_MARK
=
"== Schema Information End"
COMPAT_PREFIX
=
'== Schema Info'
.
freeze
COMPAT_PREFIX_MD
=
'## Schema Info'
.
freeze
PREFIX
=
'== Schema Information'
.
freeze
PREFIX_MD
=
'## Schema Information'
.
freeze
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?
# I dont use windows, can`t test
UNIT_TEST_DIR
=
File
.
join
(
"test"
,
"unit"
)
MODEL_TEST_DIR
=
File
.
join
(
"test"
,
"models"
)
# since rails 4.0
SPEC_MODEL_DIR
=
File
.
join
(
"spec"
,
"models"
)
FIXTURE_TEST_DIR
=
File
.
join
(
"test"
,
"fixtures"
)
FIXTURE_SPEC_DIR
=
File
.
join
(
"spec"
,
"fixtures"
)
UNIT_TEST_DIR
=
File
.
join
(
'test'
,
"unit"
)
MODEL_TEST_DIR
=
File
.
join
(
'test'
,
"models"
)
# since rails 4.0
SPEC_MODEL_DIR
=
File
.
join
(
'spec'
,
"models"
)
FIXTURE_TEST_DIR
=
File
.
join
(
'test'
,
"fixtures"
)
FIXTURE_SPEC_DIR
=
File
.
join
(
'spec'
,
"fixtures"
)
# Other test files
CONTROLLER_TEST_DIR
=
File
.
join
(
"test"
,
"controllers"
)
CONTROLLER_SPEC_DIR
=
File
.
join
(
"spec"
,
"controllers"
)
REQUEST_SPEC_DIR
=
File
.
join
(
"spec"
,
"requests"
)
ROUTING_SPEC_DIR
=
File
.
join
(
"spec"
,
"routing"
)
CONTROLLER_TEST_DIR
=
File
.
join
(
'test'
,
"controllers"
)
CONTROLLER_SPEC_DIR
=
File
.
join
(
'spec'
,
"controllers"
)
REQUEST_SPEC_DIR
=
File
.
join
(
'spec'
,
"requests"
)
ROUTING_SPEC_DIR
=
File
.
join
(
'spec'
,
"routing"
)
# Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_TEST_DIR
=
File
.
join
(
"test"
,
"exemplars"
)
EXEMPLARS_SPEC_DIR
=
File
.
join
(
"spec"
,
"exemplars"
)
EXEMPLARS_TEST_DIR
=
File
.
join
(
'test'
,
"exemplars"
)
EXEMPLARS_SPEC_DIR
=
File
.
join
(
'spec'
,
"exemplars"
)
# Machinist http://github.com/notahat/machinist
BLUEPRINTS_TEST_DIR
=
File
.
join
(
"test"
,
"blueprints"
)
BLUEPRINTS_SPEC_DIR
=
File
.
join
(
"spec"
,
"blueprints"
)
BLUEPRINTS_TEST_DIR
=
File
.
join
(
'test'
,
"blueprints"
)
BLUEPRINTS_SPEC_DIR
=
File
.
join
(
'spec'
,
"blueprints"
)
# Factory Girl http://github.com/thoughtbot/factory_girl
FACTORY_GIRL_TEST_DIR
=
File
.
join
(
"test"
,
"factories"
)
FACTORY_GIRL_SPEC_DIR
=
File
.
join
(
"spec"
,
"factories"
)
FACTORY_GIRL_TEST_DIR
=
File
.
join
(
'test'
,
"factories"
)
FACTORY_GIRL_SPEC_DIR
=
File
.
join
(
'spec'
,
"factories"
)
# Fabrication https://github.com/paulelliott/fabrication.git
FABRICATORS_TEST_DIR
=
File
.
join
(
"test"
,
"fabricators"
)
FABRICATORS_SPEC_DIR
=
File
.
join
(
"spec"
,
"fabricators"
)
FABRICATORS_TEST_DIR
=
File
.
join
(
'test'
,
"fabricators"
)
FABRICATORS_SPEC_DIR
=
File
.
join
(
'spec'
,
"fabricators"
)
# Serializers https://github.com/rails-api/active_model_serializers
SERIALIZERS_DIR
=
File
.
join
(
"app"
,
"serializers"
)
SERIALIZERS_TEST_DIR
=
File
.
join
(
"test"
,
"serializers"
)
SERIALIZERS_SPEC_DIR
=
File
.
join
(
"spec"
,
"serializers"
)
SERIALIZERS_DIR
=
File
.
join
(
'app'
,
"serializers"
)
SERIALIZERS_TEST_DIR
=
File
.
join
(
'test'
,
"serializers"
)
SERIALIZERS_SPEC_DIR
=
File
.
join
(
'spec'
,
"serializers"
)
# Controller files
CONTROLLER_DIR
=
File
.
join
(
"app"
,
"controllers"
)
CONTROLLER_DIR
=
File
.
join
(
'app'
,
"controllers"
)
# Active admin registry files
ACTIVEADMIN_DIR
=
File
.
join
(
"app"
,
"admin"
)
ACTIVEADMIN_DIR
=
File
.
join
(
'app'
,
"admin"
)
# Helper files
HELPER_DIR
=
File
.
join
(
"app"
,
"helpers"
)
HELPER_DIR
=
File
.
join
(
'app'
,
"helpers"
)
# Don't show limit (#) on these column types
# 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
NO_DEFAULT_COL_TYPES
=
%w(json jsonb hstore)
NO_DEFAULT_COL_TYPES
=
%w(json jsonb hstore)
.
freeze
class
<<
self
def
annotate_pattern
(
options
=
{})
...
...
@@ -152,14 +152,14 @@ module AnnotateModels
end
end
def
get_patterns
(
pattern_types
=
[])
def
get_patterns
(
pattern_types
=
[])
current_patterns
=
[]
root_dir
.
each
do
|
root_directory
|
Array
(
pattern_types
).
each
do
|
pattern_type
|
current_patterns
+=
files_by_pattern
(
root_directory
,
pattern_type
)
end
end
current_patterns
.
map
{
|
p
|
p
.
sub
(
/^[\/]*/
,
''
)
}
current_patterns
.
map
{
|
p
|
p
.
sub
(
/^[\/]*/
,
''
)
}
end
# Simple quoting for the default column value
...
...
@@ -171,7 +171,7 @@ module AnnotateModels
when
Float
,
Integer
then
value
.
to_s
# BigDecimals need to be output in a non-normalized form and quoted.
when
BigDecimal
then
value
.
to_s
(
'F'
)
when
Array
then
value
.
map
{
|
v
|
quote
(
v
)
}
when
Array
then
value
.
map
{
|
v
|
quote
(
v
)
}
else
value
.
inspect
end
...
...
@@ -208,8 +208,8 @@ module AnnotateModels
bare_type_allowance
=
16
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
<<
"#
#{
'-'
*
(
max_size
+
md_names_overhead
)
}
|
#{
'-'
*
md_type_allowance
}
|
#{
'-'
*
27
}
\n
"
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
"
end
cols
=
if
ignore_columns
=
options
[
:ignore_columns
]
...
...
@@ -294,13 +294,13 @@ module AnnotateModels
info
<<
"#
\n
"
info
<<
"# ### Columns
\n
"
else
info
<<
"# Table name:
#{
klass
.
table_name
}
\n
"
info
<<
"# Table name:
#{
klass
.
table_name
}
\n
"
end
info
<<
"#
\n
"
end
def
get_schema_footer_text
(
_klass
,
options
=
{})
info
=
""
info
=
''
if
options
[
:format_rdoc
]
info
<<
"#--
\n
"
info
<<
"#
#{
END_MARK
}
\n
"
...
...
@@ -310,11 +310,11 @@ module AnnotateModels
end
end
def
get_index_info
(
klass
,
options
=
{})
if
options
[
:format_markdown
]
index_info
=
"#
\n
# ### Indexes
\n
#
\n
"
def
get_index_info
(
klass
,
options
=
{})
i
ndex_info
=
i
f
options
[
:format_markdown
]
"#
\n
# ### Indexes
\n
#
\n
"
else
index_info
=
"#
\n
# Indexes
\n
#
\n
"
"#
\n
# Indexes
\n
#
\n
"
end
indexes
=
retrieve_indexes_from_table
(
klass
)
...
...
@@ -322,10 +322,10 @@ module AnnotateModels
max_size
=
indexes
.
collect
{
|
index
|
index
.
name
.
size
}.
max
+
1
indexes
.
sort_by
(
&
:name
).
each
do
|
index
|
if
options
[
:format_markdown
]
index_info
<<
sprintf
(
"# * `%s`%s:
\n
# * **`%s`**
\n
"
,
index
.
name
,
index
.
unique
?
" (_unique_)"
:
""
,
Array
(
index
.
columns
).
join
(
"`**
\n
# * **`"
))
i
ndex_info
<<
i
f
options
[
:format_markdown
]
sprintf
(
"# * `%s`%s:
\n
# * **`%s`**
\n
"
,
index
.
name
,
index
.
unique
?
" (_unique_)"
:
""
,
Array
(
index
.
columns
).
join
(
"`**
\n
# * **`"
))
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
...
...
@@ -354,11 +354,11 @@ module AnnotateModels
excludes
.
include?
(
col_type
)
end
def
get_foreign_key_info
(
klass
,
options
=
{})
if
options
[
:format_markdown
]
fk_info
=
"#
\n
# ### Foreign Keys
\n
#
\n
"
def
get_foreign_key_info
(
klass
,
options
=
{})
fk_info
=
if
options
[
:format_markdown
]
"#
\n
# ### Foreign Keys
\n
#
\n
"
else
fk_info
=
"#
\n
# Foreign Keys
\n
#
\n
"
"#
\n
# Foreign Keys
\n
#
\n
"
end
return
''
unless
klass
.
connection
.
respond_to?
(
:supports_foreign_keys?
)
&&
...
...
@@ -376,10 +376,11 @@ module AnnotateModels
constraints_info
+=
"ON DELETE =>
#{
fk
.
on_delete
}
"
if
fk
.
on_delete
constraints_info
+=
"ON UPDATE =>
#{
fk
.
on_update
}
"
if
fk
.
on_update
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
]
sprintf
(
"# * `%s`%s:
\n
# * **`%s`**
\n
"
,
format_name
.
call
(
fk
),
constraints_info
.
blank?
?
''
:
" (_
#{
constraints_info
}
_)"
,
ref_info
)
else
fk_info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s %s %s"
,
format_name
.
call
(
fk
),
"(
#{
ref_info
}
)"
,
constraints_info
).
rstrip
+
"
\n
"
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s %s %s"
,
format_name
.
call
(
fk
),
"(
#{
ref_info
}
)"
,
constraints_info
).
rstrip
+
"
\n
"
end
end
...
...
@@ -387,9 +388,9 @@ module AnnotateModels
end
# 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
#
matches the block that is already there. If so, leave it be. If not, remove the old
# info block and write a new one.
# a schema info block (a comment starting with "== Schema Information"),
#
check if it matches the block that is already there. If so, leave it be.
#
If not, remove the old
info block and write a new one.
#
# == Returns:
# true or false depending on whether the file was modified.
...
...
@@ -399,7 +400,7 @@ module AnnotateModels
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
# :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
)
old_content
=
File
.
read
(
file_name
)
return
false
if
old_content
=~
/# -\*- SkipSchemaAnnotations.*\n/
...
...
@@ -413,8 +414,8 @@ module AnnotateModels
old_columns
=
old_header
&&
old_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_comments
=
old_content
.
scan
(
magic_comment_matcher
).
flatten
.
compact
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
if
old_columns
==
new_columns
&&
!
options
[
:force
]
return
false
...
...
@@ -435,10 +436,10 @@ module AnnotateModels
old_content
.
sub!
(
magic_comment_matcher
,
''
)
old_content
.
sub!
(
annotate_pattern
(
options
),
''
)
if
%w(after bottom)
.
include?
(
options
[
position
].
to_s
)
new_content
=
magic_comments
.
join
+
(
old_content
.
rstrip
+
"
\n\n
"
+
wrapped_info_block
)
new_content
=
if
%w(after bottom)
.
include?
(
options
[
position
].
to_s
)
magic_comments
.
join
+
(
old_content
.
rstrip
+
"
\n\n
"
+
wrapped_info_block
)
else
new_content
=
magic_comments
.
join
+
wrapped_info_block
+
"
\n
"
+
old_content
magic_comments
.
join
+
wrapped_info_block
+
"
\n
"
+
old_content
end
end
...
...
@@ -450,10 +451,10 @@ module AnnotateModels
end
end
def
remove_annotation_of_file
(
file_name
,
options
=
{})
def
remove_annotation_of_file
(
file_name
,
options
=
{})
if
File
.
exist?
(
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
)
}
/
,
''
)
File
.
open
(
file_name
,
'wb'
)
{
|
f
|
f
.
puts
content
}
...
...
@@ -494,7 +495,7 @@ module AnnotateModels
# == Returns:
# an array of file names that were annotated.
#
def
annotate
(
klass
,
file
,
header
,
options
=
{})
def
annotate
(
klass
,
file
,
header
,
options
=
{})
begin
klass
.
reset_column_information
info
=
get_schema_info
(
klass
,
header
,
options
)
...
...
@@ -517,17 +518,16 @@ module AnnotateModels
position_key
=
'position_in_class'
.
to_sym
end
unless
options
[
exclusion_key
]
self
.
get_patterns
(
key
).
map
{
|
f
|
resolve_filename
(
f
,
model_name
,
table_name
)
}.
each
{
|
f
|
next
if
options
[
exclusion_key
]
get_patterns
(
key
)
.
map
{
|
f
|
resolve_filename
(
f
,
model_name
,
table_name
)
}
.
each
do
|
f
|
if
annotate_one_file
(
f
,
info
,
position_key
,
options_with_position
(
options
,
position_key
))
annotated
<<
f
end
}
end
end
rescue
Exception
=>
e
rescue
StandardError
=>
e
puts
"Unable to annotate
#{
file
}
:
#{
e
.
message
}
"
puts
"
\t
"
+
e
.
backtrace
.
join
(
"
\n\t
"
)
if
options
[
:trace
]
end
...
...
@@ -546,8 +546,8 @@ module AnnotateModels
# in the model_dir directory.
def
get_model_files
(
options
)
models
=
[]
if
!
options
[
:is_rake
]
models
=
ARGV
.
dup
.
reject
{
|
m
|
m
.
match
(
/^(.*)=/
)
}
unless
options
[
:is_rake
]
models
=
ARGV
.
dup
.
reject
{
|
m
|
m
.
match
(
/^(.*)=/
)
}
end
if
models
.
empty?
...
...
@@ -598,21 +598,18 @@ module AnnotateModels
# Retrieve loaded model class by path to the file where it's supposed to be defined.
def
get_loaded_model
(
model_path
)
begin
ActiveSupport
::
Inflector
.
constantize
(
ActiveSupport
::
Inflector
.
camelize
(
model_path
))
rescue
# Revert to the old way but it is not really robust
ObjectSpace
.
each_object
(
::
Class
).
select
do
|
c
|
ObjectSpace
.
each_object
(
::
Class
)
.
select
do
|
c
|
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
.
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
=
{})
def
parse_options
(
options
=
{})
self
.
model_dir
=
options
[
:model_dir
]
if
options
[
:model_dir
]
self
.
root_dir
=
options
[
:root_dir
]
if
options
[
:root_dir
]
end
...
...
@@ -621,7 +618,7 @@ module AnnotateModels
# ActiveRecord models. If we can find the class, and
# if its a subclass of ActiveRecord::Base,
# then pass it to the associated block
def
do_annotations
(
options
=
{})
def
do_annotations
(
options
=
{})
parse_options
(
options
)
header
=
options
[
:format_markdown
]
?
PREFIX_MD
.
dup
:
PREFIX
.
dup
...
...
@@ -651,21 +648,20 @@ module AnnotateModels
(
!
options
[
:exclude_sti_subclasses
]
||
!
(
klass
.
superclass
<
ActiveRecord
::
Base
&&
klass
.
table_name
==
klass
.
superclass
.
table_name
))
&&
!
klass
.
abstract_class?
&&
klass
.
table_exists?
if
do_annotate
annotated
.
concat
(
annotate
(
klass
,
file
,
header
,
options
))
end
annotated
.
concat
(
annotate
(
klass
,
file
,
header
,
options
))
if
do_annotate
rescue
BadModelFileError
=>
e
unless
options
[
:ignore_unknown_models
]
puts
"Unable to annotate
#{
file
}
:
#{
e
.
message
}
"
puts
"
\t
"
+
e
.
backtrace
.
join
(
"
\n\t
"
)
if
options
[
:trace
]
end
rescue
Exception
=>
e
rescue
StandardError
=>
e
puts
"Unable to annotate
#{
file
}
:
#{
e
.
message
}
"
puts
"
\t
"
+
e
.
backtrace
.
join
(
"
\n\t
"
)
if
options
[
:trace
]
end
end
def
remove_annotations
(
options
=
{})
def
remove_annotations
(
options
=
{})
parse_options
(
options
)
deannotated
=
[]
...
...
@@ -680,9 +676,9 @@ module AnnotateModels
model_file_name
=
file
deannotated_klass
=
true
if
remove_annotation_of_file
(
model_file_name
,
options
)
get_patterns
(
matched_types
(
options
))
.
map
{
|
f
|
resolve_filename
(
f
,
model_name
,
table_name
)
}.
each
do
|
f
|
get_patterns
(
matched_types
(
options
))
.
map
{
|
f
|
resolve_filename
(
f
,
model_name
,
table_name
)
}
.
each
do
|
f
|
if
File
.
exist?
(
f
)
remove_annotation_of_file
(
f
,
options
)
deannotated_klass
=
true
...
...
@@ -690,7 +686,7 @@ module AnnotateModels
end
end
deannotated
<<
klass
if
deannotated_klass
rescue
Exception
=>
e
rescue
StandardError
=>
e
puts
"Unable to deannotate
#{
File
.
join
(
file
)
}
:
#{
e
.
message
}
"
puts
"
\t
"
+
e
.
backtrace
.
join
(
"
\n\t
"
)
if
options
[
:trace
]
end
...
...
@@ -699,10 +695,10 @@ module AnnotateModels
end
def
resolve_filename
(
filename_template
,
model_name
,
table_name
)
filename_template
.
gsub
(
'%MODEL_NAME%'
,
model_name
).
gsub
(
'%PLURALIZED_MODEL_NAME%'
,
model_name
.
pluralize
).
gsub
(
'%TABLE_NAME%'
,
table_name
||
model_name
.
pluralize
)
filename_template
.
gsub
(
'%MODEL_NAME%'
,
model_name
)
.
gsub
(
'%PLURALIZED_MODEL_NAME%'
,
model_name
.
pluralize
)
.
gsub
(
'%TABLE_NAME%'
,
table_name
||
model_name
.
pluralize
)
end
def
classified_sort
(
cols
)
...
...
@@ -722,14 +718,15 @@ module AnnotateModels
rest_cols
<<
c
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
# Ignore warnings for the duration of the block ()
def
silence_warnings
old_verbose
,
$VERBOSE
=
$VERBOSE
,
nil
old_verbose
=
$VERBOSE
$VERBOSE
=
nil
yield
ensure
$VERBOSE
=
old_verbose
...
...
spec/annotate/annotate_models_spec.rb
View file @
983d36f6
...
...
@@ -567,14 +567,7 @@ EOS
describe
'#get_model_class'
do
require
'tmpdir'
module
::
ActiveRecord
class
Base
def
self
.
has_many
_name
end
end
end
# todo: use 'files' gem instead
# TODO: use 'files' gem instead
def
create
(
file
,
body
=
'hi'
)
file_path
=
File
.
join
(
AnnotateModels
.
model_dir
[
0
],
file
)
FileUtils
.
mkdir_p
(
File
.
dirname
(
file_path
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment