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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
29 deletions
+61
-29
annotate_models.rb
lib/annotate/annotate_models.rb
+16
-20
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+45
-9
No files found.
lib/annotate/annotate_models.rb
View file @
7f317332
...
...
@@ -232,27 +232,23 @@ module AnnotateModels
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
(
PATTERN
,
info_block
+
"
\n
"
)
# todo: figure out if we need to extract any logic from this merge chunk
# <<<<<<< HEAD
# # Replace the old schema info with the new schema info
# 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 new_content == old_content
# old_content.sub!(encoding, '')
# 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!
(
PATTERN
,
''
)
new_content
=
options
[
position
].
to_s
==
'after'
?
(
encoding_header
+
(
old_content
.
rstrip
+
"
\n\n
"
+
info_block
))
:
(
encoding_header
+
info_block
+
"
\n
"
+
old_content
)
if
new_content
.
end_with?
(
info_block
+
"
\n
"
)
new_content
=
old_content
.
sub
(
PATTERN
,
"
\n
"
+
info_block
)
end
# 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
.
sub!
(
encoding
,
''
)
old_content
.
sub!
(
PATTERN
,
''
)
new_content
=
options
[
position
].
to_s
==
'after'
?
(
encoding_header
+
(
old_content
.
rstrip
+
"
\n\n
"
+
info_block
))
:
(
encoding_header
+
info_block
+
"
\n
"
+
old_content
)
end
File
.
open
(
file_name
,
"wb"
)
{
|
f
|
f
.
puts
new_content
}
return
true
...
...
spec/annotate/annotate_models_spec.rb
View file @
7f317332
...
...
@@ -368,31 +368,67 @@ end
].
each
{
|
encoding_comment
|
yield
encoding_comment
}
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"
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
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
File
.
read
(
@model_file_name
).
should
==
"
#{
@schema_info
}
\n
#{
@file_content
}
"
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
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
@schema_info
}
"
end
it
"should update annotate position"
do
annotate_one_file
:position
=>
:before
describe
"with existing annotation => :before"
do
before
do
annotate_one_file
:position
=>
:before
another_schema_info
=
AnnotateModels
.
get_schema_info
(
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),]),
"== 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
another_schema_info
=
AnnotateModels
.
get_schema_info
(
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),]),
describe
"with existing annotation => :after"
do
before
do
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
@schema_info
=
another_schema_info
annotate_one_file
:position
=>
:after
it
"should retain current position"
do
annotate_one_file
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
@schema_info
}
"
end
File
.
read
(
@model_file_name
).
should
==
"
#{
@file_content
}
\n
#{
another_schema_info
}
"
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
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