Class: PricingPlans::PlanResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/pricing_plans/plan_resolver.rb

Class Method Summary collapse

Class Method Details

.assign_plan_manually!(plan_owner, plan_key, source: "manual") ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/pricing_plans/plan_resolver.rb', line 71

def assign_plan_manually!(plan_owner, plan_key, source: "manual")
  LegacyPlanAssignmentApi.create_or_update_override!(
    plan_owner,
    plan_key,
    source: source,
    called_method_name: "PricingPlans::PlanResolver.assign_plan_manually!"
  )
end

.clear_pricing_plan_override_for!(plan_owner) ⇒ Object



67
68
69
# File 'lib/pricing_plans/plan_resolver.rb', line 67

def clear_pricing_plan_override_for!(plan_owner)
  Assignment.clear_pricing_plan_override_for!(plan_owner)
end

.effective_plan_for(plan_owner) ⇒ Object



10
11
12
# File 'lib/pricing_plans/plan_resolver.rb', line 10

def effective_plan_for(plan_owner)
  resolution_for(plan_owner).plan
end

.log_debug(message) ⇒ Object



6
7
8
# File 'lib/pricing_plans/plan_resolver.rb', line 6

def log_debug(message)
  puts message if PricingPlans.configuration&.debug
end

.override_pricing_plan_for!(plan_owner, plan_key, source:) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/pricing_plans/plan_resolver.rb', line 59

def override_pricing_plan_for!(plan_owner, plan_key, source:)
  Assignment.create_or_update_pricing_plan_override_for!(
    plan_owner,
    plan_key,
    source: source
  )
end

.plan_key_for(plan_owner) ⇒ Object



14
15
16
# File 'lib/pricing_plans/plan_resolver.rb', line 14

def plan_key_for(plan_owner)
  resolution_for(plan_owner).plan_key
end

.remove_manual_assignment!(plan_owner) ⇒ Object



80
81
82
83
84
85
# File 'lib/pricing_plans/plan_resolver.rb', line 80

def remove_manual_assignment!(plan_owner)
  LegacyPlanAssignmentApi.clear_override!(
    plan_owner,
    called_method_name: "PricingPlans::PlanResolver.remove_manual_assignment!"
  )
end

.resolution_for(plan_owner) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pricing_plans/plan_resolver.rb', line 18

def resolution_for(plan_owner)
  log_debug "[PricingPlans::PlanResolver] resolution_for called for #{plan_owner.class.name}##{plan_owner.respond_to?(:id) ? plan_owner.id : 'N/A'}"

  assignment = pricing_plan_override_for(plan_owner)
  subscription = current_subscription_for(plan_owner)

  if assignment
    log_debug "[PricingPlans::PlanResolver] Returning explicit-override resolution: #{assignment.plan_key}"
    return PlanResolution.new(
      plan: Registry.plan(assignment.plan_key),
      source: :assignment,
      assignment: assignment,
      subscription: subscription
    )
  end

  if subscription
    processor_plan = subscription.processor_plan
    log_debug "[PricingPlans::PlanResolver] resolution_for subscription processor_plan = #{processor_plan.inspect}"

    if processor_plan && (plan = plan_from_processor_plan(processor_plan))
      log_debug "[PricingPlans::PlanResolver] Returning subscription-backed resolution: #{plan.key}"
      return PlanResolution.new(
        plan: plan,
        source: :subscription,
        assignment: nil,
        subscription: subscription
      )
    end
  end

  default = Registry.default_plan
  log_debug "[PricingPlans::PlanResolver] Returning default-backed resolution: #{default ? default.key : 'nil'}"
  PlanResolution.new(
    plan: default,
    source: :default,
    assignment: nil,
    subscription: subscription
  )
end