@@ -711,11 +711,11 @@ def get_model_class(file)
711
711
model_path = file . gsub ( /\. rb$/ , '' )
712
712
model_dir . each { |dir | model_path = model_path . gsub ( /^#{ dir } / , '' ) . gsub ( /^\/ / , '' ) }
713
713
begin
714
- get_loaded_model ( model_path ) || raise ( BadModelFileError . new )
714
+ get_loaded_model_properly ( model_path , file ) || raise ( BadModelFileError . new )
715
715
rescue LoadError
716
716
# this is for non-rails projects, which don't get Rails auto-require magic
717
717
file_path = File . expand_path ( file )
718
- if File . file? ( file_path ) && silence_warnings { Kernel . require ( file_path ) }
718
+ if File . file? ( file_path ) && Kernel . require ( file_path )
719
719
retry
720
720
elsif model_path =~ /\/ /
721
721
model_path = model_path . split ( '/' ) [ 1 ..-1 ] . join ( '/' ) . to_s
@@ -726,6 +726,16 @@ def get_model_class(file)
726
726
end
727
727
end
728
728
729
+ #
730
+ def get_loaded_model_properly ( model_path , file )
731
+ klass = get_loaded_model ( model_path )
732
+ return klass if klass
733
+
734
+ absolute_file = File . expand_path ( file )
735
+ model_paths = $LOAD_PATH. select { |load_path | absolute_file . include? ( load_path ) } . map { |load_path | absolute_file . sub ( load_path , '' ) . sub ( /^\/ / , '' ) . sub ( /\. rb$/ , '' ) }
736
+ model_paths . map { |model_path | get_loaded_model ( model_path ) } . find { |loaded_model | !loaded_model . nil? }
737
+ end
738
+
729
739
# Retrieve loaded model class by path to the file where it's supposed to be defined.
730
740
def get_loaded_model ( model_path )
731
741
ActiveSupport ::Inflector . constantize ( ActiveSupport ::Inflector . camelize ( model_path ) )
@@ -858,15 +868,6 @@ def classified_sort(cols)
858
868
( [ id ] << rest_cols << timestamps << associations ) . flatten . compact
859
869
end
860
870
861
- # Ignore warnings for the duration of the block ()
862
- def silence_warnings
863
- old_verbose = $VERBOSE
864
- $VERBOSE = nil
865
- yield
866
- ensure
867
- $VERBOSE = old_verbose
868
- end
869
-
870
871
private
871
872
872
873
def with_comments? ( klass , options )
0 commit comments