Only broadcast when the preview changes, use the _later variant for the broadcast, add tests
This commit is contained in:
@@ -3,5 +3,16 @@ module Card::Broadcastable
|
||||
|
||||
included do
|
||||
broadcasts_refreshes
|
||||
|
||||
before_update :remember_if_preview_changed
|
||||
end
|
||||
|
||||
private
|
||||
def remember_if_preview_changed
|
||||
@preview_changed ||= title_changed? || column_id_changed? || board_id_changed?
|
||||
end
|
||||
|
||||
def preview_changed?
|
||||
@preview_changed
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,13 +4,7 @@ module Card::Pinnable
|
||||
included do
|
||||
has_many :pins, dependent: :destroy
|
||||
|
||||
after_update_commit :broadcast_pin_updates
|
||||
end
|
||||
|
||||
def broadcast_pin_updates
|
||||
pins.each do |pin|
|
||||
pin.broadcast_replace_to [ pin.user, :pins_tray ], partial: "my/pins/pin"
|
||||
end
|
||||
after_update_commit :broadcast_pin_updates, if: :preview_changed?
|
||||
end
|
||||
|
||||
def pinned_by?(user)
|
||||
@@ -28,4 +22,11 @@ module Card::Pinnable
|
||||
def unpin_by(user)
|
||||
pins.find_by(user: user).tap { it.destroy }
|
||||
end
|
||||
|
||||
private
|
||||
def broadcast_pin_updates
|
||||
pins.find_each do |pin|
|
||||
pin.broadcast_replace_later_to [ pin.user, :pins_tray ], partial: "my/pins/pin"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
require "test_helper"
|
||||
|
||||
class Card::PinnableTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
Current.session = sessions(:david)
|
||||
end
|
||||
|
||||
test "broadcasts pin update when title changes" do
|
||||
assert_broadcasted_pin_update do
|
||||
cards(:logo).update!(title: "New title")
|
||||
end
|
||||
end
|
||||
|
||||
test "broadcasts pin update when column changes" do
|
||||
assert_broadcasted_pin_update do
|
||||
cards(:logo).update!(column: columns(:writebook_in_progress))
|
||||
end
|
||||
end
|
||||
|
||||
test "broadcasts pin update when board changes" do
|
||||
assert_broadcasted_pin_update do
|
||||
cards(:logo).update!(board: boards(:private), column: nil)
|
||||
end
|
||||
end
|
||||
|
||||
test "does not broadcast pin update when other properties change" do
|
||||
perform_enqueued_jobs do
|
||||
assert_turbo_stream_broadcasts([ pins(:logo_kevin).user, :pins_tray ], count: 0) do
|
||||
cards(:logo).update!(last_active_at: Time.current)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def assert_broadcasted_pin_update(&block)
|
||||
perform_enqueued_jobs do
|
||||
assert_turbo_stream_broadcasts([ pins(:logo_kevin).user, :pins_tray ], &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user