@@ -24,26 +24,32 @@ def belongs_to(name, scope = nil, **options)
2424 mapping
2525 end
2626
27- define_method foreign_type do
28- t = super ( )
29- mapping [ t ]
30- end
27+ foreign_type_extension = Module . new do
28+ define_method foreign_type do
29+ t = super ( )
30+ self . class . send ( "#{ foreign_type } _mapping" ) [ t ]
31+ end
3132
32- define_method "#{ foreign_type } =" do |klass |
33- enum = mapping . key ( klass . to_s )
34- if klass . kind_of? ( Class ) && klass <= ActiveRecord ::Base
35- enum ||= mapping . key ( klass . base_class . to_s )
36- enum ||= mapping . key ( klass . base_class . sti_name )
33+ define_method "#{ foreign_type } =" do |klass |
34+ mapping = self . class . send ( "#{ foreign_type } _mapping" )
35+ enum = mapping . key ( klass . to_s )
36+ if klass . kind_of? ( Class ) && klass <= ActiveRecord ::Base
37+ enum ||= mapping . key ( klass . sti_name )
38+ enum ||= mapping . key ( klass . base_class . to_s )
39+ enum ||= mapping . key ( klass . base_class . sti_name )
40+ end
41+ enum ||= klass if klass != NilClass
42+ super ( enum )
3743 end
38- enum ||= klass if klass != NilClass
39- super ( enum )
40- end
4144
42- define_method "#{ name } =" do |record |
43- super ( record )
44- send ( "#{ foreign_type } =" , record . class )
45+ define_method "#{ name } =" do |record |
46+ super ( record )
47+ send ( "#{ foreign_type } =" , record . class )
48+ end
4549 end
4650
51+ include ( foreign_type_extension )
52+
4753 validate do
4854 t = send ( foreign_type )
4955 unless t . nil? || mapping . values . include? ( t )
0 commit comments