Skip to content

Commit

Permalink
Merge pull request #7990 from ministryofjustice/CTSKF-1002
Browse files Browse the repository at this point in the history
CTSKF-1002 - CCCD - Task to copy message attachment (singular) to attachments (plural)
  • Loading branch information
VinceChiuMOJ authored Jan 16, 2025
2 parents 4e35052 + cd0b58f commit 040967b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Message < ApplicationRecord
attr_accessor :claim_action, :written_reasons_submitted

has_one_attached :attachment
has_many_attached :attachments

validates :attachment,
size: { less_than: 20.megabytes },
Expand All @@ -48,7 +49,8 @@ class Message < ApplicationRecord

scope :most_recent_last, -> { includes(:user_message_statuses).order(created_at: :asc) }

after_create :generate_statuses, :process_claim_action, :process_written_reasons, :send_email_if_required
after_create :generate_statuses, :process_claim_action, :process_written_reasons, :send_email_if_required,
:duplicate_message_attachment
before_destroy -> { attachment.purge }

class << self
Expand Down Expand Up @@ -107,4 +109,10 @@ def process_written_reasons
def claim_updater
Claims::ExternalUserClaimUpdater.new(claim, current_user: sender)
end

def duplicate_message_attachment
return unless attachment.attached?

attachments.attach(attachment.blob)
end
end
26 changes: 26 additions & 0 deletions lib/tasks/documents.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace :documents do
desc 'Copy all message attachment to message attachments.'
task :duplicate_message_attachment => :environment do
messages = Message.all
puts "There are #{messages.count} messages."

messages.each do |message|
if message.attachment.attached?
puts "Duplicating attachment of message ##{message.id}."
attachment_blob = message.attachment.blob

message.attachments.attach(attachment_blob)
puts "Attachment #{attachment_blob.filename} is duplicated in the database."
else
puts "There is no attachment in message ##{message.id}."
end
end
puts "duplicate_message_attachment done!"
end

desc'Count blob map.'
task :count_blob_map => :environment do
blobs = ActiveStorage::Attachment.includes(:blob, blob: :attachments).where(name: 'attachment').map(&:blob)
puts blobs.map { |blob| blob.attachments.count }.tally
end
end
2 changes: 1 addition & 1 deletion spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
context 'with an attachment' do
let(:trait) { :with_attachment }

it { expect { destroy_message }.to change(ActiveStorage::Attachment, :count).by(-1) }
it { expect { destroy_message }.to change(ActiveStorage::Attachment, :count).by(-2) }
it { expect { destroy_message }.to change(ActiveStorage::Blob, :count).by(-1) }
end
end
Expand Down

0 comments on commit 040967b

Please sign in to comment.