From ae497aae8569b00a5fb303299d111ef56ff6d89a Mon Sep 17 00:00:00 2001 From: Jose Farias Date: Wed, 23 Oct 2024 14:51:12 -0600 Subject: [PATCH] Clear up dependency destruction chain Buckets destroy their bubbles, which destroy their thread entries, which destroy their threadables, which is how comments end up destroyed, and then rollups end up deleting their events --- app/models/bubble/commentable.rb | 2 +- app/models/bubble/eventable.rb | 2 +- app/models/concerns/threadable.rb | 2 +- app/models/rollup.rb | 2 +- app/models/thread_entry.rb | 2 +- test/fixtures/thread_entries.yml | 12 ++++++++++++ 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/models/bubble/commentable.rb b/app/models/bubble/commentable.rb index 1fdc9b13f..1af10eacf 100644 --- a/app/models/bubble/commentable.rb +++ b/app/models/bubble/commentable.rb @@ -2,7 +2,7 @@ module Bubble::Commentable extend ActiveSupport::Concern included do - has_many :comments, dependent: :destroy + has_many :comments scope :ordered_by_comments, -> { left_joins(:comments).group(:id).order("COUNT(comments.id) DESC") } end diff --git a/app/models/bubble/eventable.rb b/app/models/bubble/eventable.rb index 0288e6b73..78e1559bf 100644 --- a/app/models/bubble/eventable.rb +++ b/app/models/bubble/eventable.rb @@ -2,7 +2,7 @@ module Bubble::Eventable extend ActiveSupport::Concern included do - has_many :events, dependent: :delete_all + has_many :events after_create -> { track_event :created } end diff --git a/app/models/concerns/threadable.rb b/app/models/concerns/threadable.rb index 10759aa98..c449d2c08 100644 --- a/app/models/concerns/threadable.rb +++ b/app/models/concerns/threadable.rb @@ -2,7 +2,7 @@ module Threadable extend ActiveSupport::Concern included do - has_one :thread_entry, as: :threadable, dependent: :destroy + has_one :thread_entry, as: :threadable after_create { create_thread_entry! bubble: bubble } after_update { bubble.touch } diff --git a/app/models/rollup.rb b/app/models/rollup.rb index 1968e4ce8..06a6bedf5 100644 --- a/app/models/rollup.rb +++ b/app/models/rollup.rb @@ -3,5 +3,5 @@ class Rollup < ApplicationRecord belongs_to :bubble - has_many :events, -> { chronologically } + has_many :events, -> { chronologically }, dependent: :delete_all end diff --git a/app/models/thread_entry.rb b/app/models/thread_entry.rb index 3efa6b97f..16e5dec0f 100644 --- a/app/models/thread_entry.rb +++ b/app/models/thread_entry.rb @@ -1,7 +1,7 @@ class ThreadEntry < ApplicationRecord belongs_to :bubble - delegated_type :threadable, types: %w[ Comment Rollup ] + delegated_type :threadable, types: %w[ Comment Rollup ], dependent: :destroy scope :chronologically, -> { order created_at: :asc, id: :desc } end diff --git a/test/fixtures/thread_entries.yml b/test/fixtures/thread_entries.yml index 4137af55b..df1634b9f 100644 --- a/test/fixtures/thread_entries.yml +++ b/test/fixtures/thread_entries.yml @@ -24,5 +24,17 @@ logo_5: created_at: <%= 1.hour.ago %> layout_1: + bubble: layout + threadable: layout_initial_activity (Rollup) + +layout_2: bubble: layout threadable: layout_overflowing_david (Comment) + +text_1: + bubble: text + threadable: text_initial_activity (Rollup) + +shipping_1: + bubble: shipping + threadable: shipping_initial_activity (Rollup)