Destroy associated rules when a channel is destroyed

This commit is contained in:
David Taylor 2017-07-18 16:42:05 +01:00
parent bdb81191d7
commit 2275b048f9
2 changed files with 16 additions and 0 deletions

View File

@ -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?

View File

@ -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) { }