delete nested site_files when directory is deleted

This commit is contained in:
Kyle Drake 2015-02-13 11:54:18 -08:00
parent e6aaba74fb
commit fbe00f6f9c
3 changed files with 23 additions and 4 deletions

View file

@ -659,6 +659,11 @@ class Site < Sequel::Model
begin
FileUtils.rm files_path(path)
rescue Errno::EISDIR
site_files.each do |site_file|
if site_file.path.match /^#{path}\//
site_file.destroy
end
end
FileUtils.remove_dir files_path(path), true
rescue Errno::ENOENT
end

View file

@ -21,7 +21,7 @@ describe '/' do
@another_site = Fabricate :site
@followed_site.toggle_follow @another_site
visit '/'
find('.news-item', match: :first).text.must_match /#{@followed_site.username} followed #{@another_site.username}/i
find('.news-item', match: :first).text.must_match /#{@followed_site.username} started following the site of #{@another_site.username}/i
end
it 'loads my activities only' do
@ -37,7 +37,7 @@ describe '/' do
@followed_site = Fabricate :site
@site.toggle_follow @followed_site
visit "/?event_id=#{@followed_site.events.first.id}"
find('.news-item').text.must_match /you followed #{@followed_site.username}/i
find('.news-item').text.must_match /you started following the site of #{@followed_site.username}/i
end
end
@ -63,4 +63,4 @@ describe '/' do
end
end
end
end
end

View file

@ -31,6 +31,20 @@ describe 'site_files' do
File.exists?(file_path).must_equal false
SiteFile[site_id: @site.id, path: 'test.jpg'].must_be_nil
end
it 'deletes all files in a directory' do
upload(
'dir' => 'test',
'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
)
upload(
'dir' => '',
'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
)
delete_file filename: 'test'
@site.site_files.select {|f| f.path =~ /^test\//}.length.must_equal 0
@site.site_files.select {|f| f.path =~ /^test/}.length.must_equal 1
end
end
describe 'upload' do
@ -161,4 +175,4 @@ describe 'site_files' do
@site.reload.changed_count.must_equal 2
end
end
end
end