I'm working on a rails application that will allow customers to sign
up for an account with a particular plan (i.e. Free, Basic, Premium,
etc) that defines what features are available, etc. Pretty standard
stuff you see everywhere, however, not something I've implemented
before.
I'm struggling with trying to figure out "the rails way" - if there is
one. Are there best practices you guys typically follow when
implementing something like this. Don't want to reinvent the wheel
here.
The direction I've started down is something like this:
class Account < ActiveRecord::Base
belongs_to :subscription_plan
delegate :limit, :to => :subscription_plan
delegate :feature_allowed, :to => :subscription_plan
end
# Table name: subscription_plans
#
# id :integer not null, primary key
# plan_name :string(255)
# limit :integer
# feature_allowed :boolean
class SubscriptionPlan < ActiveRecord::Base
has_many :accounts
end
But I'm not sure if making the SubscriptionPlan an active record class
and defining all the plans in the database is the best way to go, or
whether it makes more sense to create something like
FreeSubscriptionPlan, BasicSubscriptionPlan, PremiumSubscriptionPlan,
etc as just regular classes.
Anybody with thoughts on this or a pointer to some good resources?
Robin,
You can always define a plan structure as a quick hash in
environment.rb instead of using heavy duty AR classes:
PLANS = {
:free => { :cents => 0, :name => 'Free', :max_pics => 10 },
:basic => { :cents => 2900, :name => 'Basic', :max_pics => 500 },
:plus => { :cents => 4900, :name => 'Plus', :max_pics => 1000 },
:premium => { :cents => 9900, :name => 'Premium', :max_pics => 5000 }
}
Then you can use the hash to lookup names based on cents and so forth...
I would recommend allowing accounts be able to have multiple subscriptions:
class Account < AR
has_many :subscriptions
has_one :current_subscription, { :class_name => 'Subscription',
:order => 'id desc' }
end
This way you can track subscription history.
Hope this helps,