Class: ActiverecordCallbackLens::Collector::CallbackCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_callback_lens/collector/callback_collector.rb

Overview

Reads ActiveRecord’s internal callback chains for a model class and returns an array of CallbackDefinition structs with raw condition data, ready for the Parser layer to populate condition_tree.

Each of the five callback events (save, create, update, destroy, validation) is accessed through ActiveRecord’s private chain accessor and every ActiveSupport::Callbacks::Callback in the chain is converted into one CallbackDefinition.

Constant Summary collapse

EVENTS =

The callback events enumerated, in a stable order.

%i[save create update destroy validation].freeze
CHAIN_METHODS =

Maps each event to ActiveRecord’s internal callback chain accessor.

{
  save: :_save_callbacks,
  create: :_create_callbacks,
  update: :_update_callbacks,
  destroy: :_destroy_callbacks,
  validation: :_validation_callbacks
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ CallbackCollector

Returns a new instance of CallbackCollector.

Parameters:

  • model_class (Class)

    an ActiveRecord::Base subclass



37
38
39
# File 'lib/activerecord_callback_lens/collector/callback_collector.rb', line 37

def initialize(model_class)
  @model_class = model_class
end

Class Method Details

.collect(model_class) ⇒ Array<CallbackDefinition>

Collect all callbacks registered on a model class.

Parameters:

  • model_class (Class)

    an ActiveRecord::Base subclass

Returns:



32
33
34
# File 'lib/activerecord_callback_lens/collector/callback_collector.rb', line 32

def self.collect(model_class)
  new(model_class).collect
end

Instance Method Details

#collectArray<CallbackDefinition>

Returns:



42
43
44
# File 'lib/activerecord_callback_lens/collector/callback_collector.rb', line 42

def collect
  EVENTS.flat_map { |event| collect_event(event) }
end