Module: DurableStreams::Rails::Broadcastable::ClassMethods

Defined in:
app/models/concerns/durable_streams/rails/broadcastable.rb

Instance Method Summary collapse

Instance Method Details

#streams(stream = model_name.plural) ⇒ Object

Same as #streams_to, but the designated stream is automatically set to the current model, which can be overridden by passing stream. Examples:

class Board < ApplicationRecord
  streams
end

class Board < ApplicationRecord
  streams "boards"
end


87
88
89
90
91
# File 'app/models/concerns/durable_streams/rails/broadcastable.rb', line 87

def streams(stream = model_name.plural)
  after_create_commit  -> { stream_insert_later_to(stream) }
  after_update_commit  -> { stream_update_later }
  after_destroy_commit -> { stream_delete_later }
end

#streams_to(stream) ⇒ Object

Configures the model to broadcast creates, updates, and destroys to a stream name derived at runtime by the stream symbol invocation. Examples:

class Comment < ApplicationRecord
  belongs_to :post
  streams_to :post
end

class Comment < ApplicationRecord
  belongs_to :post
  streams_to ->(comment) { [ comment.post, :comments ] }
end


71
72
73
74
75
# File 'app/models/concerns/durable_streams/rails/broadcastable.rb', line 71

def streams_to(stream)
  after_create_commit  -> { stream_insert_later_to(stream.try(:call, self) || send(stream)) }
  after_update_commit  -> { stream_update_later_to(stream.try(:call, self) || send(stream)) }
  after_destroy_commit -> { stream_delete_later_to(stream.try(:call, self) || send(stream)) }
end

#suppressed_streams?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/concerns/durable_streams/rails/broadcastable.rb', line 101

def suppressed_streams?
  suppressed_streams
end

#suppressing_streams(&block) ⇒ Object

Executes block preventing both synchronous and asynchronous broadcasts from this model.



94
95
96
97
98
99
# File 'app/models/concerns/durable_streams/rails/broadcastable.rb', line 94

def suppressing_streams(&block)
  original, self.suppressed_streams = self.suppressed_streams, true
  yield
ensure
  self.suppressed_streams = original
end