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 Post < ApplicationRecord
undertow_sink(:search_index) { |model_name, upserted_ids, deleted_ids| PostSyncJob.perform_later(upserted_ids, deleted_ids) }
undertow_skip %w[view_count updated_at]

undertow_depends_on :author, foreign_key: :author_id, watched_columns: %w[name bio]
undertow_depends_on :tag,
                    resolver:        ->(tag) { Post.joins(:post_tags).where(post_tags: { tag_id: tag.id }) },
                    watched_columns: %w[name slug]
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)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/undertow/dsl.rb', line 47

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_sink(name, max_batch_size: nil, &handler) ⇒ Object

Registers a named sink. Call once per sink; every sink on a model receives the same (model_name, upserted_ids, deleted_ids) on each drain, so the block decides what to do with them, e.g. reindex a search index or publish to Kafka.

max_batch_size: bounds how many IDs this sink's block receives per call. Defaults to Undertow.configuration.max_batch, the same size as the popped batch, meaning the block gets called once with everything. Set it lower when this sink's downstream call has a tighter limit than other sinks on the same model; DrainJob will call the block multiple times, chunked to this size, without affecting any other sink's batch size.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
# File 'lib/undertow/dsl.rb', line 30

def undertow_sink(name, max_batch_size: nil, &handler)
  raise ArgumentError, 'undertow_sink requires a block' unless handler

  _undertow_config.sinks[name.to_sym] = { max_batch_size: max_batch_size, handler: handler }
  _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.



39
40
41
42
# File 'lib/undertow/dsl.rb', line 39

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