From 6cfc0978a4f76c8d41687920dadb74ae31d0e598 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Thu, 30 Oct 2025 15:49:37 +0100 Subject: [PATCH] Columns touch the cards when the name/color changes --- .../collections/columns/closeds_controller.rb | 2 +- .../collections/columns/not_nows_controller.rb | 2 +- .../collections/columns/streams_controller.rb | 2 +- .../collections/columns_controller.rb | 2 +- app/controllers/concerns/collection_scoped.rb | 4 ---- app/helpers/cards_helper.rb | 2 +- app/jobs/card/touch_all_job.rb | 7 +++++++ app/models/collection/cards.rb | 13 +++++++++++++ app/models/column.rb | 9 +++++++++ test/models/collection/cards_test.rb | 15 +++++++++++++++ test/models/column_test.rb | 16 ++++++++++++++++ 11 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 app/jobs/card/touch_all_job.rb create mode 100644 app/models/collection/cards.rb create mode 100644 test/models/collection/cards_test.rb diff --git a/app/controllers/collections/columns/closeds_controller.rb b/app/controllers/collections/columns/closeds_controller.rb index c2c81ae85..af78c1e25 100644 --- a/app/controllers/collections/columns/closeds_controller.rb +++ b/app/controllers/collections/columns/closeds_controller.rb @@ -3,6 +3,6 @@ class Collections::Columns::ClosedsController < ApplicationController def show set_page_and_extract_portion_from @collection.cards.closed.recently_closed_first - cards_fresh_when @page.records + fresh_when @page.records end end diff --git a/app/controllers/collections/columns/not_nows_controller.rb b/app/controllers/collections/columns/not_nows_controller.rb index 3eb4d21c4..aacb7186f 100644 --- a/app/controllers/collections/columns/not_nows_controller.rb +++ b/app/controllers/collections/columns/not_nows_controller.rb @@ -3,6 +3,6 @@ class Collections::Columns::NotNowsController < ApplicationController def show set_page_and_extract_portion_from @collection.cards.postponed.reverse_chronologically.with_golden_first - cards_fresh_when @page.records + fresh_when @page.records end end diff --git a/app/controllers/collections/columns/streams_controller.rb b/app/controllers/collections/columns/streams_controller.rb index 305f0edc6..8eb7da7da 100644 --- a/app/controllers/collections/columns/streams_controller.rb +++ b/app/controllers/collections/columns/streams_controller.rb @@ -3,6 +3,6 @@ class Collections::Columns::StreamsController < ApplicationController def show set_page_and_extract_portion_from @collection.cards.awaiting_triage.by_last_activity.with_golden_first - cards_fresh_when @page.records + fresh_when @page.records end end diff --git a/app/controllers/collections/columns_controller.rb b/app/controllers/collections/columns_controller.rb index 7f50a734e..95af8ac34 100644 --- a/app/controllers/collections/columns_controller.rb +++ b/app/controllers/collections/columns_controller.rb @@ -5,7 +5,7 @@ class Collections::ColumnsController < ApplicationController def show set_page_and_extract_portion_from @column.cards.active.by_last_activity.with_golden_first - cards_fresh_when @page.records + fresh_when @page.records end def create diff --git a/app/controllers/concerns/collection_scoped.rb b/app/controllers/concerns/collection_scoped.rb index 8636ecd77..2ddde9531 100644 --- a/app/controllers/concerns/collection_scoped.rb +++ b/app/controllers/concerns/collection_scoped.rb @@ -9,8 +9,4 @@ module CollectionScoped def set_collection @collection = Current.user.collections.find(params[:collection_id]) end - - def cards_fresh_when(cards) - fresh_when etag: [ cards, @collection.entropy_configuration, @collection.columns.all ] - end end diff --git a/app/helpers/cards_helper.rb b/app/helpers/cards_helper.rb index 3753bcfc1..4bf463a7e 100644 --- a/app/helpers/cards_helper.rb +++ b/app/helpers/cards_helper.rb @@ -48,6 +48,6 @@ module CardsHelper end def card_preview_cache_parts(card) - [ card, card.collection.entropy_configuration, card.collection.publication, card.column ] + [ card, card.collection.entropy_configuration ] end end diff --git a/app/jobs/card/touch_all_job.rb b/app/jobs/card/touch_all_job.rb new file mode 100644 index 000000000..7b96fa5b7 --- /dev/null +++ b/app/jobs/card/touch_all_job.rb @@ -0,0 +1,7 @@ +class Card::TouchAllJob < ApplicationJob + queue_as :backend + + def perform(container) + container.cards.in_batches(&:touch_all) + end +end diff --git a/app/models/collection/cards.rb b/app/models/collection/cards.rb new file mode 100644 index 000000000..bc67c7531 --- /dev/null +++ b/app/models/collection/cards.rb @@ -0,0 +1,13 @@ +module Collection::Cards + extend ActiveSupport::Concern + + included do + has_many :cards, dependent: :destroy + + after_update_commit :touch_all_cards_later, if: :saved_change_to_name? + end + + def touch_all_cards_later + Card::TouchAllJob.perform_later(self) + end +end diff --git a/app/models/column.rb b/app/models/column.rb index 7585c39be..6a30b3e77 100644 --- a/app/models/column.rb +++ b/app/models/column.rb @@ -8,9 +8,18 @@ class Column < ApplicationRecord validates :color, presence: true before_validation :set_default_color + after_save_commit :touch_all_cards_later, if: :should_invalidate_cards? private def set_default_color self.color ||= Card::DEFAULT_COLOR end + + def touch_all_cards_later + Card::TouchAllJob.perform_later(self) + end + + def should_invalidate_cards? + saved_change_to_name? || saved_change_to_color? + end end diff --git a/test/models/collection/cards_test.rb b/test/models/collection/cards_test.rb new file mode 100644 index 000000000..f2ec9b9db --- /dev/null +++ b/test/models/collection/cards_test.rb @@ -0,0 +1,15 @@ +require "test_helper" + +class Collection::CardsTest < ActiveSupport::TestCase + test "touch cards when the name changes" do + collection = collections(:writebook) + + assert_enqueued_with(job: Card::TouchAllJob) do + collection.update!(name: "New Name") + end + + assert_no_enqueued_jobs(only: Card::TouchAllJob) do + collection.update!(updated_at: 1.hour.from_now) + end + end +end diff --git a/test/models/column_test.rb b/test/models/column_test.rb index b65ff2037..f0eab9a2f 100644 --- a/test/models/column_test.rb +++ b/test/models/column_test.rb @@ -6,4 +6,20 @@ class ColumnTest < ActiveSupport::TestCase assert_equal Card::DEFAULT_COLOR, column.color end + + test "touch all the cards when the name or color changes" do + column = columns(:writebook_triage) + + assert_enqueued_with(job: Card::TouchAllJob) do + column.update!(name: "New Name") + end + + assert_enqueued_with(job: Card::TouchAllJob) do + column.update!(color: "#FF0000") + end + + assert_no_enqueued_jobs(only: Card::TouchAllJob) do + column.update!(updated_at: 1.hour.from_now) + end + end end