From b6e006ae157d6828036bd5513beaf1fe91c37f7e Mon Sep 17 00:00:00 2001 From: Nick Pezza Date: Tue, 19 Nov 2024 18:30:53 -0500 Subject: [PATCH] Use wiring role when inserting message if using connects_to --- app/models/solid_cable/message.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/solid_cable/message.rb b/app/models/solid_cable/message.rb index 193d45a..eee8297 100644 --- a/app/models/solid_cable/message.rb +++ b/app/models/solid_cable/message.rb @@ -12,8 +12,10 @@ class Message < SolidCable::Record class << self def broadcast(channel, payload) - insert({ created_at: Time.current, channel:, payload:, - channel_hash: channel_hash_for(channel) }) + using_writing_role do + insert({ created_at: Time.current, channel:, payload:, + channel_hash: channel_hash_for(channel) }) + end end def channel_hashes_for(channels) @@ -25,6 +27,16 @@ def channel_hashes_for(channels) def channel_hash_for(channel) Digest::SHA256.digest(channel.to_s).unpack1("q>") end + + private + + def using_writing_role + if SolidCable.connects_to.present? + ActiveRecord::Base.connected_to(role: :writing) { yield } + else + yield + end + end end end end