Module: Planify::User

Includes:
ClassHelper
Defined in:
lib/planify/user.rb,
lib/planify/user/plan_info.rb,
lib/planify/user/limitable_counts.rb

Defined Under Namespace

Classes: LimitableCounts, PlanInfo

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassHelper

#normalize_class

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
# File 'lib/planify/user.rb', line 10

def self.included(base)
  base.class_eval do
    embeds_one :planify_plan_info, as: :planify_user, class_name: "Planify::User::PlanInfo", validate: false
    embeds_one :planify_limitable_counts, as: :planify_user, class_name: "Planify::User::LimitableCounts", validate: false
  end
end

Instance Method Details

#can_create?(limitable) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/planify/user.rb', line 56

def can_create?(limitable)
  key = normalize_class(limitable)
  limitable_counts.fetch(key) < plan.limit(key)
end

#created(limitable) ⇒ Object



46
47
48
49
# File 'lib/planify/user.rb', line 46

def created(limitable)
  key = normalize_class(limitable)
  limitable_counts.increment(key)
end

#creation_count(limitable) ⇒ Object



41
42
43
44
# File 'lib/planify/user.rb', line 41

def creation_count(limitable)
  key = normalize_class(limitable)
  limitable_counts.fetch(key)
end

#destroyed(limitable) ⇒ Object



51
52
53
54
# File 'lib/planify/user.rb', line 51

def destroyed(limitable)
  key = normalize_class(limitable)
  limitable_counts.decrement(key)
end

#has_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/planify/user.rb', line 61

def has_feature?(feature)
  plan.feature_enabled?(feature)
end

#has_plan(plan_name, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/planify/user.rb', line 17

def has_plan(plan_name, &block)
  plan = Planify::Plans.get(plan_name)
  plan_info = PlanInfo.new(name: plan_name)

  if block_given?
    configuration = Planify::Plan.new
    configuration.instance_eval &block

    plan_info.limit_overrides = configuration.limits.all
    plan_info.feature_overrides = configuration.features
  end

  self.planify_plan_info = plan_info
  nil
end

#limitable_countsObject



37
38
39
# File 'lib/planify/user.rb', line 37

def limitable_counts
  self.planify_limitable_counts ||= LimitableCounts.new
end

#planObject



33
34
35
# File 'lib/planify/user.rb', line 33

def plan
  @plan ||= load_plan_from_info(self.planify_plan_info)
end