Skip to content

Commit

Permalink
Use wiring role when inserting message if using connects_to
Browse files Browse the repository at this point in the history
  • Loading branch information
npezza93 committed Nov 19, 2024
1 parent 0d2b112 commit b6e006a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/models/solid_cable/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit b6e006a

Please sign in to comment.