From 028199cbd935b253948746ee3b141e777a36b13c Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Thu, 18 Apr 2024 16:09:22 +0200 Subject: [PATCH 1/2] initialize temporal classes of subclasses history classes where not correctly initialized in such a case: ```rb class User end class BigUser < User end User.history.last # => exception if it is a BigUserHistory without this patch ``` --- lib/temporal_tables/temporal_class.rb | 3 +++ spec/basic_history_spec.rb | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/temporal_tables/temporal_class.rb b/lib/temporal_tables/temporal_class.rb index cc7f49a..ec435d6 100644 --- a/lib/temporal_tables/temporal_class.rb +++ b/lib/temporal_tables/temporal_class.rb @@ -59,6 +59,9 @@ def find_sti_class(type_name) type_name += 'History' unless type_name =~ /History\Z/ begin + # Calling .history makes sure history class is created + type_name.sub(/History$/, '').constantize.history + super rescue ActiveRecord::SubclassNotFound superclass.send(:find_sti_class, type_name) diff --git a/spec/basic_history_spec.rb b/spec/basic_history_spec.rb index dcf4465..5d49165 100644 --- a/spec/basic_history_spec.rb +++ b/spec/basic_history_spec.rb @@ -147,6 +147,14 @@ end end + describe 'when working with STI and using superclass history' do + let!(:broom) { Broom.create person: emily, model: 'Cackler 2000' } + + it 'allows to fetch history entry of subclass history' do + expect(FlyingMachine.history.last.class.name).to eql('BroomHistory') + end + end + describe 'when working with STI one level deep' do let!(:broom) { Broom.create person: emily, model: 'Cackler 2000' } From 00372e1cf7931ab844893dbac470c627bfbdaf3b Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Wed, 24 Apr 2024 18:30:06 +0200 Subject: [PATCH 2/2] rubocop --- spec/basic_history_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/basic_history_spec.rb b/spec/basic_history_spec.rb index 5d49165..819d2c6 100644 --- a/spec/basic_history_spec.rb +++ b/spec/basic_history_spec.rb @@ -148,9 +148,8 @@ end describe 'when working with STI and using superclass history' do - let!(:broom) { Broom.create person: emily, model: 'Cackler 2000' } - it 'allows to fetch history entry of subclass history' do + Broom.create person: emily, model: 'Cackler 2000' expect(FlyingMachine.history.last.class.name).to eql('BroomHistory') end end