Skip to content

Commit

Permalink
initialize temporal classes of subclasses (#39)
Browse files Browse the repository at this point in the history
* 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
```

* rubocop
  • Loading branch information
doits authored Oct 19, 2024
1 parent 07d61c7 commit 6afd324
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/temporal_tables/temporal_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions spec/basic_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@
end
end

describe 'when working with STI and using superclass history' do
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

describe 'when working with STI one level deep' do
let!(:broom) { Broom.create person: emily, model: 'Cackler 2000' }

Expand Down

0 comments on commit 6afd324

Please sign in to comment.