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
992ad11a
Commit
992ad11a
authored
Jul 26, 2017
by
Alexander Belozerov
Committed by
Cuong Tran
Jul 25, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Fix #464] Add an empty line between magic comment and schema (#491)
parent
1e2047d2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
5 deletions
+42
-5
annotate_models.rb
lib/annotate/annotate_models.rb
+18
-5
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+24
-0
No files found.
lib/annotate/annotate_models.rb
View file @
992ad11a
...
@@ -503,8 +503,7 @@ module AnnotateModels
...
@@ -503,8 +503,7 @@ 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_comments_block
=
magic_comments_as_string
(
old_content
)
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
...
@@ -522,13 +521,13 @@ module AnnotateModels
...
@@ -522,13 +521,13 @@ module AnnotateModels
# if there *was* no old schema info (no substitution happened) or :force was passed,
# if there *was* no old schema info (no substitution happened) or :force was passed,
# we simply need to insert it in correct position
# we simply need to insert it in correct position
if
new_content
==
old_content
||
options
[
:force
]
if
new_content
==
old_content
||
options
[
:force
]
old_content
.
sub!
(
magic_comment_matcher
,
''
)
old_content
.
g
sub!
(
magic_comment_matcher
,
''
)
old_content
.
sub!
(
annotate_pattern
(
options
),
''
)
old_content
.
sub!
(
annotate_pattern
(
options
),
''
)
new_content
=
if
%w(after bottom)
.
include?
(
options
[
position
].
to_s
)
new_content
=
if
%w(after bottom)
.
include?
(
options
[
position
].
to_s
)
magic_comments
.
join
+
(
old_content
.
rstrip
+
"
\n\n
"
+
wrapped_info_block
)
magic_comments
_block
+
(
old_content
.
rstrip
+
"
\n\n
"
+
wrapped_info_block
)
else
else
magic_comments
.
join
+
wrapped_info_block
+
"
\n
"
+
old_content
magic_comments
_block
+
wrapped_info_block
+
"
\n
"
+
old_content
end
end
end
end
...
@@ -540,6 +539,20 @@ module AnnotateModels
...
@@ -540,6 +539,20 @@ module AnnotateModels
end
end
end
end
def
magic_comment_matcher
Regexp
.
new
(
/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/
)
end
def
magic_comments_as_string
(
content
)
magic_comments
=
content
.
scan
(
magic_comment_matcher
).
flatten
.
compact
if
magic_comments
.
any?
magic_comments
.
join
+
"
\n
"
else
''
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
)
...
...
spec/annotate/annotate_models_spec.rb
View file @
992ad11a
...
@@ -1476,6 +1476,30 @@ end
...
@@ -1476,6 +1476,30 @@ end
end
end
end
end
it
'adds an empty line between magic comments and annotation (position :before)'
do
content
=
"class User < ActiveRecord::Base
\n
end
\n
"
magic_comments_list_each
do
|
magic_comment
|
model_file_name
,
=
write_model
'user.rb'
,
"
#{
magic_comment
}
\n
#{
content
}
"
annotate_one_file
position: :before
schema_info
=
AnnotateModels
.
get_schema_info
(
@klass
,
'== Schema Info'
)
expect
(
File
.
read
(
model_file_name
)).
to
eq
(
"
#{
magic_comment
}
\n\n
#{
schema_info
}
\n
#{
content
}
"
)
end
end
it
'adds an empty line between magic comments and model file content (position :after)'
do
content
=
"class User < ActiveRecord::Base
\n
end
\n
"
magic_comments_list_each
do
|
magic_comment
|
model_file_name
,
=
write_model
'user.rb'
,
"
#{
magic_comment
}
\n
#{
content
}
"
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
}
"
)
end
end
describe
"if a file can't be annotated"
do
describe
"if a file can't be annotated"
do
before
do
before
do
allow
(
AnnotateModels
).
to
receive
(
:get_loaded_model
).
with
(
'user'
).
and_return
(
nil
)
allow
(
AnnotateModels
).
to
receive
(
:get_loaded_model
).
with
(
'user'
).
and_return
(
nil
)
...
...
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