File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -87,11 +87,22 @@ def self.try_load_create_model(result, id)
8787 ddoc = result &.content &.[]( 'type' )
8888 return nil unless ddoc
8989
90- ::CouchbaseOrm ::Base . descendants . each do |model |
91- if model . design_document == ddoc
90+ models_to_visit = [ ::CouchbaseOrm ::Base ]
91+ visited_models = { }
92+
93+ until models_to_visit . empty?
94+ model = models_to_visit . shift
95+ next if visited_models [ model ]
96+
97+ visited_models [ model ] = true
98+
99+ if model != ::CouchbaseOrm ::Base && model . design_document == ddoc
92100 return model . instantiate ( result . content , id , nil , model )
93101 end
102+
103+ models_to_visit . concat ( Array . wrap ( model . descendants ) ) if model . respond_to? ( :descendants )
94104 end
105+
95106 nil
96107 end
97108end
Original file line number Diff line number Diff line change @@ -24,6 +24,17 @@ class BaseTestWithIgnoredProperties < CouchbaseOrm::Base
2424 attribute :job , :string
2525end
2626
27+ class ApplicationRecord < CouchbaseOrm ::Base
28+ end
29+
30+ class EventBase < ApplicationRecord
31+ attribute :name , :string
32+ end
33+
34+ class SendSmsEvent < EventBase
35+ attribute :phone_number , :string
36+ end
37+
2738describe CouchbaseOrm ::Base do
2839 it 'is comparable to other objects' do
2940 base = BaseTest . create! ( name : 'joe' )
@@ -153,6 +164,18 @@ class BaseTestWithIgnoredProperties < CouchbaseOrm::Base
153164 base . destroy
154165 end
155166
167+ it 'tries to load with descendant chain ApplicationRecord -> EventBase -> SendSmsEvent' do
168+ event = SendSmsEvent . create! ( name : 'sms' , phone_number : '+15551234567' )
169+ loaded = CouchbaseOrm . try_load ( event . id )
170+
171+ expect ( loaded ) . to be_a ( SendSmsEvent )
172+ expect ( loaded . id ) . to eq ( event . id )
173+ expect ( loaded . name ) . to eq ( 'sms' )
174+ expect ( loaded . phone_number ) . to eq ( '+15551234567' )
175+ ensure
176+ event &.destroy
177+ end
178+
156179 it 'is able to create model with a custom ID' do
157180 base = BaseTest . create! ( id : 'custom_id' , name : 'joe' )
158181 expect ( base . id ) . to eq ( 'custom_id' )
You can’t perform that action at this time.
0 commit comments