Class: Acta::Projection

Inherits:
Handler show all
Defined in:
lib/acta/projection.rb

Constant Summary collapse

APPLYING_FLAG =
:acta_projection_applying

Class Method Summary collapse

Methods inherited from Handler

on

Class Method Details

.applying!Object

Mark the current thread as inside projection-side code for the duration of the block. Acta sets this internally when invoking projection handlers and during ‘Acta.rebuild!`’s truncate phase, so ‘acta_managed!` AR models know to allow the writes.

Apps can wrap fixture setup, migrations, or one-off backfill operations in ‘Acta::Projection.applying! { … }` to bypass the safety net intentionally.



52
53
54
55
56
57
58
# File 'lib/acta/projection.rb', line 52

def self.applying!
  previous = Thread.current[APPLYING_FLAG]
  Thread.current[APPLYING_FLAG] = true
  yield
ensure
  Thread.current[APPLYING_FLAG] = previous
end

.applying?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/acta/projection.rb', line 60

def self.applying?
  Thread.current[APPLYING_FLAG] == true
end

.inherited(subclass) ⇒ Object



7
8
9
10
# File 'lib/acta/projection.rb', line 7

def self.inherited(subclass)
  super
  Acta.register_projection(subclass)
end

.truncate!Object

Default implementation deletes every row in each declared class. Apps override ‘truncate!` directly if they need custom teardown logic; in that case `truncates` still drives FK-based ordering and the override provides the actual deletion.



40
41
42
# File 'lib/acta/projection.rb', line 40

def self.truncate!
  truncated_classes.each(&:delete_all)
end

.truncated_classesObject



32
33
34
# File 'lib/acta/projection.rb', line 32

def self.truncated_classes
  @truncated_classes ||= []
end

.truncates(*ar_classes) ⇒ Object

Declare the AR classes whose rows this projection owns. Acta uses these declarations both as the default ‘truncate!` target list and as input to `Acta.rebuild!`’s cross-projection ordering — projections whose tables are FK-referenced by another projection’s tables run first, so children are deleted before their parents.

class CatalogProjection < Acta::Projection
  truncates Trail, Zone   # within-projection: child first, parent second

  on ZoneRegistered { |e| Zone.create!(...) }
  on TrailRegistered { |e| Trail.create!(...) }
end

Pass classes in safe within-projection order (children before parents); Acta only orders projections relative to each other via the global FK graph, not the within-projection list.



28
29
30
# File 'lib/acta/projection.rb', line 28

def self.truncates(*ar_classes)
  @truncated_classes = ar_classes
end