File tree Expand file tree Collapse file tree 3 files changed +40
-5
lines changed Expand file tree Collapse file tree 3 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -104,8 +104,8 @@ Into environment gems from Github checkout:
104104
105105 git clone https://github.com/ctran/annotate_models.git annotate_models
106106 cd annotate_models
107- rake build
108- gem install pkg /annotate-*.gem
107+ rake gem
108+ gem install dist /annotate-*.gem
109109
110110## Usage
111111
Original file line number Diff line number Diff line change @@ -617,7 +617,9 @@ def get_loaded_model(model_path, file)
617617
618618 # Retrieve loaded model class by path to the file where it's supposed to be defined.
619619 def get_loaded_model_by_path ( model_path )
620- ActiveSupport ::Inflector . constantize ( ActiveSupport ::Inflector . camelize ( model_path ) )
620+ klass = ActiveSupport ::Inflector . constantize ( ActiveSupport ::Inflector . camelize ( model_path ) )
621+
622+ klass if klass . is_a? ( Class ) && klass < ActiveRecord ::Base
621623 rescue StandardError , LoadError
622624 # Revert to the old way but it is not really robust
623625 ObjectSpace . each_object ( ::Class )
Original file line number Diff line number Diff line change @@ -2109,7 +2109,40 @@ class Foo < ActiveRecord::Base
21092109
21102110 let :file_content_2 do
21112111 <<-EOS
2112- class Bar::Foo
2112+ class Bar::Foo < ActiveRecord::Base
2113+ end
2114+ EOS
2115+ end
2116+
2117+ let :klass_2 do
2118+ AnnotateModels . get_model_class ( File . join ( AnnotateModels . model_dir [ 0 ] , filename_2 ) )
2119+ end
2120+
2121+ it 'finds valid model' do
2122+ expect ( klass . name ) . to eq ( 'Foo' )
2123+ expect ( klass_2 . name ) . to eq ( 'Bar::Foo' )
2124+ end
2125+ end
2126+
2127+ context 'the class name and base name clash' do
2128+ let :filename do
2129+ 'foo.rb'
2130+ end
2131+
2132+ let :file_content do
2133+ <<-EOS
2134+ class Foo < ActiveRecord::Base
2135+ end
2136+ EOS
2137+ end
2138+
2139+ let :filename_2 do
2140+ 'bar/foo.rb'
2141+ end
2142+
2143+ let :file_content_2 do
2144+ <<-EOS
2145+ class Bar::Foo < ActiveRecord::Base
21132146 end
21142147 EOS
21152148 end
@@ -2158,7 +2191,7 @@ class Voucher < ActiveRecord::Base
21582191 let :file_content_2 do
21592192 <<~EOS
21602193 class Voucher
2161- class Foo
2194+ class Foo < ActiveRecord::Base
21622195 end
21632196 end
21642197 EOS
You can’t perform that action at this time.
0 commit comments