Class: PricingPlans::AssociationLimitRegistry

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

Overview

Stores has_many limited_by_pricing_plans declarations that could not be resolved at declaration time (e.g., child class not loaded yet). Flushed after registry configuration or on engine to_prepare.

Class Method Summary collapse

Class Method Details

.flush_pending!Object



17
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
# File 'lib/pricing_plans/association_limit_registry.rb', line 17

def flush_pending!
  pending.delete_if do |entry|
    owner = entry[:plan_owner_class]
    assoc = owner.reflect_on_association(entry[:association_name])
    next false unless assoc

    begin
      child_klass = assoc.klass
      child_klass.include PricingPlans::Limitable unless child_klass.ancestors.include?(PricingPlans::Limitable)
      opts = entry[:options]
      limit_key = (opts[:limit_key] || entry[:association_name]).to_sym
      # Define sugar methods on the plan owner when the association resolves
      PricingPlans::PlanOwner.define_limit_sugar_methods(owner, limit_key)
      child_klass.limited_by_pricing_plans(
        limit_key,
        plan_owner: child_klass.reflections.values.find { |r| r.macro == :belongs_to && r.foreign_key.to_s == assoc.foreign_key.to_s }&.name || owner.name.underscore.to_sym,
        per: opts[:per],
        error_after_limit: opts[:error_after_limit],
        count_scope: opts[:count_scope]
      )
      true
    rescue StandardError
      false
    end
  end
end

.pendingObject



9
10
11
# File 'lib/pricing_plans/association_limit_registry.rb', line 9

def pending
  @pending ||= []
end

.register(plan_owner_class:, association_name:, options:) ⇒ Object



13
14
15
# File 'lib/pricing_plans/association_limit_registry.rb', line 13

def register(plan_owner_class:, association_name:, options:)
  pending << { plan_owner_class: plan_owner_class, association_name: association_name, options: options }
end