Commit 805acf73 by Cuong Tran

Merge pull request #181 from razum2um/develop

prevent multiple requires
parents c5f30d3a e89cc7f8
...@@ -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.
......
...@@ -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"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment