From fbe00f6f9c0fe2075a59a441e399bed7975e4473 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Fri, 13 Feb 2015 11:54:18 -0800 Subject: [PATCH] delete nested site_files when directory is deleted --- models/site.rb | 5 +++++ tests/acceptance/index_tests.rb | 6 +++--- tests/site_file_tests.rb | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/models/site.rb b/models/site.rb index b66e8e81..964c7f23 100644 --- a/models/site.rb +++ b/models/site.rb @@ -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 diff --git a/tests/acceptance/index_tests.rb b/tests/acceptance/index_tests.rb index 756c71b9..19c01800 100644 --- a/tests/acceptance/index_tests.rb +++ b/tests/acceptance/index_tests.rb @@ -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 \ No newline at end of file +end diff --git a/tests/site_file_tests.rb b/tests/site_file_tests.rb index a1fba1c3..d0e8d8da 100644 --- a/tests/site_file_tests.rb +++ b/tests/site_file_tests.rb @@ -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 \ No newline at end of file +end