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
7f317332
Commit
7f317332
authored
Feb 25, 2014
by
Cuong Tran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Retain existing position unless :force is passed
parent
95401212
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
21 deletions
+53
-21
annotate_models.rb
lib/annotate/annotate_models.rb
+10
-14
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+43
-7
No files found.
lib/annotate/annotate_models.rb
View file @
7f317332
...
@@ -232,27 +232,23 @@ module AnnotateModels
...
@@ -232,27 +232,23 @@ module AnnotateModels
if
old_columns
==
new_columns
&&
!
options
[
:force
]
if
old_columns
==
new_columns
&&
!
options
[
:force
]
return
false
return
false
else
else
# Replace inline the old schema info with the new schema info
new_content
=
old_content
.
sub
(
PATTERN
,
info_block
+
"
\n
"
)
# todo: figure out if we need to extract any logic from this merge chunk
if
new_content
.
end_with?
(
info_block
+
"
\n
"
)
# <<<<<<< HEAD
new_content
=
old_content
.
sub
(
PATTERN
,
"
\n
"
+
info_block
)
# # Replace the old schema info with the new schema info
end
# new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n*/, info_block)
# # But, if there *was* no old schema info, we simply need to insert it
# if there *was* no old schema info (no substitution happened) or :force was passed,
# if new_content == old_content
# we simply need to insert it in correct position
# old_content.sub!(encoding, '')
if
new_content
==
old_content
||
options
[
:force
]
# new_content = options[:position] == 'after' ?
# (encoding_header + (old_content =~ /\n$/ ? old_content : old_content + "\n") + info_block) :
# (encoding_header + info_block + old_content)
# end
# =======
# Strip the old schema info, and insert new schema info.
old_content
.
sub!
(
encoding
,
''
)
old_content
.
sub!
(
encoding
,
''
)
old_content
.
sub!
(
PATTERN
,
''
)
old_content
.
sub!
(
PATTERN
,
''
)
new_content
=
options
[
position
].
to_s
==
'after'
?
new_content
=
options
[
position
].
to_s
==
'after'
?
(
encoding_header
+
(
old_content
.
rstrip
+
"
\n\n
"
+
info_block
))
:
(
encoding_header
+
(
old_content
.
rstrip
+
"
\n\n
"
+
info_block
))
:
(
encoding_header
+
info_block
+
"
\n
"
+
old_content
)
(
encoding_header
+
info_block
+
"
\n
"
+
old_content
)
end
File
.
open
(
file_name
,
"wb"
)
{
|
f
|
f
.
puts
new_content
}
File
.
open
(
file_name
,
"wb"
)
{
|
f
|
f
.
puts
new_content
}
return
true
return
true
...
...
spec/annotate/annotate_models_spec.rb
View file @
7f317332
...
@@ -368,31 +368,67 @@ end
...
@@ -368,31 +368,67 @@ end
].
each
{
|
encoding_comment
|
yield
encoding_comment
}
].
each
{
|
encoding_comment
|
yield
encoding_comment
}
end
end
it
"should
annotate the file before the model if
position == 'before'"
do
it
"should
put annotation before class if :
position == 'before'"
do
annotate_one_file
:position
=>
"before"
annotate_one_file
:position
=>
"before"
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
end
end
it
"should
annotate before if given
:position => :before"
do
it
"should
put annotation before class if
:position => :before"
do
annotate_one_file
:position
=>
:before
annotate_one_file
:position
=>
:before
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
end
end
it
"should
annotate after if given
:position => :after"
do
it
"should
put annotation after class if
:position => :after"
do
annotate_one_file
:position
=>
:after
annotate_one_file
:position
=>
:after
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
@schema_info
}
"
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
@schema_info
}
"
end
end
it
"should update annotate position"
do
describe
"with existing annotation => :before"
do
before
do
annotate_one_file
:position
=>
:before
annotate_one_file
:position
=>
:before
another_schema_info
=
AnnotateModels
.
get_schema_info
(
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),]),
another_schema_info
=
AnnotateModels
.
get_schema_info
(
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),]),
"== Schema Info"
)
"== Schema Info"
)
@schema_info
=
another_schema_info
@schema_info
=
another_schema_info
end
it
"should retain current position"
do
annotate_one_file
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
end
it
"should retain current position even when :position is changed to :after"
do
annotate_one_file
:position
=>
:after
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
end
it
"should change position to :after when :force => true"
do
annotate_one_file
:position
=>
:after
,
:force
=>
true
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
@schema_info
}
"
end
end
describe
"with existing annotation => :after"
do
before
do
annotate_one_file
:position
=>
:after
annotate_one_file
:position
=>
:after
another_schema_info
=
AnnotateModels
.
get_schema_info
(
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),]),
"== Schema Info"
)
@schema_info
=
another_schema_info
end
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
another_schema_info
}
"
it
"should retain current position"
do
annotate_one_file
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
@schema_info
}
"
end
it
"should retain current position even when :position is changed to :before"
do
annotate_one_file
:position
=>
:before
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
@schema_info
}
"
end
it
"should change position to :before when :force => true"
do
annotate_one_file
:position
=>
:before
,
:force
=>
true
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
end
end
end
it
'should skip columns with option[:ignore_columns] set'
do
it
'should skip columns with option[:ignore_columns] set'
do
...
...
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