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:
104
104
105
105
git clone https://github.com/ctran/annotate_models.git annotate_models
106
106
cd annotate_models
107
- rake build
108
- gem install pkg /annotate-*.gem
107
+ rake gem
108
+ gem install dist /annotate-*.gem
109
109
110
110
## Usage
111
111
Original file line number Diff line number Diff line change @@ -617,7 +617,9 @@ def get_loaded_model(model_path, file)
617
617
618
618
# Retrieve loaded model class by path to the file where it's supposed to be defined.
619
619
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
621
623
rescue StandardError , LoadError
622
624
# Revert to the old way but it is not really robust
623
625
ObjectSpace . each_object ( ::Class )
Original file line number Diff line number Diff line change @@ -2109,7 +2109,40 @@ class Foo < ActiveRecord::Base
2109
2109
2110
2110
let :file_content_2 do
2111
2111
<<-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
2113
2146
end
2114
2147
EOS
2115
2148
end
@@ -2158,7 +2191,7 @@ class Voucher < ActiveRecord::Base
2158
2191
let :file_content_2 do
2159
2192
<<~EOS
2160
2193
class Voucher
2161
- class Foo
2194
+ class Foo < ActiveRecord::Base
2162
2195
end
2163
2196
end
2164
2197
EOS
You can’t perform that action at this time.
0 commit comments