From 2275b048f9fa986af9ffb0bab25bd783f16072be Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 18 Jul 2017 16:42:05 +0100 Subject: [PATCH] Destroy associated rules when a channel is destroyed --- app/models/channel.rb | 5 +++++ spec/models/channel_spec.rb | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/models/channel.rb b/app/models/channel.rb index 7c3aeaa..a3238d0 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -10,6 +10,11 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel self.data = {} if self.data.nil? end + after_destroy :destroy_rules + def destroy_rules + rules.destroy_all() + end + validate :provider_valid?, :data_valid? def provider_valid? diff --git a/spec/models/channel_spec.rb b/spec/models/channel_spec.rb index 7670a0d..12147e2 100644 --- a/spec/models/channel_spec.rb +++ b/spec/models/channel_spec.rb @@ -52,6 +52,17 @@ RSpec.describe DiscourseChat::Channel do expect(channel.rules.size).to eq(2) end + it 'destroys its rules on destroy' do + channel = DiscourseChat::Channel.create({provider:'dummy'}) + expect(channel.rules.size).to eq(0) + rule1 = DiscourseChat::Rule.create(channel: channel) + rule2 = DiscourseChat::Rule.create(channel: channel) + + expect(DiscourseChat::Rule.with_channel(channel).exists?).to eq(true) + channel.destroy() + expect(DiscourseChat::Rule.with_channel(channel).exists?).to eq(false) + end + describe 'validations' do let(:channel) { }