Commit d8ed8a7f by John Doe

remove dependent objects on destroy

parent 82bfc966
......@@ -2,7 +2,10 @@
# Database object
class Db < ActiveRecord::Base
belongs_to :root_folder, foreign_key: 'db_folder_id', class_name: 'DbFolder'
belongs_to :root_folder,
foreign_key: 'db_folder_id',
class_name: 'DbFolder',
dependent: :destroy
belongs_to :nilm
def url
......
......@@ -4,8 +4,13 @@
# and one or more DbFolders as subfolders
class DbFolder < ActiveRecord::Base
belongs_to :parent, class_name: 'DbFolder'
has_many :subfolders, class_name: 'DbFolder', foreign_key: 'parent_id'
has_many :db_files
has_many :subfolders,
class_name: 'DbFolder',
foreign_key: 'parent_id',
dependent: :destroy
has_many :db_files,
dependent: :destroy
def self.defined_attributes
[:name, :description, :hidden]
......
......@@ -11,6 +11,14 @@ RSpec.describe 'DbFile' do
specify { expect(db_file).to respond_to(:hidden) }
end
it 'removes streams destroyed' do
stream = DbStream.create
file = DbFile.create
file.db_streams << stream
file.destroy
expect(DbStream.find_by_id(stream.id)).to be nil
end
describe 'remove' do
let(:db_streams) { FactoryGirl.build_list(:db_stream, 5) }
let(:db_file) { FactoryGirl.create(:db_file) }
......@@ -31,4 +39,5 @@ RSpec.describe 'DbFile' do
expect(db_service).to have_received(:remove_file)
end
end
end
......@@ -12,6 +12,23 @@ RSpec.describe 'DbFolder' do
specify { expect(db_folder).to respond_to(:hidden) }
end
describe 'when destroyed' do
before(:all) do
@folder = DbFolder.create
@subfolder = DbFolder.create
@file = DbFile.create
@folder.subfolders << @subfolder
@folder.db_files << @file
@folder.destroy
end
it 'removes subfolders' do
expect(DbFolder.find_by_id(@subfolder.id)).to be_nil
end
it 'removes subfiles' do
expect(DbFile.find_by_id(@file.id)).to be_nil
end
end
describe 'insert_file' do
let(:db_folder) { FactoryGirl.create(:db_folder) }
let(:new_file) { FactoryGirl.create(:db_file) }
......
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'DbStream' do
RSpec.describe 'Db' do
describe 'object' do
let(:db) { Db.new }
specify { expect(db).to respond_to(:url) }
specify { expect(db).to respond_to(:root_folder) }
end
it 'removes the root folder when destroyed' do
root_folder = DbFolder.create
db = Db.create(root_folder: root_folder)
db.destroy
expect(DbFolder.find_by_id(root_folder.id)).to be nil
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment