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
805acf73
Commit
805acf73
authored
Apr 21, 2014
by
Cuong Tran
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #181 from razum2um/develop
prevent multiple requires
parents
c5f30d3a
e89cc7f8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
3 deletions
+32
-3
annotate_models.rb
lib/annotate/annotate_models.rb
+13
-3
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+19
-0
No files found.
lib/annotate/annotate_models.rb
View file @
805acf73
...
@@ -369,10 +369,20 @@ module AnnotateModels
...
@@ -369,10 +369,20 @@ module AnnotateModels
# Check for namespaced models in subdirectories as well as models
# Check for namespaced models in subdirectories as well as models
# in subdirectories without namespacing.
# in subdirectories without namespacing.
def
get_model_class
(
file
)
def
get_model_class
(
file
)
# this is for non-rails projects, which don't get Rails auto-require magic
require
File
.
expand_path
(
"
#{
model_dir
}
/
#{
file
}
"
)
model_path
=
file
.
gsub
(
/\.rb$/
,
''
)
model_path
=
file
.
gsub
(
/\.rb$/
,
''
)
get_loaded_model
(
model_path
)
||
get_loaded_model
(
model_path
.
split
(
'/'
).
last
)
begin
get_loaded_model
(
model_path
)
or
raise
LoadError
.
new
(
"cannot load a model from
#{
file
}
"
)
rescue
LoadError
# this is for non-rails projects, which don't get Rails auto-require magic
if
Kernel
.
require
(
File
.
expand_path
(
"
#{
model_dir
}
/
#{
model_path
}
"
))
retry
elsif
model_path
.
match
(
/\//
)
model_path
=
model_path
.
split
(
'/'
)[
1
..-
1
].
join
(
'/'
).
to_s
retry
else
raise
end
end
end
end
# 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.
...
...
spec/annotate/annotate_models_spec.rb
View file @
805acf73
...
@@ -287,6 +287,21 @@ EOS
...
@@ -287,6 +287,21 @@ EOS
check_class_name
'foo_with_known_macro.rb'
,
'FooWithKnownMacro'
check_class_name
'foo_with_known_macro.rb'
,
'FooWithKnownMacro'
end
.
should
==
""
end
.
should
==
""
end
end
it
"should not require model files twice"
do
create
'loaded_class.rb'
,
<<-
EOS
class LoadedClass < ActiveRecord::Base
CONSTANT = 1
end
EOS
path
=
File
.
expand_path
(
"
#{
AnnotateModels
.
model_dir
}
/loaded_class"
)
Kernel
.
load
"
#{
path
}
.rb"
expect
(
Kernel
).
not_to
receive
(
:require
).
with
(
path
)
capturing
(
:stderr
)
{
check_class_name
'loaded_class.rb'
,
'LoadedClass'
}.
should_not
include
(
"warning: already initialized constant LoadedClass::CONSTANT"
)
end
end
end
describe
"#remove_annotation_of_file"
do
describe
"#remove_annotation_of_file"
do
...
@@ -500,6 +515,8 @@ end
...
@@ -500,6 +515,8 @@ end
describe
"if a file can't be annotated"
do
describe
"if a file can't be annotated"
do
before
do
before
do
AnnotateModels
.
stub
(
:get_loaded_model
).
with
(
'user'
).
and_return
(
nil
)
write_model
(
'user.rb'
,
<<-
EOS
)
write_model
(
'user.rb'
,
<<-
EOS
)
class User < ActiveRecord::Base
class User < ActiveRecord::Base
raise "oops"
raise "oops"
...
@@ -528,6 +545,8 @@ end
...
@@ -528,6 +545,8 @@ end
describe
"if a file can't be deannotated"
do
describe
"if a file can't be deannotated"
do
before
do
before
do
AnnotateModels
.
stub
(
:get_loaded_model
).
with
(
'user'
).
and_return
(
nil
)
write_model
(
'user.rb'
,
<<-
EOS
)
write_model
(
'user.rb'
,
<<-
EOS
)
class User < ActiveRecord::Base
class User < ActiveRecord::Base
raise "oops"
raise "oops"
...
...
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