Class: VoteFu::VotesChannel

Inherits:
ApplicationCable::Channel show all
Defined in:
app/channels/vote_fu/votes_channel.rb

Class Method Summary collapse

Instance Method Summary collapse

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, message)
  stream_name = "vote_fu:#{voteable.class.name}:#{voteable.id}"
  ActionCable.server.broadcast(stream_name, message)
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: render_widget_html(voteable, scope)
    })
  end
end

Instance Method Details

#subscribedObject

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

#unsubscribedObject



19
20
21
# File 'app/channels/vote_fu/votes_channel.rb', line 19

def unsubscribed
  stop_all_streams
end