Skip to content

Commit ed6c55a

Browse files
Merge pull request #1931 from basecamp/pin-card-refresh
Update pinned cards when the title changes
2 parents 046a42c + 59ff50c commit ed6c55a

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
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/eventable.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def event_was_created(event)
1919
def touch_last_active_at
2020
# Not using touch so that we can detect attribute change on callbacks
2121
update!(last_active_at: Time.current)
22-
broadcast_activity
2322
end
2423

2524
private
@@ -36,8 +35,4 @@ def track_title_change
3635
def create_system_comment_for(event)
3736
SystemCommenter.new(self, event).comment
3837
end
39-
40-
def broadcast_activity
41-
broadcast_render_later_to self, :activity, partial: "card/display/refresh_activity", locals: { card: self }
42-
end
4338
end

app/models/card/pinnable.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Card::Pinnable
33

44
included do
55
has_many :pins, dependent: :destroy
6+
7+
after_update_commit :broadcast_pin_updates, if: :preview_changed?
68
end
79

810
def pinned_by?(user)
@@ -20,4 +22,11 @@ def pin_by(user)
2022
def unpin_by(user)
2123
pins.find_by(user: user).tap { it.destroy }
2224
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
2332
end

app/views/card/display/_refresh_activity.turbo_stream.erb

Lines changed: 0 additions & 4 deletions
This file was deleted.

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)