Module: Undertow::DSL

Defined in:
lib/undertow/dsl.rb

Overview

Class-level DSL extended onto ActiveRecord::Base by the Railtie. Any model that calls these methods automatically registers itself with Undertow and gets Trackable behavior wired in at boot, no include needed.

class Activity < ApplicationRecord
  undertow_on_drain ->(model_name, ids, deleted_ids) { ActivityReindexJob.perform_later(ids, deleted_ids) }
  undertow_skip     %w[lock_version searchkick_reindexing]

  undertow_depends_on :provider, foreign_key: :provider_id, watched_columns: %w[approved mobile]
  undertow_depends_on :location_series,
                      resolver:        ->(ls) { Activity.where(series_id: ls.series_id) },
                      watched_columns: %w[location_id hidden]
end

Instance Method Summary collapse

Instance Method Details

#undertow_depends_on(association, foreign_key: nil, resolver: nil, watched_columns: nil) ⇒ Object

Track invalidations from an upstream association. ‘watched_columns` accepts strings or symbols; blanks are ignored. Empty/nil means no watched-column filter.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/undertow/dsl.rb', line 34

def undertow_depends_on(association, foreign_key: nil, resolver: nil, watched_columns: nil)
  raise ArgumentError, 'provide exactly one of foreign_key: or resolver:' unless foreign_key.nil? ^ resolver.nil?

  normalized_watched = Array(watched_columns).map(&:to_s).
    reject(&:blank?).uniq.presence

  _undertow_config.dependencies << {
     association: association,
     foreign_key: foreign_key,
     resolver: resolver,
     watched_columns: normalized_watched
  }.freeze
  _undertow_ensure_trackable!
end

#undertow_on_drain(callable) ⇒ Object



19
20
21
22
# File 'lib/undertow/dsl.rb', line 19

def undertow_on_drain(callable)
  _undertow_config.on_drain = callable
  _undertow_ensure_trackable!
end

#undertow_skip(columns) ⇒ Object

Suppress self-tracking when these root-model columns are the only changes. Accepts strings or symbols; blanks are ignored.



26
27
28
29
# File 'lib/undertow/dsl.rb', line 26

def undertow_skip(columns)
  _undertow_config.skip_columns = Array(columns).map(&:to_s).reject(&:blank?).uniq
  _undertow_ensure_trackable!
end