Last active
April 6, 2020 18:17
-
-
Save adarsh/51c9988d828a5de361a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_one :subscription, dependent: :destroy | |
end | |
class Subscription < ActiveRecord::Base | |
acts_as_paranoid | |
belongs_to :user | |
enum plan: ServicePlans.plan_names | |
def data_range_query | |
{ created_at: data_range } | |
end | |
def beginning_of_oldest_allowed_date | |
(end_of_today - data_window_span).beginning_of_day | |
end | |
private | |
def data_range | |
Range.new(beginning_of_oldest_allowed_date, end_of_today) | |
end | |
def end_of_today | |
Time.zone.today.end_of_day | |
end | |
def data_window_span | |
plan_attributes.data_window_span | |
end | |
def plan_attributes | |
ServicePlans.new(user) | |
end | |
end | |
class ServicePlans | |
PLANS = { | |
free: { | |
index: 0, | |
data_window_span: 30.days, | |
export_allowed?: false, | |
}, | |
essential: { | |
index: 1, | |
data_window_span: 90.days, | |
export_allowed?: true, | |
}, | |
pro: { | |
index: 2, | |
data_window_span: 365.days, | |
export_allowed?: true, | |
}, | |
enterprise: { | |
index: 3, | |
data_window_span: (365 * 2).days, | |
export_allowed?: true, | |
}, | |
}.freeze | |
def self.default_plan | |
'pro' | |
end | |
def self.plan_names | |
PLANS.keys | |
end | |
def initialize(user) | |
@user = user | |
end | |
def data_window_span | |
attribute.fetch(:data_window_span) | |
end | |
def export_allowed? | |
attribute.fetch(:export_allowed?) | |
end | |
private | |
attr_reader :user | |
def attribute | |
PLANS.fetch(plan.to_sym) | |
end | |
def plan | |
subscription.plan | |
end | |
def subscription | |
user.subscription | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PLANS
should be it's own value object, now that I look at it.