Commit 8056ecf0 by Cuong Tran

Fix the conversion from file path to class name which should fix #125 and #141

parent 2b34d608
......@@ -377,13 +377,18 @@ module AnnotateModels
# Retrieve loaded model class by path to the file where it's supposed to be defined.
def get_loaded_model(model_path)
ObjectSpace.each_object(::Class).
select do |c|
Class === c and # note: we use === to avoid a bug in activesupport 2.3.14 OptionMerger vs. is_a?
c.ancestors.respond_to?(:include?) and # to fix FactoryGirl bug, see https://github.com/ctran/annotate_models/pull/82
c.ancestors.include?(ActiveRecord::Base)
end.
detect { |c| ActiveSupport::Inflector.underscore(c) == model_path }
begin
ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize(model_path))
rescue
# Revert to the old way but it is not really robust
ObjectSpace.each_object(::Class).
select do |c|
Class === c and # note: we use === to avoid a bug in activesupport 2.3.14 OptionMerger vs. is_a?
c.ancestors.respond_to?(:include?) and # to fix FactoryGirl bug, see https://github.com/ctran/annotate_models/pull/82
c.ancestors.include?(ActiveRecord::Base)
end.
detect { |c| ActiveSupport::Inflector.underscore(c) == model_path }
end
end
# We're passed a name of things that might be
......
......@@ -182,6 +182,38 @@ EOS
check_class_name 'bar/foo_inside_bar.rb', 'Bar::FooInsideBar'
end
it "should find AR model when duplicated by a nested model" do
create 'foo.rb', <<-EOS
class Foo < ActiveRecord::Base
end
EOS
create 'bar/foo.rb', <<-EOS
class Bar::Foo
end
EOS
check_class_name 'bar/foo.rb', 'Bar::Foo'
check_class_name 'foo.rb', 'Foo'
end
it "should find AR model nested inside a class" do
create 'voucher.rb', <<-EOS
class Voucher < ActiveRecord::Base
end
EOS
create 'voucher/foo.rb', <<-EOS
class Voucher
class Foo
end
end
EOS
check_class_name 'voucher.rb', 'Voucher'
check_class_name 'voucher/foo.rb', 'Voucher::Foo'
end
it "should not care about unknown macros" do
create 'foo_with_macro.rb', <<-EOS
class FooWithMacro < ActiveRecord::Base
......
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