Skip to content

Instantly share code, notes, and snippets.

@adarsh
Last active April 6, 2020 18:17
Show Gist options
  • Save adarsh/51c9988d828a5de361a4 to your computer and use it in GitHub Desktop.
Save adarsh/51c9988d828a5de361a4 to your computer and use it in GitHub Desktop.
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
@adarsh
Copy link
Author

adarsh commented Apr 6, 2020

PLANS should be it's own value object, now that I look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment