This gem provides a shorthand DSL for including re-usable sets of mixin modules across a large set of STI models. If you find yourself with a folder of subclasses full of boilerplate and shared functionality, this gem will let you replace those files with one or a few small files that read more descriptively, like so:
MySuperClass.describe_descendants_with(MySuperClassMixins) do
type :some_type
type :another_subtype do
email_addressable
end
type :some_domain_event do
email_addressable
payment_validatable
end
end
My blog post describes the evolution of this gem at Hired pretty well.
TL;DR imagine the example in the section below with a marketplace’s domain described therein.
Here’s a piece of the real-world example that gave life to this gem:
# config/initializers/activities.rb
Activity.describe_descendants_with(Activity::Descriptors) do
type :completed_survey do
user_required
end
type :bid_on_developer do
approved_employers_only
target_required
end
type :auction_membership_confirmed do
approved_developers_only
actor_unique_to_auction
target_required
end
# ... others omitted for brevity ...
end
Given this example, one is describing the descendants of an Activity class (that also live on the activities table with a type column for STI), the following is happening.
So the above is basically shorthand for this:
# in models/completed_survey.rb
class CompletedSurvey < Activity
include Activity::Descriptors::UserRequired
end
# in models/bid_on_developer.rb
class BidOnDeveloper < Activity
include Activity::Descriptors::ApprovedEmployersOnly
include Activity::Descriptors::TargetRequired
end
# in models/auction_membership_confirmed.rb
class AuctionMembershipConfirmed < Activity
include Activity::Descriptors::ApprovedEmployersOnly
include Activity::Descriptors::ActorUniqueToAuction
include Activity::Descriptors::TargetRequired
end