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? || params["id"].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? || params["id"].nil? total = Total.find(params["id"]) unless total.nil? total.update(count: total.count - 1) end end end def self.update(total) ActionCable.server.broadcast("counts", {"update": { id: total.id, name: total.name, count: total.count, edit_link: Rails.application.routes.url_helpers.edit_count_path(total) }}) end def self.destroy(total) ActionCable.server.broadcast("counts", {"destroy": total.id}) end end