In this episode, we're doing a bit of code clean-up by moving all the archivable-related logic out of our Card
and Column
models into a reusable trait. Since both models have the same archiving scopes and casts, instead of repeating ourselves, we'll create an Archivable
trait that can be included wherever we need this functionality.
We don't just move code blindly—there are a couple of things to be careful about, like how to merge the casts
property and handle dynamic table names. You'll see how we fix up the trait's boot logic and merge in the casts without overriding existing ones. We also add a new scope to the trait, making it easy to get the latest archived items by ordering them by the archived_at
timestamp.
Finally, we plug our new trait into both the Card
and Column
models, test archiving and unarchiving, and check that the ordering works as expected. This helps keep our codebase neat and DRY, and sets us up to add archiving to other models just by using the trait!