Skip to content

Commit

Permalink
initialize temporal classes of subclasses
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
doits committed Apr 18, 2024
1 parent d502ba4 commit f493db2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/temporal_tables/history_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def self.history # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
# Traverse associations and make sure they have
# history classes too.
history_class.constantize.temporalize_associations!

# Traverse subclasses and make sure they have
# history classes too.
history_class.constantize.temporalize_subclasses!

history_class.constantize
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/temporal_tables/temporal_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def self.temporalize_associations! # rubocop:disable Metrics/MethodLength, Metri
)
end
end

# Iterates all subclasses, makes sure their history classes are
# created and initialized
def self.temporalize_subclasses!
orig_class.subclasses.each do |subclass|
# skip temporal classes: they are subclasses, too
next if subclass.include?(TemporalClass)

# call .history to ensure temporal class is created
subclass.history
end
end
end
end

Expand Down

0 comments on commit f493db2

Please sign in to comment.