File tree Expand file tree Collapse file tree 2 files changed +38
-3
lines changed Expand file tree Collapse file tree 2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -602,7 +602,9 @@ def get_loaded_model(model_path, file)
602
602
603
603
# Retrieve loaded model class by path to the file where it's supposed to be defined.
604
604
def get_loaded_model_by_path ( model_path )
605
- ActiveSupport ::Inflector . constantize ( ActiveSupport ::Inflector . camelize ( model_path ) )
605
+ klass = ActiveSupport ::Inflector . constantize ( ActiveSupport ::Inflector . camelize ( model_path ) )
606
+
607
+ klass if klass . is_a? ( Class ) && klass < ActiveRecord ::Base
606
608
rescue StandardError , LoadError
607
609
# Revert to the old way but it is not really robust
608
610
ObjectSpace . each_object ( ::Class )
Original file line number Diff line number Diff line change @@ -2074,7 +2074,40 @@ class Foo < ActiveRecord::Base
2074
2074
2075
2075
let :file_content_2 do
2076
2076
<<-EOS
2077
- class Bar::Foo
2077
+ class Bar::Foo < ActiveRecord::Base
2078
+ end
2079
+ EOS
2080
+ end
2081
+
2082
+ let :klass_2 do
2083
+ AnnotateModels . get_model_class ( File . join ( AnnotateModels . model_dir [ 0 ] , filename_2 ) )
2084
+ end
2085
+
2086
+ it 'finds valid model' do
2087
+ expect ( klass . name ) . to eq ( 'Foo' )
2088
+ expect ( klass_2 . name ) . to eq ( 'Bar::Foo' )
2089
+ end
2090
+ end
2091
+
2092
+ context 'the class name and base name clash' do
2093
+ let :filename do
2094
+ 'foo.rb'
2095
+ end
2096
+
2097
+ let :file_content do
2098
+ <<-EOS
2099
+ class Foo < ActiveRecord::Base
2100
+ end
2101
+ EOS
2102
+ end
2103
+
2104
+ let :filename_2 do
2105
+ 'bar/foo.rb'
2106
+ end
2107
+
2108
+ let :file_content_2 do
2109
+ <<-EOS
2110
+ class Bar::Foo < ActiveRecord::Base
2078
2111
end
2079
2112
EOS
2080
2113
end
@@ -2108,7 +2141,7 @@ class Voucher < ActiveRecord::Base
2108
2141
let :file_content_2 do
2109
2142
<<~EOS
2110
2143
class Voucher
2111
- class Foo
2144
+ class Foo < ActiveRecord::Base
2112
2145
end
2113
2146
end
2114
2147
EOS
You can’t perform that action at this time.
0 commit comments