Skip to content

Commit 5899726

Browse files
authored
Merge pull request #563 from wri/fix/soft-deleting-annex-should-not-remove-file
Fix/soft deleting annex should not remove file
2 parents 4dc786b + 5455c32 commit 5899726

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

app/admin/operator_document_annex.rb

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99

1010
active_admin_paranoia
1111

12-
# To include the deleted operator document annexes
13-
scope_to do
14-
Class.new do
15-
def self.operator_document_annexes
16-
OperatorDocumentAnnex.unscoped.distinct
17-
end
18-
end
19-
end
20-
2112
controller do
2213
def scoped_collection
2314
end_of_association_chain.includes([:user, annex_documents: [documentable: [:operator, required_operator_document: :translations]]])
@@ -45,11 +36,7 @@ def scoped_collection
4536
:attachment, :uploaded_by
4637

4738
csv do
48-
column I18n.t("active_admin.required_operator_document_page.exists") do |annex|
49-
annex.deleted_at.nil?
50-
end
5139
column :status
52-
5340
column I18n.t("active_admin.operator_page.documents") do |annex|
5441
documents = []
5542
annex.annex_documents.each do |ad|
@@ -60,7 +47,6 @@ def scoped_collection
6047
column I18n.t("active_admin.dashboard_page.columns.operator") do |annex|
6148
annex.annex_documents.first&.documentable&.operator&.name
6249
end
63-
6450
column I18n.t("activerecord.models.user") do |annex|
6551
annex.user&.name
6652
end
@@ -71,9 +57,6 @@ def scoped_collection
7157
end
7258

7359
index do
74-
bool_column I18n.t("active_admin.required_operator_document_page.exists") do |od|
75-
od.deleted_at.nil?
76-
end
7760
tag_column :status
7861
column I18n.t("active_admin.operator_page.documents") do |od|
7962
next if od.annex_document.nil?
@@ -93,17 +76,24 @@ def scoped_collection
9376
fmu = doc.documentable_type.constantize.unscoped.find(doc.documentable_id).fmu
9477
link_to(fmu.name, admin_fmu_path(fmu.id)) if fmu
9578
end
96-
9779
column :user, sortable: "users.name"
9880
column :expire_date
9981
column :start_date
10082
column :created_at
10183
column :uploaded_by
10284
column :attachment do |o|
103-
link_to o.attachment&.identifier, o.attachment&.url
85+
if o.attachment&.identifier.present?
86+
name = o.attachment.identifier
87+
name += " (Missing file)" if o.attachment.blank?
88+
link_to name, o.attachment.url
89+
end
90+
end
91+
if params[:scope] == "archived"
92+
column :deleted_at
93+
else
94+
column(I18n.t("active_admin.approve")) { |annex| link_to I18n.t("active_admin.approve"), approve_admin_operator_document_annex_path(annex), method: :put }
95+
column(I18n.t("active_admin.reject")) { |annex| link_to I18n.t("active_admin.reject"), reject_admin_operator_document_annex_path(annex), method: :put }
10496
end
105-
column(I18n.t("active_admin.approve")) { |annex| link_to I18n.t("active_admin.approve"), approve_admin_operator_document_annex_path(annex), method: :put }
106-
column(I18n.t("active_admin.reject")) { |annex| link_to I18n.t("active_admin.reject"), reject_admin_operator_document_annex_path(annex), method: :put }
10797
actions
10898
end
10999

app/models/operator_document_annex.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class OperatorDocumentAnnex < ApplicationRecord
3232
class_name: "AnnexDocument", inverse_of: :operator_document_annex
3333
has_many :operator_document_histories, through: :annex_documents_history, source: :documentable, source_type: "OperatorDocumentHistory"
3434

35+
skip_callback :commit, :after, :remove_attachment!
36+
after_real_destroy :remove_attachment!
37+
3538
before_validation(on: :create) do
3639
self.status = OperatorDocumentAnnex.statuses[:doc_pending]
3740
end

spec/factories/operator_document_annexes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
start_date { Date.yesterday }
2222
expire_date { Date.tomorrow }
2323
name { "annex name" }
24+
attachment { Rack::Test::UploadedFile.new(File.join(Rails.root, "spec", "support", "files", "doc.pdf")) }
2425

2526
transient do
2627
force_status { nil }

spec/models/operator_document_annex_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@
2525
expect(operator_document_annex).to be_valid
2626
end
2727

28+
describe "deletion" do
29+
let!(:annex) { create(:operator_document_annex) }
30+
31+
context "when soft deleting record" do
32+
it "does not delete the original file" do
33+
original_file_path = annex.attachment.file.file
34+
expect(File.exist?(original_file_path)).to be true
35+
annex.destroy!
36+
expect(annex.deleted?).to be true
37+
expect(File.exist?(original_file_path)).to be true
38+
expect(annex.attachment.file.file).to eq(original_file_path)
39+
end
40+
end
41+
42+
context "when hard deleting record" do
43+
it "deletes the original file" do
44+
original_file_path = annex.attachment.file.file
45+
expect(File.exist?(original_file_path)).to be true
46+
annex.really_destroy!
47+
expect(File.exist?(original_file_path)).to be false
48+
end
49+
end
50+
end
51+
2852
describe "Instance methods" do
2953
describe "#expire_document_annex" do
3054
it "set status to doc_expired" do

0 commit comments

Comments
 (0)