Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTSKF-1002 - CCCD - Task to copy message attachment (singular) to attachments (plural) #7990

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 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,11 @@ def process_written_reasons
def claim_updater
Claims::ExternalUserClaimUpdater.new(claim, current_user: sender)
end

def duplicate_message_attachment
return unless attachment.attached?

attachment_blob = attachment.blob
attachments.attach(attachment_blob) unless attachments.find_by(blob: attachment_blob)
VinceChiuMOJ marked this conversation as resolved.
Show resolved Hide resolved
end
end
25 changes: 25 additions & 0 deletions lib/tasks/documents.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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

existing_attachment = message.attachments.find_by(blob: attachment_blob)
if existing_attachment
puts "Attachment #{existing_attachment.blob.filename} is already duplicated in the database with ID #{existing_attachment.id}."
else
message.attachments.attach(attachment_blob)
puts "Attachment #{attachment_blob.filename} is duplicated in the database."
end
else
puts "There is no attachment in message ##{message.id}."
end
end
puts "duplicate_message_attachment done!"
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