2023-02-04 14:22:05 +01:00
|
|
|
class CountChannel < ApplicationCable::Channel
|
2023-02-05 23:10:37 +01:00
|
|
|
|
2023-02-04 14:22:05 +01:00
|
|
|
def subscribed
|
2023-02-05 21:01:07 +01:00
|
|
|
stream_from "counts"
|
2023-02-04 14:22:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def unsubscribed
|
|
|
|
# Any cleanup needed when channel is unsubscribed
|
|
|
|
end
|
2023-02-05 21:01:07 +01:00
|
|
|
|
|
|
|
def plus_one(params)
|
2023-02-05 23:10:37 +01:00
|
|
|
unless current_user.nil? || params["id"].nil?
|
2023-02-05 21:01:07 +01:00
|
|
|
total = Total.find(params["id"])
|
|
|
|
unless total.nil?
|
|
|
|
total.update(count: total.count + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def minus_one(params)
|
2023-02-05 23:10:37 +01:00
|
|
|
unless current_user.nil? || params["id"].nil?
|
2023-02-05 21:01:07 +01:00
|
|
|
total = Total.find(params["id"])
|
|
|
|
unless total.nil?
|
|
|
|
total.update(count: total.count - 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update(total)
|
2023-02-05 23:10:37 +01:00
|
|
|
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)
|
|
|
|
}})
|
2023-02-05 21:01:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.destroy(total)
|
|
|
|
ActionCable.server.broadcast("counts", {"destroy": total.id})
|
|
|
|
end
|
|
|
|
|
2023-02-04 14:22:05 +01:00
|
|
|
end
|