From 1b3ff6a92fb9e46bc8396734fdb42462bbe33e7e Mon Sep 17 00:00:00 2001 From: David Yip Date: Sat, 27 Jan 2018 01:31:22 -0600 Subject: [PATCH] Cascade mute/block destruction to notes (#193) --- app/models/block.rb | 2 +- app/models/mute.rb | 2 +- spec/models/block_spec.rb | 8 ++++++++ spec/models/mute_spec.rb | 9 +++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/models/block.rb b/app/models/block.rb index da045efa7..3e308a058 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -21,7 +21,7 @@ class Block < ApplicationRecord after_create :remove_blocking_cache after_destroy :remove_blocking_cache - has_one :note, class_name: 'Glitch::Note', as: :target + has_one :note, class_name: 'Glitch::Note', as: :target, dependent: :destroy private diff --git a/app/models/mute.rb b/app/models/mute.rb index 5f4e6ccf2..227a5e3a1 100644 --- a/app/models/mute.rb +++ b/app/models/mute.rb @@ -22,7 +22,7 @@ class Mute < ApplicationRecord after_create :remove_blocking_cache after_destroy :remove_blocking_cache - has_one :note, class_name: 'Glitch::Note', as: :target + has_one :note, class_name: 'Glitch::Note', as: :target, dependent: :destroy private diff --git a/spec/models/block_spec.rb b/spec/models/block_spec.rb index acbdc77f5..43c901f13 100644 --- a/spec/models/block_spec.rb +++ b/spec/models/block_spec.rb @@ -44,4 +44,12 @@ RSpec.describe Block, type: :model do expect(Rails.cache.exist?("exclude_account_ids_for:#{account.id}")).to eq false expect(Rails.cache.exist?("exclude_account_ids_for:#{target_account.id}")).to eq false end + + it 'removes associated note after destruction' do + block = Fabricate(:block) + block.create_note!(note: 'Too many oats') + block.destroy! + + expect(Glitch::Note.count).to eq(0) + end end diff --git a/spec/models/mute_spec.rb b/spec/models/mute_spec.rb index 83ba793b2..81c8cb7fc 100644 --- a/spec/models/mute_spec.rb +++ b/spec/models/mute_spec.rb @@ -1,5 +1,14 @@ require 'rails_helper' RSpec.describe Mute, type: :model do + subject { Fabricate(:mute) } + describe '#destroy' do + it 'destroys associated notes' do + subject.create_note!(note: 'Too many jorts') + subject.destroy! + + expect(Glitch::Note.count).to eq(0) + end + end end