diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 31b6dae1b..a85f6678a 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -73,7 +73,8 @@ class RepositoriesController < ApplicationController def destroy flash[:success] = t('repositories.index.delete_flash', name: @repository.name) - @repository.destroy + @repository.discard + ClearDiscardedRepositoriesJob.perform_later redirect_to team_repositories_path end diff --git a/app/jobs/clear_discarded_repositories_job.rb b/app/jobs/clear_discarded_repositories_job.rb new file mode 100644 index 000000000..bcd704944 --- /dev/null +++ b/app/jobs/clear_discarded_repositories_job.rb @@ -0,0 +1,11 @@ +class ClearDiscardedRepositoriesJob + class << self + def perform_later + Repository.with_discarded.discarded.destroy_all + end + + handle_asynchronously :perform_later, + queue: :clear_discarded_repositories, + priority: 20 + end +end diff --git a/app/services/smart_annotations/html_preview.rb b/app/services/smart_annotations/html_preview.rb index 9e87ea974..1650db644 100644 --- a/app/services/smart_annotations/html_preview.rb +++ b/app/services/smart_annotations/html_preview.rb @@ -45,13 +45,13 @@ module SmartAnnotations def generate_rep_item_snippet(name, object) if object - repository_name = object.repository.name + repository_name = fetch_repository_name(object) return "" \ "#{trim_repository_name(repository_name)} " \ "#{object.name}" end - "Rep " \ + "Inv " \ "#{name} #{I18n.t('atwho.res.deleted')}" end @@ -66,6 +66,13 @@ module SmartAnnotations return "#{names[0].slice(0..1)}#{names[1][0]}" if size == 2 "#{names[0][0]}#{names[1][0]}#{names[2][0]}" end + + def fetch_repository_name(object) + return object.repository.name if object.repository + repository = Repository.with_discarded.find_by_id(object.repository_id) + return 'Inv' unless repository + repository.name + end end end end diff --git a/app/services/smart_annotations/permision_eval.rb b/app/services/smart_annotations/permision_eval.rb index dc8dd41ed..51f563ed9 100644 --- a/app/services/smart_annotations/permision_eval.rb +++ b/app/services/smart_annotations/permision_eval.rb @@ -24,7 +24,12 @@ module SmartAnnotations end def validate_rep_item_permissions(user, object) - can_read_team?(user, object.repository.team) + return can_read_team?(user, object.repository.team) if object.repository + # handles discarded repositories + repository = Repository.with_discarded.find_by_id(object.repository_id) + # evaluate to false if repository not found + return false unless repository + can_read_team?(user, repository.team) end end end diff --git a/lib/tasks/clear_discarded_repositories.rake b/lib/tasks/clear_discarded_repositories.rake deleted file mode 100644 index a791ed68b..000000000 --- a/lib/tasks/clear_discarded_repositories.rake +++ /dev/null @@ -1,6 +0,0 @@ -namespace :clear_discarded_repositories do - desc 'Removes all discarded repositories' - task run: :environment do - Repository.with_discarded.discarded.destroy_all - end -end diff --git a/lib/tasks/clear_repository_rows_and_columns_without_repository.rake b/lib/tasks/clear_repository_rows_and_columns_without_repository.rake new file mode 100644 index 000000000..c2076a898 --- /dev/null +++ b/lib/tasks/clear_repository_rows_and_columns_without_repository.rake @@ -0,0 +1,9 @@ +namespace :clear_repository_rows_and_columns_without_repository do + desc 'Removes all repository rows/columns and ' \ + 'referenced entities with dependent: destroy' + task run: :environment do + repository_ids = Repository.select(:id) + RepositoryRow.where.not(repository_id: repository_ids).delete_all + RepositoryColumn.where.not(repository_id: repository_ids).delete_all + end +end diff --git a/spec/services/smart_annotations/html_preview_spec.rb b/spec/services/smart_annotations/html_preview_spec.rb index fb79699c2..e74ee5f36 100644 --- a/spec/services/smart_annotations/html_preview_spec.rb +++ b/spec/services/smart_annotations/html_preview_spec.rb @@ -47,7 +47,7 @@ describe SmartAnnotations::HtmlPreview do it 'returns a html snippet' do snippet = subject.html('my item', 'rep_item', nil) expect(snippet).to eq( - 'Rep my item (deleted)' + 'Inv my item (deleted)' ) end end