Class: VoteFu::VotesChannel
- Inherits:
-
ApplicationCable::Channel
- Object
- ActionCable::Channel::Base
- ApplicationCable::Channel
- VoteFu::VotesChannel
- Defined in:
- app/channels/vote_fu/votes_channel.rb
Class Method Summary collapse
-
.broadcast_to_voteable(voteable, message) ⇒ Object
Broadcast to all subscribers of a voteable (any scope).
-
.broadcast_vote_update(voteable, vote: nil, action: :updated) ⇒ Object
Broadcast vote update to all subscribers.
Instance Method Summary collapse
-
#subscribed ⇒ Object
Subscribe to vote updates for a specific voteable.
- #unsubscribed ⇒ Object
Class Method Details
.broadcast_to_voteable(voteable, message) ⇒ Object
Broadcast to all subscribers of a voteable (any scope)
49 50 51 52 |
# File 'app/channels/vote_fu/votes_channel.rb', line 49 def self.broadcast_to_voteable(voteable, ) stream_name = "vote_fu:#{voteable.class.name}:#{voteable.id}" ActionCable.server.broadcast(stream_name, ) end |
.broadcast_vote_update(voteable, vote: nil, action: :updated) ⇒ Object
Broadcast vote update to all subscribers
Called from Vote model callbacks or controller
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/channels/vote_fu/votes_channel.rb', line 27 def self.broadcast_vote_update(voteable, vote: nil, action: :updated) return unless VoteFu.configuration.turbo_broadcasts scopes = [nil] + voteable.received_votes.distinct.pluck(:scope).compact scopes.each do |scope| stream_name = stream_name_for(voteable, scope) ActionCable.server.broadcast(stream_name, { type: "vote_update", action: action, voteable_type: voteable.class.name, voteable_id: voteable.id, scope: scope, vote: vote&.as_json(only: i[id value created_at]), stats: vote_stats(voteable, scope), html: (voteable, scope) }) end end |
Instance Method Details
#subscribed ⇒ Object
Subscribe to vote updates for a specific voteable
Params:
voteable_type: "Post"
voteable_id: 123
scope: "quality" (optional)
12 13 14 15 16 17 |
# File 'app/channels/vote_fu/votes_channel.rb', line 12 def subscribed voteable = find_voteable return reject unless voteable stream_for voteable_stream_name end |
#unsubscribed ⇒ Object
19 20 21 |
# File 'app/channels/vote_fu/votes_channel.rb', line 19 def unsubscribed stop_all_streams end |