Module: Turbo::Broadcastable::ClassMethods
- Defined in:
- app/models/concerns/turbo/broadcastable.rb
Instance Method Summary collapse
-
#broadcast_target_default ⇒ Object
All default targets will use the return of this method.
-
#broadcasts(stream = model_name.plural, inserts_by: :append, target: broadcast_target_default, **rendering) ⇒ Object
Same as
#broadcasts_to
, but the designated stream for updates and destroys is automatically set to the current model, for creates - to the model plural name, which can be overriden by passingstream
. -
#broadcasts_refreshes(stream = model_name.plural) ⇒ Object
Same as
#broadcasts_refreshes_to
, but the designated stream for page refreshes is automatically set to the current model, for creates - to the model plural name, which can be overriden by passingstream
. -
#broadcasts_refreshes_to(stream) ⇒ Object
Configures the model to broadcast a “page refresh” on creates, updates, and destroys to a stream name derived at runtime by the
stream
symbol invocation. -
#broadcasts_to(stream, inserts_by: :append, target: broadcast_target_default, **rendering) ⇒ Object
Configures the model to broadcast creates, updates, and destroys to a stream name derived at runtime by the
stream
symbol invocation. - #suppressed_turbo_broadcasts? ⇒ Boolean
-
#suppressing_turbo_broadcasts(&block) ⇒ Object
Executes
block
preventing both synchronous and asynchronous broadcasts from this model.
Instance Method Details
#broadcast_target_default ⇒ Object
All default targets will use the return of this method. Overwrite if you want something else than model_name.plural
.
222 223 224 |
# File 'app/models/concerns/turbo/broadcastable.rb', line 222 def broadcast_target_default model_name.plural end |
#broadcasts(stream = model_name.plural, inserts_by: :append, target: broadcast_target_default, **rendering) ⇒ Object
Same as #broadcasts_to
, but the designated stream for updates and destroys is automatically set to the current model, for creates - to the model plural name, which can be overriden by passing stream
.
191 192 193 194 195 |
# File 'app/models/concerns/turbo/broadcastable.rb', line 191 def broadcasts(stream = model_name.plural, inserts_by: :append, target: broadcast_target_default, **rendering) after_create_commit -> { broadcast_action_later_to(stream, action: inserts_by, target: target.try(:call, self) || target, **rendering) } after_update_commit -> { broadcast_replace_later(**rendering) } after_destroy_commit -> { broadcast_remove } end |
#broadcasts_refreshes(stream = model_name.plural) ⇒ Object
Same as #broadcasts_refreshes_to
, but the designated stream for page refreshes is automatically set to the current model, for creates - to the model plural name, which can be overriden by passing stream
.
215 216 217 218 219 |
# File 'app/models/concerns/turbo/broadcastable.rb', line 215 def broadcasts_refreshes(stream = model_name.plural) after_create_commit -> { broadcast_refresh_later_to(stream) } after_update_commit -> { broadcast_refresh_later } after_destroy_commit -> { broadcast_refresh } end |
#broadcasts_refreshes_to(stream) ⇒ Object
Configures the model to broadcast a “page refresh” on creates, updates, and destroys to a stream name derived at runtime by the stream
symbol invocation. Examples:
class Message < ApplicationRecord
belongs_to :board
broadcasts_refreshes_to :board
end
class Message < ApplicationRecord
belongs_to :board
broadcasts_refreshes_to ->() { [ .board, :messages ] }
end
209 210 211 |
# File 'app/models/concerns/turbo/broadcastable.rb', line 209 def broadcasts_refreshes_to(stream) after_commit -> { broadcast_refresh_later_to(stream.try(:call, self) || send(stream)) } end |
#broadcasts_to(stream, inserts_by: :append, target: broadcast_target_default, **rendering) ⇒ Object
Configures the model to broadcast creates, updates, and destroys to a stream name derived at runtime by the stream
symbol invocation. By default, the creates are appended to a dom id target name derived from the model’s plural name. The insertion can also be made to be a prepend by overwriting inserts_by
and the target dom id overwritten by passing target
. Examples:
class Message < ApplicationRecord
belongs_to :board
broadcasts_to :board
end
class Message < ApplicationRecord
belongs_to :board
broadcasts_to ->() { [ .board, :messages ] }, inserts_by: :prepend, target: "board_messages"
end
class Message < ApplicationRecord
belongs_to :board
broadcasts_to ->() { [ .board, :messages ] }, partial: "messages/custom_message"
end
183 184 185 186 187 |
# File 'app/models/concerns/turbo/broadcastable.rb', line 183 def broadcasts_to(stream, inserts_by: :append, target: broadcast_target_default, **rendering) after_create_commit -> { broadcast_action_later_to(stream.try(:call, self) || send(stream), action: inserts_by, target: target.try(:call, self) || target, **rendering) } after_update_commit -> { broadcast_replace_later_to(stream.try(:call, self) || send(stream), **rendering) } after_destroy_commit -> { broadcast_remove_to(stream.try(:call, self) || send(stream)) } end |
#suppressed_turbo_broadcasts? ⇒ Boolean
234 235 236 |
# File 'app/models/concerns/turbo/broadcastable.rb', line 234 def suppressed_turbo_broadcasts? suppressed_turbo_broadcasts end |
#suppressing_turbo_broadcasts(&block) ⇒ Object
Executes block
preventing both synchronous and asynchronous broadcasts from this model.
227 228 229 230 231 232 |
# File 'app/models/concerns/turbo/broadcastable.rb', line 227 def suppressing_turbo_broadcasts(&block) original, self.suppressed_turbo_broadcasts = self.suppressed_turbo_broadcasts, true yield ensure self.suppressed_turbo_broadcasts = original end |