Introduce Common#activity_key_prefix to allow key customization. (In support of STI) #392
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
This PR introduces the
activity_key_prefix
method inPublicActivity::Common
.This method returns
self.class.name
by default. The result of this method is used to determine the prefix of activity keys when only an action is provided tocreate_activity
. Example usage below.Why?
This change is in support of using single table inheritance. This is not a generalized approach to supporting STI, but it provides a mechanism for users to obtain the behavior necessary for maintaining the same trackable_type, key structure and association queries for parent and child classes.
Below is a demonstration of the current issue preventing simple STI usage and the ability to resolve the issue by overriding
activity_key_prefix
.With these models:
The current behavior is:
Notice that the activity key is inconsistent with the
trackable_type
and association. Ideally a generalized solution would exist to support STI in this way, but changing the default behavior would likely break existing implementations somewhere. The ideal approach would be to scan up the ancestry tree to find the ancestor that directly included public activity and use that class name. In the meantime, this PR simple provides a mechanism to implement this behavior on a per-model basis. Like so:I realize that my desired STI behavior may not align with others who want to use the child class specific key to do i18n lookups on a child class basis. However, this change will not effect existing implementations, it simply gives users the ability to customize the prefix however they want, if they want.