Assume my users can subscribe to several plan_types. I want to define those plans as classes, and be able to keep, for each user, a reference to a subscription_plan and also a presubscription_plan. This is like a State pattern :
class Payment::SubscriptionPlan
def self.max_subscription_quantity
120
end
end
class Payment::ConcretePlan1 < Payment::SubscriptionPlan
def self.commitment_period
2.months
end
end
class Payment::ConcretePlan2 < Payment::SubscriptionPlan
def self.max_subscription_quantity
50
end
end
What is the best way to "persist" the relation to a class in the database ? Currently I was doing the following using the as_enum gem
class Settings
setting :subscription_plans, type: Array, default: [
Payment::ConcretePlan1,
Payment::ConcretePlan2
]
end
class User
as_enum :subscription_plan, Settings.subscription_plans, map: :string
as_enum :presubscription_plan, Settings.subscription_plans, map: :string
def subscription_plan
return nil if super.nil?
Payment.const_get(super.to_s.demodulize)
end
def presubscription_plan
return nil if super.nil?
Payment.const_get(super.to_s.demodulize)
end
end
The current implementation works but I'm not sure this is the best way to tackle the problem
