Skip to content

Commit c89db89

Browse files
committed
Only broadcast when the preview changes, use the _later variant for the broadcast, add tests
1 parent bcad6b9 commit c89db89

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

app/models/card/broadcastable.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,16 @@ module Card::Broadcastable
33

44
included do
55
broadcasts_refreshes
6+
7+
before_update :remember_if_preview_changed
68
end
9+
10+
private
11+
def remember_if_preview_changed
12+
@preview_changed ||= title_changed? || column_id_changed? || board_id_changed?
13+
end
14+
15+
def preview_changed?
16+
@preview_changed
17+
end
718
end

app/models/card/pinnable.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ module Card::Pinnable
44
included do
55
has_many :pins, dependent: :destroy
66

7-
after_update_commit :broadcast_pin_updates
8-
end
9-
10-
def broadcast_pin_updates
11-
pins.each do |pin|
12-
pin.broadcast_replace_to [ pin.user, :pins_tray ], partial: "my/pins/pin"
13-
end
7+
after_update_commit :broadcast_pin_updates, if: :preview_changed?
148
end
159

1610
def pinned_by?(user)
@@ -28,4 +22,11 @@ def pin_by(user)
2822
def unpin_by(user)
2923
pins.find_by(user: user).tap { it.destroy }
3024
end
25+
26+
private
27+
def broadcast_pin_updates
28+
pins.find_each do |pin|
29+
pin.broadcast_replace_later_to [ pin.user, :pins_tray ], partial: "my/pins/pin"
30+
end
31+
end
3132
end

test/models/card/pinnable_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require "test_helper"
2+
3+
class Card::PinnableTest < ActiveSupport::TestCase
4+
setup do
5+
Current.session = sessions(:david)
6+
end
7+
8+
test "broadcasts pin update when title changes" do
9+
assert_broadcasted_pin_update do
10+
cards(:logo).update!(title: "New title")
11+
end
12+
end
13+
14+
test "broadcasts pin update when column changes" do
15+
assert_broadcasted_pin_update do
16+
cards(:logo).update!(column: columns(:writebook_in_progress))
17+
end
18+
end
19+
20+
test "broadcasts pin update when board changes" do
21+
assert_broadcasted_pin_update do
22+
cards(:logo).update!(board: boards(:private), column: nil)
23+
end
24+
end
25+
26+
test "does not broadcast pin update when other properties change" do
27+
perform_enqueued_jobs do
28+
assert_turbo_stream_broadcasts([ pins(:logo_kevin).user, :pins_tray ], count: 0) do
29+
cards(:logo).update!(last_active_at: Time.current)
30+
end
31+
end
32+
end
33+
34+
private
35+
def assert_broadcasted_pin_update(&block)
36+
perform_enqueued_jobs do
37+
assert_turbo_stream_broadcasts([ pins(:logo_kevin).user, :pins_tray ], &block)
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)