count-anything/app/channels/count_channel.rb
Louis Vallat d07857c011
feat: added counts and instant update and destroy
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
2023-02-05 21:01:07 +01:00

41 lines
777 B
Ruby

class CountChannel < ApplicationCable::Channel
def subscribed
stream_from "counts"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def plus_one(params)
unless current_user.nil?
total = Total.find(params["id"])
unless total.nil?
total.update(count: total.count + 1)
end
end
end
def minus_one(params)
unless current_user.nil?
total = Total.find(params["id"])
unless total.nil?
total.update(count: total.count - 1)
end
end
end
def self.create(total)
end
def self.update(total)
ActionCable.server.broadcast("counts", {"update": total})
end
def self.destroy(total)
ActionCable.server.broadcast("counts", {"destroy": total.id})
end
end